c# - Dynamic JsonIgnore for properties within a list -


i using json.net library. have json looks this:

"templates": [     {         "name": "default fields 1",         "fields": [16, 10, 0, 4, 5, 11, 12, 7, 15, 17, 9, 25, 3],         "formats": [             "string", "datetime", "leftzeropaddedstring13", "2dp", "2dp", "2dp", "2dp", "int", "int",             "string", "int", "int", "int", "int"         ]     },     {         "name": "default fields 2",         "fields": [3, 25, 9, 17, 15, 7, 12, 11, 5, 4, 0, 10, 16],         "formats": [             "int", "int", "int", "int", "string", "int", "int", "2dp", "2dp", "2dp", "2dp", "leftzeropaddedstring13",             "datetime", "string"         ]     }] 

and deserializing templates property within following class:

public class options {     public list<fieldtemplate> templates { get; set; } } public class fieldtemplate {     public string name { get; set; }     public list<int> fields { get; set; }     public list<string> formats { get; set; } } 

this works fine, under circumstances (not always) want prevent fields , formats properties being included when serialize object again (though want keep name property in serialized output). have thought using shouldserializefields() , shouldserializeformats() within fieldtemplate , looping through object set boolean property each of these methods can read, doesn't seem elegant. there better way? example, set boolean properties in options fieldtemplate use. don't know how though, or if possible.

in end used static class switch each property wanted dynamically show/hide:

public class fieldtemplate_jsonserialization_switches {     public static bool fields = true;     public static bool formats = true; }  public class options {     public list<fieldtemplate> templates { get; set; } }  public class fieldtemplate {     public string name { get; set; }     public list<int> fields { get; set; }     public list<string> formats { get; set; }      // instructions json.net     public bool shouldserializefields()     {         return fieldtemplate_jsonserialization_switches.fields;     }      public bool shouldserializeformats()     {         return fieldtemplate_jsonserialization_switches.formats;     } } 

then elsewhere can enable/disable these properties:

if (condition) {     fieldtemplate_jsonserialization_switches.fields = true;     fieldtemplate_jsonserialization_switches.formats = false; } else {     fieldtemplate_jsonserialization_switches.fields = false;     fieldtemplate_jsonserialization_switches.formats = true; } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -