recursive stringify

143 views
Skip to first unread message

matt

unread,
Nov 17, 2009, 7:06:13 PM11/17/09
to JSON Schema
Hello,
Ive always been a huge supporter of Json, so i had this project where
i had to take a whole bunch of dynamically generated data and pass it
to a backend script.

I gathered the data, constructed a javascript object, and then went to
stringify it but i found that all of my children attributes had been
stripped out.

my javascript object is roughly the same as this (there are some
slight variations, but i think this is pretty close representation of
it):

{
"property1":"value",
"property2":"value",
"property3":"value",
"property4": {
"childProp1":{
"grandchildProp1":"value",
"grandchildProp2":"value",
"grandchildProp3":"value"
}
},
"property4": {
"childProp1":{
"grandchildProp1":"value",
"grandchildProp2":"value",
"grandchildProp3":"value"
}
}
}

The problem im having,
when i use any serialization library/api it shows property4/5 as empty
arrays. It completely drops all of the children and grandchildren.

I thought at first this was a common thing, but i have been scouring
google for hours, and have talked to everyone within my company who
knows anything about JSON.

This project is already overdue, and everything is being held up on
the simple fact that I cant stringify/serialize the JavaScript object.

Does anyone know why the children would be dropped? Is this the
standard behavior of stringify/serialize? Is it something wrong with
my object?

Matt

Seth Wessitsh

unread,
Nov 17, 2009, 7:59:34 PM11/17/09
to json-...@googlegroups.com
Hi Matt,

I only have a couple of guesses as to why its not working.  But it might be helpful to know what libraries you've tried and whether properties 4 and 5 are supposed to be arrays or objects.  In the example you've indicated that they are objects but mention that the output for them consists of empty arrays.

Since you've done so much to try and solve the problem you've probably already covered these but just in case.

My first guess would be that the libraries are optionally recursive and perhaps you've forgotten to pass an argument which tells them follow nodes.

Another possibility is that your properties 4 and 5 are inheriting their properties and the libraries only follow properties which belong to the object itself and not to a super object (via the hasOwnProperty method).

If neither of those is the case perhaps when you built your object properties 4 and 5 were supplied as references which were deleted/modified by the time you tried to stringify the resultant object.

Anyway, you could try the dojox.json.ref.toJson method in Dojo (docs) if you haven't yet which should do the trick unless the problem is outside the library itself.

Seth C. Wessitsh
se...@wessitsh.com



--

You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To post to this group, send email to json-...@googlegroups.com.
To unsubscribe from this group, send email to json-schema...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-schema?hl=.



Reply all
Reply to author
Forward
0 new messages