JSON.NET supports circular reference serialization by preserving all references with the following settings:
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
settings.PreserveReferencesHandling = PreserveReferencesHandling.All;
That allows the following code to run without errors, properly serializing and deserializing the object with its self-reference intact.
public class SelfReferencingClass
{
public string Name;
public SelfReferencingClass Self;
public SelfReferencingClass() {Name="Default"; Self=this;}
}
SelfReferencingClass s = new SelfReferencingClass();
string jsondata = JsonConvert.SerializeObject( d, settings );
s = JsonConvert.DeserializeObject<SelfReferencingClass>( jsondata, settings );
The jsondata string looks like this:
{"$id":"1","Name":"Default","Self":{"$ref":"1"}}
The problem is... how is this feature of JSON.NET useful at all without a corresponding client side JavaScript library that can interpret these references, and also support encoding such references itself?
What client-side library (e.g. JSON.stringify) supports this feature/encoding using "$id" and "$ref" fields? If none exist, is there a known way to add support to an existing library?
Adding support myself would be a pretty straightforward two-pass process. First, deserialize the entire string, and as you create each object add it to a dictionary using its "$id" value as the key. When you encounter references (object consisting of just a "$ref" property) you could add it to a list of object+fieldname that you could go back over to replace each encountered reference by looking up its key in the final dictionary of created objects.
JSON.stringify
.' – Guanajuato