showing results for - "json serializer skip property"
Filippo
22 Jun 2018
1public class Account
2{
3    public string FullName { get; set; }
4    public string EmailAddress { get; set; }
5
6    [JsonIgnore]
7    public string PasswordHash { get; set; }
8}
Paul
11 Jun 2017
1public class PropertyRenameAndIgnoreSerializerContractResolver : DefaultContractResolver
2{
3    private readonly Dictionary<Type, HashSet<string>> _ignores;
4    private readonly Dictionary<Type, Dictionary<string, string>> _renames;
5
6    public PropertyRenameAndIgnoreSerializerContractResolver()
7    {
8        _ignores = new Dictionary<Type, HashSet<string>>();
9        _renames = new Dictionary<Type, Dictionary<string, string>>();
10    }
11
12    public void IgnoreProperty(Type type, params string[] jsonPropertyNames)
13    {
14        if (!_ignores.ContainsKey(type))
15            _ignores[type] = new HashSet<string>();
16
17        foreach (var prop in jsonPropertyNames)
18            _ignores[type].Add(prop);
19    }
20
21    public void RenameProperty(Type type, string propertyName, string newJsonPropertyName)
22    {
23        if (!_renames.ContainsKey(type))
24            _renames[type] = new Dictionary<string, string>();
25
26        _renames[type][propertyName] = newJsonPropertyName;
27    }
28
29    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
30    {
31        var property = base.CreateProperty(member, memberSerialization);
32
33        if (IsIgnored(property.DeclaringType, property.PropertyName))
34        {
35            property.ShouldSerialize = i => false;
36            property.Ignored = true;
37        }
38
39        if (IsRenamed(property.DeclaringType, property.PropertyName, out var newJsonPropertyName))
40            property.PropertyName = newJsonPropertyName;
41
42        return property;
43    }
44
45    private bool IsIgnored(Type type, string jsonPropertyName)
46    {
47        if (!_ignores.ContainsKey(type))
48            return false;
49
50        return _ignores[type].Contains(jsonPropertyName);
51    }
52
53    private bool IsRenamed(Type type, string jsonPropertyName, out string newJsonPropertyName)
54    {
55        Dictionary<string, string> renames;
56
57        if (!_renames.TryGetValue(type, out renames) || !renames.TryGetValue(jsonPropertyName, out newJsonPropertyName))
58        {
59            newJsonPropertyName = null;
60            return false;
61        }
62
63        return true;
64    }
65}
66
queries leading to this page
json ignore property c 23how to access jsonignore in c 23ignore propery json c 23jsonconvert serializeobject skip propertiesc 23 json ignore errorsnewtonsoft json ignore propertyignore json property c 23json serializer skip propertynewtonsoft json include exclude tagsjson ignore c 23jsonproperty annotation c 23 ignorejsonconvert serializeobject how to skip propertiesc 23json ignorejson not mapped c 23jsonproperty c 23 ignore for responsejson ignore json netjson 2cnet ignore fieldjson ignore in c 23 when nullc 23 newtonsoft class ignore propertyjson ignore attribute c 23newtonsoft ignore property when serializingnewtonsoft jsonproperty ignorec 23 json ignorec 23 ignore json propertyignore property in json serialization c 23c 23 attribute json ignorewhat dispayed in json when ignorejson attribute is on property 3fhow to ignore json properties in c 23json ignore in c 23json attribute c 23 ignorejsonignore c 23 newtonsoft usingnewtsoft json ignorejson ignorejson ignore propertyignore property serialization jsonnewtonsoft serialize object ignore propertynewtonsoft json not serialize specific fieldsjsonignore c 23newtonsoft serialize ignore propertyjsonnet ignorenewtonsoft serializeobject ignore propertyexclude property from json serialization newtonsoftjsonignore c 23 newtonsoftnewtonsoft json ignore properties on putc 23 attribute ignore jsonjson serialization c 23 skip propertyjson serialize ignore propertynewtonsoft json exclude propertyjson propertuy ignorehow to have jsonproperty ignore c 23newtonsoft jsonserializer ignore propertyc 23 system json ignore propertynewtonsoft json serialization ignorwe c 23 json ignore propertyignore vs json ignore c 23jsonignore newtonsoftjson annotation c 23 ignoreserializeobject except propertyhow to exclude property from json serialization in newtonsoftjson ignore owned property c 23newton soft serialize ignore propertiesignore json property case netc 23 json ignore deserializationnet json format ignore propertyignore property json c 23newtonsft json ignorc 23 json ignore attributejson net ignore propertyhow to use jsonignoreattributec 23 property json ignorejson property ignore c 23ignore json property name c 23json net don 27t serialize propertydot net json ignorenewtonsoft jsonserializer exclude propertyignore field without json ignoreset attribute ignore when serializing with newtonsoftjson property ignorec 23 jsonconvert ignore propertiesc 23 newtonsoft json not serialize propertynewtonsoft json serialize only certain propertiesexclude property from json serialization c 23json ignore property at runtime c 23ignore property json serialization c 23 newtonsoftjson net property ignorejson serializer ignore propertyjsonproperty c 23 ignorejson ignore in c 23jsonserializer ignore property c 23c 23 model json ignorehow to make json ignore c 23json ignore 5dnewtonsoft json ignore propertynewtonsoft ignore propertyc 23 jsonproperty ignorec 23 newtonsoft json jsonignoreattributenewtonsoft jsonignore examplec 23 json ignore default valuenewtonsoft json exclude property from serializationc 23 json ignore property serializerjson ignore property unless specified c 23 jsonignore for serializationjson net ignore property during serializationjson serializer skip property