Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a JavaScript (client-side) JSON library that integrates well with JSON.NET (server-side) and supports the "preserve references" feature?
    primarykey
    data
    text
    <p>JSON.NET supports circular reference serialization by preserving all references with the following settings:</p> <pre><code>JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; settings.PreserveReferencesHandling = PreserveReferencesHandling.All; </code></pre> <p>That allows the following code to run without errors, properly serializing and deserializing the object with its self-reference intact.</p> <pre><code>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&lt;SelfReferencingClass&gt;( jsondata, settings ); </code></pre> <p>The jsondata string looks like this:</p> <pre><code>{"$id":"1","Name":"Default","Self":{"$ref":"1"}} </code></pre> <p>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?</p> <p><strong>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?</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload