-
Notifications
You must be signed in to change notification settings - Fork 5
/
JsonHelper.cs
79 lines (68 loc) · 3.32 KB
/
JsonHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace JPLOPS.Util
{
public class IgnorePropertiesResolver : DefaultContractResolver
{
private HashSet<string> ignore;
/// <summary>
/// skip certain properties for serialization
/// each name should be of the form TypeName.PropertyName
/// </summary>
public IgnorePropertiesResolver(string[] names)
{
ignore = new HashSet<string>(names);
}
protected override JsonProperty CreateProperty(System.Reflection.MemberInfo mi, MemberSerialization ms)
{
JsonProperty prop = base.CreateProperty(mi, ms);
string name = prop.DeclaringType.Name + "." + prop.PropertyName;
prop.Ignored = ignore.Contains(name);
return prop;
}
}
public class JsonHelper
{
public static JsonSerializerSettings Settings(bool autoTypes = true, string[] ignoreProperties = null,
bool ignoreNulls = false)
{
var settings = new JsonSerializerSettings();
if (autoTypes) settings.TypeNameHandling = TypeNameHandling.Auto;
if (ignoreNulls) settings.NullValueHandling = NullValueHandling.Ignore;
if (ignoreProperties != null) settings.ContractResolver = new IgnorePropertiesResolver(ignoreProperties);
//serialize enums as their string equivalents instead of ints
//the main reason is to reduce backwards compatibility problems if we add values to an enum
//this also improves readability of the local database
//but at the expense of increased disk usage
//(enums that were previously serialized as ints are still accepted)
settings.Converters.Add(new StringEnumConverter());
return settings;
}
public static string ToJson(Object o, bool indent = false, bool autoTypes = true,
string[] ignoreProperties = null, bool ignoreNulls = false)
{
Formatting formatting = indent ? Formatting.Indented : Formatting.None;
return JsonConvert.SerializeObject(o, typeof(object), formatting,
Settings(autoTypes, ignoreProperties, ignoreNulls));
}
public static object FromJson(string json, bool autoTypes = true, string[] ignoreProperties = null,
bool ignoreNulls = false)
{
return JsonConvert.DeserializeObject(json, Settings(autoTypes, ignoreProperties, ignoreNulls));
}
public static T FromJson<T>(string json, bool autoTypes = true, string[] ignoreProperties = null,
bool ignoreNulls = false)
{
return JsonConvert.DeserializeObject<T>(json, Settings(autoTypes, ignoreProperties, ignoreNulls));
}
public static object FromJson(string json, object obj, bool autoTypes = true, string[] ignoreProperties = null,
bool ignoreNulls = false)
{
JsonConvert.PopulateObject(json, obj, Settings(autoTypes, ignoreProperties, ignoreNulls));
return obj;
}
}
}