BDE 4.14.0 Production release
|
Provide a tokenizer for extracting JSON data from a streambuf
.
streambuf
This component provides a class, bdljsn::Tokenizer
, that traverses data stored in a bsl::streambuf
one node at a time and provides clients access to the data associated with that node, including its type and data value. Client code can use the reset
function to associate a bsl::streambuf
containing JSON data with a tokenizer object and then call the advanceToNextToken
function to extract individual data values.
This class
was created to be used by other components in the bdljsn
and baljsn
packages and in most cases clients should use the bdljsn_jsonutil , baljsn_decoder , or bdljsn_datumutil components instead of using this class
.
On malformed JSON, tokenization may fail before the end of input is reached, but not all such errors are detected. In particular, callers should check that closing brackets and braces match opening ones.
The bdljsn::Tokenizer
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 can put the tokenizer into strict conformance mode (see setConformanceMode
). The behavioral differences are each controlled by options. The differences between a default constructed tokenizer and one in strict mode are:
The default-constructed bdljsn::Tokenizer
is created having the options shown above (in the"Default" column) and a conformancemode
of bdljsn::e_RELAXED
. Accordingly, users are free to change any of the option values to any combination that may be needed; however, once a tokenizer is set to strict mode the options are set to the values shown above (in the "Strict" column) and changes are not allowed (doing so leads to undefined behavior) unless the conformance mode is again set to relaxed.
This section illustrates intended use of this component.
For this example, we will use bdljsn::Tokenizer
to read each node in a JSON document and populate a simple Employee
object.
First, we will define the JSON data that the tokenizer will traverse over:
Next, we will construct populate a streambuf
with this data:
Then, we will create a bdljsn::Tokenizer
object and associate the above streambuf with it:
Next, we will create an address record type and object.
Then, we will traverse the JSON data one node at a time:
Finally, we will verify that the address
aggregate has the correct values: