0

I have been looking for an answer to this all afternoon and i cant seem to find the best way to accomplish what i need to.

My JSON string (returned from a web service) has circular references in it (#ref) which point to $id in the string. Now i know that if use jquery parseJSON it creates the javascript object and i can access properties a la myObject.MyPropertyName. However, when i get to a #ref, i am unsure how to get the object that ID points to (which i assume is already created as a result of the de-serialization...

Should i be iterating though the object and all its child objects until i find it, or is there an easier way?

$.ajax({
        type: "POST",
        url: "/Task.asmx/GetTask",
        data: "{'id':'" + '27' + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            _Data = $.parseJSON(msg.d ? msg.d : msg);
            _this.Company = _Data[0].t_Program.t_Company;
            _this.Program = _Data[0].t_Program;
            _this.Task = _Data[0];
        },
        complete: function () {

        }
    });

The area in question is _Data[0].t_Program because it does not return an object but rather returns

_Data[0].t_Program
 {...}
   $ref: "12"

I dont exactly know the best way to get the object with $id "12". Based on the posts below it seems i should loop through the existing object, but i was hoping there was a jquery function that did that...

Many Thanks!

3
  • Try posting some of your code as an example so we can fully picture what you are trying to do. good luck! :) May 25, 2012 at 1:21
  • let the code speak for itself!
    – Joseph
    May 25, 2012 at 1:21
  • Welcome to stack overflow. Remember to upvote ALL answers that you find useful, including those to others' questions. And "check" / accept the answer to your questions which best answer your questions.
    – Larry K
    May 25, 2012 at 2:08

3 Answers 3

1

No, jQuery is not natively capable of resolving circular references in objects converted from JSON.

The only library for that which I know is Dojo's dojox.json.ref module.

But, your server application serializes that JSON somehow. Don't tell me that the solution it uses does not offer a deserialisation algorithm!

1
0

As my friend Alan, the author of the Xerox Courier (RPC over the net) library, used to say to me, "there are no pointers on the wire."

In other words, it is impossible for a JSON representation of a data structure to be circular. (But a circular structure can be flattened into a non-circular JSON structure.) As the JSON site says:

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

No pointers!

So the entire JSON will have been turned into Javascript Objects and/or Arrays after the jQuery parseJSON operation completes.

If the original stucture's ref_id values were used as the property names in the JSON / Javascript object, then they'll all be there.

The real issue is that you need to understand how your server serialized its data structure into the JSON data structure. Look in your server-side code to determine that.

Then use Javascript to de-serialize the JSON structure back into the best Javascript structure to fit your needs.

Re: Should i be iterating though the object and all its child objects until i find it, or is there an easier way?

The easier way would be to go through the Javascript structure once, and build up an additional "indexing" object whose properties are the #ref_id and the values are the original structure/value.

0

Sample: var jsonCyclicReferenceFixed = JsonRecursive.parse(jsonWithRefAndId);

