c# - Handling reference loops in JSON.net -


i wish serialize collection (list<item>) of items json.

these items have collection of connection gives information connection 1 item second item. , since connection object has reference items makes infinite loop.

my question is there way me skip serialization of connection collection when serializing object second time.

i've tried things inheriting jsonconverter , writing custom writejson() method there have no sence whether should write out array or not.

i've tried using custom contractresolver no results.


classes

public class item {     private static int _lastid = 0;      public item()     {         id = ++_lastid;         connections = new list<connection>();     }       public int id { get; set; }      public string name { get; set; }      public string prop1 { get; set; }      public string prop2 { get; set; }      public list<connection> connections { get; set; }  }    public class connection {     private connection(connectiontype type, item source, item target)     {         if (type == connectiontype.none)             throw new argumentexception();         if (source == null)             throw new argumentnullexception("source");         if (target == null)             throw new argumentnullexception("target");          type = type;         source = source;         target = target;     }       public connectiontype type { get; set; }      public item source { get; set; }      public item target { get; set; }       public static void connect(connectiontype type, item source, item target)     {         var conn = new connection(type, source, target);         source.connections.add(conn);         target.connections.add(conn);     } } 


wanted result:

[     {         "id": 1,         "name": "item #1",         "prop1": "val1",         "prop2": "val2",         "connections": {             "type": "conntype",             "source": {                 "id": 1,                 "name": "item #1",                 "prop1": "val1",                 "prop2": "val2"                 // no connections array             },             "target": {                 "id": 2,                 "name": "item #2",                 "prop1": "val1",                 "prop2": "val2"                 // no connections array             }         }     },     {         "id": 2,         "name": "item #2",         "prop1": "val1",         "prop2": "val2",         "connections": {             "type": "conntype",             "source": {                 "id": 1,                 "name": "item #1",                 "prop1": "val1",                 "prop2": "val2"                 // no connections array             },             "target": {                 "id": 2,                 "name": "item #2",                 "prop1": "val1",                 "prop2": "val2"                 // no connections array             }         }     } ] 



edit:

c#

var settings = new jsonserializersettings     {         contractresolver = new camelcasepropertynamescontractresolver(),         referenceloophandling = referenceloophandling.ignore,         formatting = formatting.indented     }; settings.converters.add(new stringenumconverter()); var json = jsonconvert.serializeobject(collection, settings); 

instead of declaring type of source , target of item, may introduce new class, say, itemclass, which'll contain fields of class item, except connections property.

public class itemclass {     public int id { get; set; }      public string name { get; set; }      public string prop1 { get; set; }      public string prop2 { get; set; } }  public class connection {     // ...      public connectiontype type { get; set; }      public itemclass source { get; set; }      public itemclass target { get; set; }      // ... } 

now you'll have overhead of populating new itemclass type instance accordingly.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -