I have written a single page application that uses rest services to retrieve JSON objects. The JSON objects being returned are C# objects serialized using the Newtonsoft.JSON library. The returned JSON object contains $ref pointers to specific $id objects within the same JSON object (The question here shows an example). I need to access the actual data that the $ref points to, but I do not know how to access it without locating the original source.
I've found solutions such as writing a new contract serializer and writing my own implementation of resolving the references. I feel like there should be an easier solution than these two. I've also explored the Newtonsoft JSON serialization settings; I've tried setting PreserveReferenceHandling to None, but I haven't had any success there.
I'm looking for solutions other than the ones I've mentioned. I'm hoping there's a simple solution that requires very little code to implement and no additional frameworks.
UPDATE
I've been reading some more about this and discovered that $ref is a JSON Pointer fragment. The reference number is an ID that references an item from a different schema. That appears to be the solution, but a schema needs to be defined for it to work.
RFC for JSON Pointer Fragments
Info about JSON Pointer Fragments
References:
Resolve circular JSON references
Serialization Settings
Best Answer
I was able to resolve the "$ref" links in my json code by adapting the code, this blogger posted, to be Angular compliant.
http://willseitz-code.blogspot.com/2013/01/javascript-to-deserialize-json-that.html
I created an angular service to resolve the "$ref" I get from NewtonSofts JSON serializer.
Usage would be like so:
Please keep in mind, this is an Angular service. So I inject it into whichever controller needs to have these references resolved, and make the call to pointerParse() accordingly.
Please note though that if you want to post this object back to the server, your browsers JSON.stringify() call has to be able to handle an object with circular references. By default, in my case, Angular does not call JSON.stringify() with the parameters required to be able to stringify an object with circular references. So, in this case I would suggest posting only a small portion of data and not this entire object back or instantiating your own call to stringify an object like this.
Lastly, I must say that dealing with $ref is a pain and I decided to preserve reference handling with only arrays. This seemed to work well for me. I still had to access array values with $values however I didnt have to worry about resolving references and didnt have to worry about the server blowing up trying to serialize all of the object circular references to json.
Update: Even with only resolving arrays there are still references that you may need to resolve. In those instances use the pointerParse() function to take care of them.