|
BDE 4.39.x Production Release
|
Basic Application Library JSoN (baljsn)
Provide components for encoding/decoding in the JSON format.
Basic Application Library JSoN (baljsn)
The baljsn package provides facilities for encoding and decoding value-semantic objects in the JSON encoding format. Currently, the encoder and decoder provided in this package work with types that support the bdeat framework (see the bdlat package for details), which is a compile-time interface for manipulating struct-like and union-like objects. Typical usage is illustrated in the baljsn_encoder and baljsn_decoder components documentation.
There are 2 sets of the encoding/decoding member functions:
encode & decode, andencodeAny & decodeAny (referred as "any-functions" below).The first set uses templates and compile-time metaprogramming to generate the encoding/decoding C++ code. The second set uses the runtime dispatch approach - base classes and virtual functions - to provide the same functionality. The result does not depend on the functions you use - their functionality is absolutely identical. The difference is not in what they do, but in how they do it.
As usual, the compile-time approach can be faster, but generates a lot of code. And since this code is in the headers, this causes big load on the compiler and linker - the generated code is in each translation unit and in the object file generated from it. As a result, slow compilation, huge memory and CPU consumption during the compilation and linking, and a larger resulting executable file size.
All the above mentioned shortcomings (except the final runtime performance) can be mitigated by using the "any-functions", at the cost of a small runtime slowdown. All the encoding/decoding code is pre-compiled and located in one place - the BDE library (archive) files. But how big is the execution slowdown? Our benchmarks show that the slowdown is less than 10%.
The 'baljsn' package currently has 20 components having 8 levels of physical dependency. The list below shows the hierarchical ordering of the components. The order of components within each level is not architecturally significant, just alphabetical.
baljsn_datumdecoderoptions : Provide options for decoding JSON into a Datum object.
baljsn_datumencoderoptions : Provide an attribute class for specifying Datum<->JSON options.
baljsn_datumutil : Provide utilities converting between bdld::Datum and JSON data.
baljsn_decoder : Provide a JSON decoder for bdeat compatible types.
baljsn_decoderoptions : Provide an attribute class for specifying JSON decoding options.
baljsn_decoderoptionsutil : Provide a utility for configuring baljsn::DecoderOptions.
baljsn_encodeimplutil : Provide a utility to encode bdlat-compatible types as JSON.
baljsn_encoder : Provide a JSON encoder for bdlat-compatible types.
baljsn_encoder_testtypes : !PRIVATE! Provide value-semantic attribute classes
baljsn_encoderoptions : Provide an attribute class for specifying JSON encoding options.
baljsn_encodingstyle : Provide value-semantic attribute classes.
baljsn_formatter : Provide a formatter for encoding data in the JSON format.
baljsn_jsonconverter : Provide conversions between JSON and bdlat-compatible types.
baljsn_jsonformatter : Provide a formatter for converting bdlat object to Json analog.
baljsn_jsonparserutil : Provide a utility to get simple types from bdl::json objects
baljsn_jsontokenizer : Provide a tokenizer for viewing parts of a bdljsn::Json object.
baljsn_parserutil : Provide a utility for decoding JSON data into simple types.
baljsn_printutil : Provide a utility for encoding simple types in the JSON format.
baljsn_simpleformatter : Provide a simple formatter for encoding data in the JSON format.
baljsn_tokenizer : Provide a tokenizer for extracting JSON data from a streambuf.
The following table provides a mapping between an element's 'bdem' elem type (as specified in bdem_elemtype ), its XSD type, its C++ type, its corresponding JSON type, and the encoding used.
The format grammar specified below uses the Extended BNF notation (except that ',' is not used for concatenation to enhance readability). A quick reference of EBNF is provided below (refer here for more details):
(1) Double types (float, decimal, and double) are encoded in the number format by default with the values NaN, +INF and -INF resulting in an encoding error. These values can be printed as strings by setting the 'encodeInfAndNaNAsStrings' encoder option to 'true'.
(2) In practice only the timezone-enabled components are used by bcem_Aggregate and generated types. The supported format is a subset of the ISO 8601 standard. For further details refer to the bdepu_iso8601 component.
(3) Null values are not encoded on the wire by default. The 'encodeNullElements' options can be set to 'true' to ensure that null values are encoded.
(4) The name of an element corresponds to that element's name in the provided xsd.
The 'baljsn::DecoderOption' parameter of the 'decode' function has a configuration option named 'validateInputIsUtf8'. If this option is 'true', the 'decode' function will succeed only if the encoding of the JSON data is UTF-8, which the JSON specification requires. If the option is 'false', 'decode' will not validate that the encoding of the JSON data is UTF-8, and may succeed even if the data does not satisfy the UTF-8 validity requirement of the JSON specification. This option primarily affects the acceptance of string literals, which are the parts of JSON documents that may have rational justification for having non-UTF-8, and therefore invalid, content.
Ideally, users should set 'validateInputIsUtf8' to 'true'. However, some legacy applications currently might be trafficking in JSON that contains non-UTF-8 with no adverse effects to their clients. Consequently, this option is 'false' by default to maintain backward compatibility.