BDE 4.14.0 Production release
|
Provide a JSON decoder for bdeat
compatible types.
bdeat
-compliant typesThis component provides a class, baljsn::Decoder
, for decoding value-semantic objects in the JSON format. In particular, the class
contains a parameterized decode
function that decodes an object from a specified stream. There are two overloaded versions of this function:
bsl::streambuf
bsl::istream
This component can be used with types that support the bdeat
framework (see the bdeat
package for details), which is a compile-time interface for manipulating struct-like and union-like objects. In particular, types generated by the bas_codegen.pl
tool, and other dynamic types, can be decoded using this class
. The decode
function can be invoked on any object that satisfies the requirements of a sequence, choice, or array object as defined in the bdlat_sequencefunctions , bdlat_choicefunctions , and bdlat_arrayfunctions components.
Although the JSON format is easy to read and write and is very useful for debugging, it is relatively expensive to encode and decode and relatively bulky to transmit. It is more efficient to use a binary encoding (such as BER) if the encoding format is under your control (see balber_berdecoder ).
Refer to the details of the JSON encoding format supported by this decoder in the package documentation file (doc/baljsn.txt).
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.
The baljsn::Decoder
class allows several convenient variances from the JSON grammar as described in RFC8259 (see https://www.rfc-editor.org/rfc/rfc8259). If strict conformance is needed, users should use the read
overloads that accept a baljsn::DecoderOptions
object and set the following attributes to the values shown below:
See also {bdljsn_tokenizer |Strict Conformance}.
This section illustrates intended use of this component.
Consider that we want to exchange an employee's information between two processes. To allow this information exchange we will define the XML schema representation for that class, use bas_codegen.pl
to create the Employee
class
for storing that information, and decode into that object using the baljsn
decoder.
First, we will define the XML schema inside a file called employee.xsd
:
Then, we will use the bas_codegen.pl
tool, to generate the C++ classes for this schema. The following command will generate the header and implementation files for the all the classes in the test_messages components in the current directory:
Next, we will create a test::Employee
object:
Then, we will create a baljsn::Decoder
object:
Next, we will specify the input data provided to the decoder:
Now, we will decode this object using the decode
function of the baljsn decoder by providing it a baljsn::DecoderOptions
object. The decoder options allow us to specify that unknown elements should not be skipped. Setting this option to false
will result in the decoder returning an error on encountering an unknown element:
Finally, we will verify that the decoded object is as expected: