Primitive types problems with var and JSON

This is what I do:

var aTrueVar (true);

String stringValue (JSON::toString (aTrueVar));

var anotherTrueVar = JSON::parse (stringValue);

at this point, I expect anotherTrueVar to be "true", but it is "null" instead

I found the reason for this in the fact that all the parse(...) methods expect to be feeded with either an array (starting with '[') or an object (starting with '{'), but don't take in account the fact that the passed string could be a primitive value instead (a number, a true/false value or a proper string instead).

I thought that a more suitable implementation for those "parse" methods would have been along the lines of the (currently private) method parseAny. Is there a reason why this isn't so and, if there is, how could I serialize and deserialize my var's to and from strings regardless of the fact that they could be primitive values or arrays?

 

TL;DR: the JSON::parse() method isn't the reciprocal of the JSON::toString() method when it comes to primitive type vars. Could you please make it so, or provide a workaround?

Well, it's done like that deliberately because valid JSON must be either an object or array. E.g. go to this site http://jsonlint.com and type in "true" and it'll fail you.

I guess it's a valid FR to ask for another parse method that does permit primitives, but the existing code isn't wrong.

Yes, I understand that the parse() methods only expect whole JSON documents, and thus at the top level only "array" or "objects" are valid (using the terminologly at www.json.org), but I think that, since the toString() method also outputs "values", which are a generalization that recursively includes "arrays", "objects" and the other primitive values, the addition of a parseValue() method that parses them all (and thus that can "revert" the output of toString() back to its original var) is a good addition, otherwise there's no elegant way around this (unless I missed some obvious one).

 

Yup. I'll add a method for that.