From: Stephan B. <sg...@go...> - 2011-05-25 15:11:04
|
On Wed, May 25, 2011 at 4:56 PM, Baptiste Lepilleur < bap...@gm...> wrote: > >> This is that feature that allows the following usage: > > Json::Value array; > array.append( 1234 ); > That means a null value is effectively mutable, and only arrays/objects are mutable in JS (JSON doesn't describe mutability, and it has no operators which would allow mutation). > Maybe we should introduce a distinction between "default" initialization > and "null", but I'm not sure how to go about that... > Absolutely. In the 2 JSON libs i've implemented (one in C++, one in C), the following values all have a corresponding static/constant Value instance (mainly as a memory allocation optimization): true false null "" (empty string) undefined (not used by JSON, but as a placeholder for default-initialized values) double 0.0 integer 0 (the last two are internal optimizations, and not generally all that useful) A default-constructed Value has the undefined value: Value v; // v.isUndef() will return true v = Object(); // now v.isObject() will return true If someone wants a Null value, they get a reference to the shared instance: Value v = Value::Null(); This requires no allocation (the object instances are static/const), and because values are all immutable (except array/object, of course), this is safe in terms of "nobody can change the value of Null by assigning over a reference to it." i don't know if the jsoncpp code is structured such that such constant values would be immediately implementable, though. -- ----- stephan beal http://wanderinghorse.net/home/stephan/ |