3

Have I any chance to serialize meta (any format, so I can store it in DB)?

var obj1 = {};
var obj2 = {};
obj1.link = obj2;
obj2.link = obj1;
var meta = [obj1, obj2];

As I understand the problem is that JSON serialize object`s links to objects.

asked May 6, 2010 at 15:03

3 Answers 3

2

Yes. You'll need to give your objects some kind of ID and use that as a reference.

var obj1 = {id: "obj1"};
var obj2 = {id: "obj2"};
obj1.link = "obj2";
obj2.link = "obj1";
var meta = [obj1, obj2];
answered May 6, 2010 at 15:06
Sign up to request clarification or add additional context in comments.

Comments

1

One approach is to use an object as the outermost container, using the keys of the object as the ids:

var objs = { 
 obj1: { link: "obj2" }, 
 obj2: { link: "obj1" } 
}

Then you can follow the links with just a property lookup:

var o1 = objs["obj1"];
var o2 = objs[o1.link];

And this converts to JSON without needing any conversions

answered May 6, 2010 at 15:17

Comments

1

JSON serializing such links can easily be avoided by using the replacer function:

var a = {}, b = {};
var d = {
 a: a, 
 b: b, 
 c: "c"
};
JSON.stringify(d, function(key, value) {
 if (value === a || value === b) {
 return;
 }
 return value;
});
// returns '{"c":"c"}'
answered May 6, 2010 at 15:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.