c# - Is there a way to ignore get-only properties in Json.NET without using JsonIgnore attributes? -


is there way ignore get-only properties using json.net serializer without using jsonignore attributes?

for example, have class these properties:

    public keys hotkey { get; set; }      public keys keycode     {                 {             return hotkey & keys.keycode;         }     }      public keys modifierskeys     {                 {             return hotkey & keys.modifiers;         }     }      public bool control     {                 {             return (hotkey & keys.control) == keys.control;         }     }      public bool shift     {                 {             return (hotkey & keys.shift) == keys.shift;         }     }      public bool alt     {                 {             return (hotkey & keys.alt) == keys.alt;         }     }      public modifiers modifiersenum     {                 {             modifiers modifiers = modifiers.none;              if (alt) modifiers |= modifiers.alt;             if (control) modifiers |= modifiers.control;             if (shift) modifiers |= modifiers.shift;              return modifiers;         }     }      public bool isonlymodifiers     {                 {             return keycode == keys.controlkey || keycode == keys.shiftkey || keycode == keys.menu;         }     }      public bool isvalidkey     {                 {             return keycode != keys.none && !isonlymodifiers;         }     } 

do need add [jsonignore] of them (i have many other classes), or there better way ignore get-only properties?

you can implementing custom icontractresolver , using during serialization. if subclass defaultcontractresolver, becomes easy do:

class writablepropertiesonlyresolver : defaultcontractresolver {     protected override ilist<jsonproperty> createproperties(type type, memberserialization memberserialization)     {         ilist<jsonproperty> props = base.createproperties(type, memberserialization);         return props.where(p => p.writable).tolist();     } } 

here test program demonstrating how use it:

using system; using system.collections.generic; using system.linq; using newtonsoft.json; using newtonsoft.json.serialization;  class program {     static void main(string[] args)     {         widget w = new widget { id = 2, name = "joe schmoe" };          jsonserializersettings settings = new jsonserializersettings         {             contractresolver = new writablepropertiesonlyresolver()         };          string json = jsonconvert.serializeobject(w, settings);          console.writeline(json);     } }  class widget {     public int id { get; set; }     public string name { get; set; }     public string lowercasename     {         { return (name != null ? name.tolower() : null); }     } } 

here output of above. notice read-only property lowercasename not included in output.

{"id":2,"name":"joe schmoe"} 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -