1

Closed

It's impossible to create custom ReferenceResolver.

description

I try to solve problem about JSON referencing like the following code.
var json = {};
json.a = { foo: 'bar' };
json.b = { foo: null, parent: json.a };
Normally, we can change "ReferenceResolver" setting to proper class to handle reference value.

Image

However, JSON.net pass context object as internal class that I cannot access any property on it. Moreover, this internal doesn't has any information about current processing object.

Image

So, it's impossible to serialize the above JSON like this.
{
    "a": { "foo": "bar" },
    "b": { "foo": null, parent: "#a" }
}
For more information about JSON referencing, please click at the following link.
http://www.sitepen.com/blog/2008/06/17/json-referencing-in-dojo/

file attachments

comments

SoulMaster wrote Apr 6, 2013 at 4:45 AM

So, it's limitation of your library. However, I cannot implement any custom reference resolver. becuase your given context object is always an internal class. It's totally useless. I don't know why you create this setting as public property but no one can change this value and no documentation at all.

Moreover, your id-based resolver has some limitation when work with complex object. It always serealize duplicate reference $id.

SoulMaster wrote Apr 6, 2013 at 4:49 AM

Ps. Default reference resolver is internal class too.

JamesNK wrote Apr 6, 2013 at 4:50 AM

Its based on id, not location.

** Closed by JamesNK 4/5/2013 2:05 PM

SoulMaster wrote Apr 6, 2013 at 8:41 AM

You still don't understand my point. All of given context object and default reference resolver are internal. I cannot return any value in ResolveReference and other methods.

If it possible to create custom IResolveReference interface. Please give me an example.

JamesNK wrote Apr 11, 2013 at 6:52 AM

The internal object is there so implementers can optionally use it to keep track of when the deserialization context has changed. You don't need to use it.

** Closed by JamesNK 4/5/2013 11:55 PM

SoulMaster wrote May 6, 2013 at 7:26 AM

It is a limitation of deserialization process that cannot access JToken object or structure of current serialized object. So, we need to keep data in "$id" property and add in dictionary to search later.

However, I still prefer path-based reference because it generates very small without provide any information in "$id" property.
var json = 
[
    {
        "Name": "John Smith",
        "Spouse": 
        {
            "Name": "Jane Smith",
            "Spouse": { "$ref": "[0]" }
        }
    },
    {
        "$ref": "[0].Spouse"
    }
]