Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:48:27+00:00 2026-06-15T18:48:27+00:00

I’m using SignalR to return a complex object graph to my JavaScript client. This

  • 0

I’m using SignalR to return a complex object graph to my JavaScript client. This object graph has multiple references throughout to the same object, so the JSON that SignalR/Json.NET returns looks a lot like this:

{
    "$id": "57",
    "Name": "_default",
    "User": {
        "$id": "58",
        "UserTag": "ken",
        "Sessions": [{
            "$id": "59",
            "SessionId": "0ca7474e-273c-4eb2-a0c1-1eba2f1a711c",
            "User": {
                "$ref": "58"
            },
            "Room": {
                "$ref": "57"
            }
        }],
    },

    "Sessions": [{
        "$ref": "59"
    }]
}

(Of course, a lot more complicated in real life, but you get the idea.)

And of course, when Json.NET is serializing by reference rather than by value, it assigns each object a $id value (e.g., "$id":"57", and then later just refers to that object using that id (e.g., "$ref":"57". And so far as I can tell, when it is Json.NET (using C#/.NET) that is deserializing those references, it places the appropriate instances of the object in the appropriate places.

All good so far – but what’s the best way to deserialize these in JavaScript, so that I actually get the appropriate object instances in the appropriate places, instead of just weird $ref fields?

I could presumably write my own general-purpose deserializer, but I have to imagine that somebody else has already tackled this problem, and I’d just as soon not reinvent any wheels. Unfortunately, my Google skills apparently aren’t sufficient to locate that solution :-).

Edit:

I see that there’s an IETF draft proposal about how this sort of thing is supposed to work. And it looks like the always helpful Douglas Crockford has a tentative implementation of it. Unfortunately, the IETF proposal uses a different schema than Json.NET uses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:48:28+00:00Added an answer on June 15, 2026 at 6:48 pm

    Well, I think this will do it. I modified Crockford’s cycle.js to handle the reference format that Json.NET uses. And because TypeScript is an unspeakably better language than JavaScript, I rewrote it in TS. I certainly don’t swear that it has no bugs (if anybody points them out, I’ll try to fix ’em), but it seems to handle the complex object graphs I’ve throw at it so far.

    export function retrocycle(obj: any): void {
        var catalog: any[] = [];
        catalogObject(obj, catalog);
        resolveReferences(obj, catalog);
    }
    
    function catalogObject(obj, catalog: any[]):void {
    
        // The catalogObject function walks recursively through an object graph
        // looking for $id properties. When it finds an object with that property, then
        // it adds it to the catalog under that key.
    
        var i: number;
        if (obj && typeof obj === 'object') {
            var id:string = obj.$id;
            if (typeof id === 'string') {
                catalog[id] = obj;
            }
    
            if (Object.prototype.toString.apply(obj) === '[object Array]') {
                for (i = 0; i < obj.length; i += 1) {
                    catalogObject(obj[i], catalog);
                }
            } else {
                for (name in obj) {
                    if (typeof obj[name] === 'object') {
                        catalogObject(obj[name], catalog);
                    }
                }
            }
        }
    }
    
    function resolveReferences(obj: any, catalog: any[]) {
    
        // The resolveReferences function walks recursively through the object looking for $ref
        // properties. When it finds one that has a value that is an id, then it
        // replaces the $ref object with a reference to the object that is found in the catalog under
        // that id.
    
        var i:number, item:any, name:string, id:string;
    
        if (obj && typeof obj === 'object') {
            if (Object.prototype.toString.apply(obj) === '[object Array]') {
                for (i = 0; i < obj.length; i += 1) {
                    item = obj[i];
                    if (item && typeof item === 'object') {
                        id = item.$ref;
                        if (typeof id === 'string') {
                            obj[i] = catalog[id];
                        } else {
                            resolveReferences(item, catalog);
                        }
                    }
                }
            } else {
                for (name in obj) {
                    if (typeof obj[name] === 'object') {
                        item = obj[name];
                        if (item) {
                            id = item.$ref;
                            if (typeof id === 'string') {
                                obj[name] = catalog[id];
                            } else {
                                resolveReferences(item, catalog);
                            }
                        }
                    }
                }
            }
        }
    }
    

    And the equivalent JS:

    function retrocycle(obj) {
        var catalog = [];
        catalogObject(obj, catalog);
        resolveReferences(obj, catalog);
    }
    
    function catalogObject(obj, catalog) {
        var i;
        if (obj && typeof obj === 'object') {
            var id = obj.$id;
            if (typeof id === 'string') {
                catalog[id] = obj;
            }
            if (Object.prototype.toString.apply(obj) === '[object Array]') {
                for (i = 0; i < obj.length; i += 1) {
                    catalogObject(obj[i], catalog);
                }
            } else {
                for (name in obj) {
                    if (typeof obj[name] === 'object') {
                        catalogObject(obj[name], catalog);
                    }
                }
            }
        }
    }
    
    function resolveReferences(obj, catalog) {
        var i, item, name, id;
        if (obj && typeof obj === 'object') {
            if (Object.prototype.toString.apply(obj) === '[object Array]') {
                for (i = 0; i < obj.length; i += 1) {
                    item = obj[i];
                    if (item && typeof item === 'object') {
                        id = item.$ref;
                        if (typeof id === 'string') {
                            obj[i] = catalog[id];
                        } else {
                            resolveReferences(item, catalog);
                        }
                    }
                }
            } else {
                for (name in obj) {
                    if (typeof obj[name] === 'object') {
                        item = obj[name];
                        if (item) {
                            id = item.$ref;
                            if (typeof id === 'string') {
                                obj[name] = catalog[id];
                            } else {
                                resolveReferences(item, catalog);
                            }
                        }
                    }
                }
            }
        }
    }
    

    You use it kinda like so (assuming you’ve got SignalR hubs wired up):

    $.connection.roomHub.server.joinRoom()
        .done(function(room) {
            retrocycle(room);
        });
    

    I also created a quick-and-dirty little repository out on BitBucket for it: https://bitbucket.org/smithkl42/jsonnetdecycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.