[Qt-interest] serialize QScriptValue to json?
Nathan Carter
nathancarter5 at gmail.com
Thu Apr 9 03:44:09 CEST 2009
On Apr 8, 2009, at 4:50 PM, Liam Staskawicz wrote:
> Hi - I've just been getting my feet wet with QtScript and I've
> successfully been parsing JSON into QScriptValues via
> QScriptEngine::evaluate(). I haven't found yet if there's a built-in
> or recommended way to serialize a QScriptValue back to JSON.
>
> Is the idea to just create a routine that iterates over all the
> properties of a QScriptValue and builds up a JSON string? Any other
> tips? Thanks a bunch.
You have to be very careful with this. Consider:
a = [];
a.push( a );
Now your iteration goes forever.
This, incidentally, also puts the built-in print() function into an
infinite loop, so if your standards are not that high, you can just
assume the caller has to avoid passing your routines these annoying
structures.
But this is a tiny example that may seem unrealistic, while in
reality, such structures can certainly arise. Consider a tree
structure where pointers to children and parents are kept; then maybe
( a.firstChild == b ) && ( b.parent == a ). Forever again.
Just something to keep in mind. I've found this problem tricky, but
not unsolvable. Although I wasn't doing JSON, but traversing object
trees for another reason.
Nathan
More information about the Qt-interest-old
mailing list