You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Extensions/JsonExtensions.cs

27 lines
830 B

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Infrastructure.Extensions
{
public static class JsonExtensions
{
public static string ToJson(this object value, bool isCamelCase = false)
{
return JsonConvert.SerializeObject(value, GetSettings(isCamelCase));
}
public static T FromJson<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json, GetSettings(false));
}
private static JsonSerializerSettings GetSettings(bool isCamelCase)
{
return new JsonSerializerSettings
{
ContractResolver = isCamelCase ? new CamelCasePropertyNamesContractResolver() : null,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
}
}
}