(function(){

function type(value){

    var t = typeof(value);

    if( t == "object" && value instanceof Array) {
        return "array";
    }

    if( t == "object" && value && "$id" in value && "$values" in value) {
        return "array";
    }

    return t;
}

function TypeConverterFactory(){

    var converters = {};

    var defaultConverter = {
        fromJson: function(value){ return value; },
        toJson: function(value){ return value; },
    };

    this.create = function(type){
        var converter = converters[type];
        if(!converter) return defaultConverter;
        return converter;
    };

    this.register = function(type, converter){
        converters[type] = converter;
        converter.valueConverter = this.valueConverter;
    };
}



function ObjectConverter(){

    this.fromJson = function(obj){

        if( obj == null ) return null;              

        if( "$ref" in obj ){
            var reference = this.dictionary[obj.$ref];
            return reference;
        }

        if("$id" in obj){
            this.dictionary[obj.$id] = obj;
            delete obj.$id;
        }

        for(var prop in obj){
            obj[prop] = this.valueConverter.convertFromJson(obj[prop]);
        }

        return obj;

    }

    this.toJson = function(obj){

        var id = 0;

        if(~(id = this.dictionary.indexOf(obj))){
            return { "$ref" : (id + 1).toString() };
        }

        var convertedObj = { "$id" : this.dictionary.push(obj).toString() };

        for(var prop in obj){
            convertedObj[prop] = this.valueConverter.convertToJson(obj[prop]);
        }

        return convertedObj;

    }

}   

function ArrayConverter(){

    var self = this;

    this.fromJson = function(arr){

        if( arr == null ) return null;              

        if("$id" in arr){

            var values = arr.$values.map(function(item){
                return self.valueConverter.convertFromJson(item);
            });

            this.dictionary[arr.$id] = values;

            delete arr.$id;

            return values;
        }

        return arr;

    }

    this.toJson = function(arr){

        var id = 0;

        if(~(id = this.dictionary.indexOf(arr))){
            return { "$ref" : (id + 1).toString() };
        }

        var convertedObj = { "$id" : this.dictionary.push(arr).toString() };

        convertedObj.$values = arr.map(function(arrItem){
            return self.valueConverter.convertToJson(arrItem);
        });

        return convertedObj;

    }

}   

function ValueConverter(){

    this.typeConverterFactory = new TypeConverterFactory();
    this.typeConverterFactory.valueConverter = this;
    this.typeConverterFactory.register("array", new ArrayConverter);
    this.typeConverterFactory.register("object", new ObjectConverter);

    this.dictionary = {};

    this.convertToJson = function(valor){

        var converter = this.typeConverterFactory.create(type(valor));
        converter.dictionary = this.dictionary;
        return converter.toJson(valor);         

    }

    this.convertFromJson = function(valor){

        var converter = this.typeConverterFactory.create(type(valor));
        converter.dictionary = this.dictionary;         
        return converter.fromJson(valor);

    }

}


function JsonRecursive(){

    this.valueConverter = new ValueConverter();

}

JsonRecursive.prototype.convert = function(obj){
    this.valueConverter.dictionary = [];
    var converted = this.valueConverter.convertToJson(obj);
    return converted;

}

JsonRecursive.prototype.parse = function(string){
    this.valueConverter.dictionary = {};
    var referenced = JSON.parse(string);
    return this.valueConverter.convertFromJson(referenced);

}


JsonRecursive.prototype.stringify = function(obj){

    var converted = this.convert(obj);
    var params = [].slice.call(arguments, 1);
    return JSON.stringify.apply(JSON, [converted].concat(params));

}

if(window){

    if( window.define ){
        //to AMD (require.js)
        window.define(function(){
            return new JsonRecursive();
        });

    }else{
        //basic exposition
        window.jsonRecursive = new JsonRecursive();
    }

    return;
}

if(global){
    // export to node.js
    module.exports = new JsonRecursive();   
}


}());

Sample:

// a object recursive
// var parent = {};
// var child = {};
// parent.child = child;
// child.parent = parent;
//
//results in this recursive json

var json = '{"$id":"0","name":"Parent","child":{"$id":"1","name":"Child","parent":{"$ref":"0"}}}'

//Parsing a Recursive Json to Object with references
var obj = jsonRecursive.parse(json);

// to see results try console.log( obj );

alert(obj.name);
alert(obj.child.name);
3
  • It would be great if you could add some explanation to the code you posted. This will help everybody (especially inexperienced users) to understand what the code does :-) Sep 28, 2012 at 10:55
  • This code just get the jsonObject and create a new object with $refs replaced by the right object reference. The usage is quite simple: var jsonCyclicReferenceFixed = JsonRecursive.parse(jsonWithRefAndId);
    – jvitor83
    Oct 1, 2012 at 21:11
  • 1
    Another topic: stackoverflow.com/questions/15312529/…
    – jvitor83
    Apr 15, 2013 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.