|
BDE 4.39.x Production Release
|
Provide an in-memory representation of a JSON document.
Provide an in-memory representation of a JSON document.
This component provides a value-semantic type, bdljsn::Json, used as an in-memory representation for a JSON document. This component also provides bdljsn::JsonArray and bdljsn::JsonObject types for representing JSON Arrays and Objects respectively.
bdljsn::Json has a close structural similarity to the JSON grammar itself, which at a high-level looks like:
Where the Object and Array alternatives can recursively contain JSON. Just like this grammar, a bdljsn::Json is a variant holding either an Object, Array, String, Number, Boolean, or null. These variant selections are represented by the following types:
JsonObjectJsonArraybsl::stringJsonNumberboolJsonNullFor more details on the JSON grammar see:
bdljsn_jsonutil is the recommended facility to write and read bdljsn::Json objects to and from JSON document text.
operator<< overloads are available for all the types. In addition a canonical BDE print method is available for bdljsn::Json, bdljsn::JsonObject, bdljsn::JsonArray bdljsn::JsonNumber and bdljsn::JsonNull.
Every bdljsn::Json represents a (valid) JSON document (bdljsn::Json does not have an "invalid" state). This means writing a bdljsn::Json to a string can only fail if an out-of-memory condition occurs.
Similarly, every JSON document has a bdljsn::Json representation. However, bdljsn::JsonObject represents Objects having only unique member names, meaning that duplicate member names in a JSON document will be ignored (see bdljsn::JsonUtil for more information on how duplicate names are handled when parsing a JSON document). Note that the JSON RFC says that Object member names "SHOULD be unique", and there is no standard behavior for JSON parsers where member names are not unique (typically an in-process representation with unique member names is used).
In order to preserve the invariant that all bdljsn::Json objects are valid JSON documents there are some constructors and assignment operations in bdljsn package that have notable preconditions:
bdljsn::JsonNumber constructors from string require that the string conform to the JSON grammar for a number (see bdljsn::NumberUtil::isValidNumber).double or bdldfp::Decimal64 require the value not be either INF or NaNbdljsn::Json constructors and assignment operators require that strings contain valid UTF-8 (see bdlde::Utf8Util::isValid).bdljsn::Json type's definition of value (i.e., the behavior for operator==) mirrors comparing the text of two JSON documents where all the white-space is ignored.
Concretely, bdljsn::Json is a variant type, whose definition of equality is derived from the definition of equality of its constituent types. I.e., two bdljsn::Json objects compare equal if they have the same type and the two objects of that type they contain compare equal. The definition of equality for Object, Array, Boolean, string, and JsonNull types are relatively self-explanatory (see respective operator== definitions for details). The definition of equality for JsonNumber is notable: JsonNumber objects define value in terms of the text of the JSON number string they contain. So two JSON numbers having the same numerical value may compare unequal (e.g., "2" and "2.0" and "20e-1" are considered different JsonNumber values!). Note that bdljsn::JsonNumber::isEqual provides a semantic comparison of two numbers (see bdljsn_jsonnumber for more detail).
The bdljsn::JsonArray and bdljsn::JsonObject classes have constructors and assorted manipulator methods (e.g., assign, insert) that accept instances of std::initializer_list which are invoked when users supply braced lists of values. When values of the following types are used in those braced lists, the values are converted to bdljsn::Json objects (as the JsonArray elements or as the "value" portion of JsonObject members).
JsonNullboolfloatdoublebdldfp::Decimal64const char *bsl::stringstd::stringstd::pmr::string // where availablebsl::string_viewstd::string_view // where availableJsonObjectJsonArrayJsonNumberJsonThis section illustrates the intended use of this component.
Most often bdljsn::Json objects will be written and read from JSON text using bdljsn_jsonutil . In this simple example, we demonstrate manually creating the document below and then verify the properties of the resulting object:
First, we use bdljsn::Json::makeObject to configure the top level bdljsn::Json object to be a bdljsn::Json object, and use the various manipulators of bdljsn::JsonObject to configure its value:
Notice that we used operator[] to implicitly create new members of the top-level object. Using json.theObject().insert would be more efficient (see example 2).
Finally, we validate the properties of the resulting object:
Example 1 used operator[] to implicitly add members to the Objects. Using operator[] is intuitive but not the most efficient method to add new members to a bdljsn::JsonObject (similar to using operator[] to add elements to an unordered_map). The following code demonstrates a more efficient way to create the same bdljsn::Json representation as Example 1:
The Json class provides the (overloaded) visit method that invokes a user-supplied "visitor" functor according to the current type() of the Json object.
For example, suppose one needs to survey the structure of the Json object created in {Example 1} (and again in {Example 2}) and, in doing so, compile a tally of each of each of the Json sub-objects and their their types (i.e., object, array, string, ...).
First, we define a compliant visitor class, TallyByTypeVisitor:
Notice that we have no need to change the value of the examined Json objects so we use a set of operator() overloads compatible with the visit accessor method. Accordingly, we careful below to use this visitor only with const-qualified Json objects
Then, we define the constructor and the six operator() overloads. In each overload by type we increment the appropriate element in the user-supplied array of integers.
Next, we define the visitor overload for array types. After incrementing the tally array we pass this same visitor object to the Json object in the array so those objects are included in the tally.
Notice that element is const-qualified so the accessor visit method is invoked.
Then, we implement the visitor overload for the object type. Examination of the object produces a sequence of name-value pairs where the second part is a Json object that we must visit.
Again, notice that this visitor is used as an argument to a const-qualified Json object.
Finally, we make a survey of the Json object created in {Example 1} (and duplicated in {Example 2}). From visual inspection of the source JSON document we expect 10 Json objects distributed thus:
{"boolean": false }, interior object[2.76, true]"text"2.763.14false, from the internal objecttrue, from the arraytrue, from top-level objectnullUse of our visitor functor on example1 confirms these observations: