29 lines
870 B
C#
29 lines
870 B
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System;
|
|
|
|
namespace SuccessfulCodeTools.Extensions
|
|
{
|
|
public static class ObjectExtensions
|
|
{
|
|
public static Lazy<JsonSerializerSettings> _settings = new Lazy<JsonSerializerSettings>(GetSettings);
|
|
|
|
private static JsonSerializerSettings GetSettings()
|
|
{
|
|
var settings = new JsonSerializerSettings()
|
|
{
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
};
|
|
settings.Converters.Add(new StringEnumConverter());
|
|
settings.NullValueHandling = NullValueHandling.Ignore;
|
|
return settings;
|
|
}
|
|
|
|
public static string ToJson(this object obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj, _settings.Value);
|
|
}
|
|
}
|
|
}
|