BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baljsn_tokenizer

Detailed Description

Outline

Purpose

Provide a tokenizer for extracting JSON data from a streambuf.

Classes

See also
baljsn_decoder, baljsn_parserutil

Description

This component provides a class, baljsn::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 baljsn package and in most cases clients should use the baljsn_decoder component 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.

Usage

This section illustrates intended use of this component.

Example 1: Extracting JSON Data into an Object

For this example, we will use baljsn::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:

const char *INPUT = " {\n"
" \"street\" : \"Lexington Ave\",\n"
" \"state\" : \"New York\",\n"
" \"zipcode\" : 10022\n"
" }";

Next, we will construct populate a streambuf with this data:

bdlsb::FixedMemInStreamBuf isb(INPUT, bsl::strlen(INPUT));
Definition bdlsb_fixedmeminstreambuf.h:187

Then, we will create a baljsn::Tokenizer object and associate the above streambuf with it:

tokenizer.reset(&isb);
Definition bdljsn_tokenizer.h:234
void reset(bsl::streambuf *streambuf)
Definition bdljsn_tokenizer.h:753

Next, we will create an address record type and object.

struct Address {
bsl::string d_street;
bsl::string d_state;
int d_zipcode;
} address = { "", "", 0 };
Definition bslstl_string.h:1281

Then, we will traverse the JSON data one node at a time:

// Read '{'
int rc = tokenizer.advanceToNextToken();
assert(!rc);
rc = tokenizer.advanceToNextToken();
assert(!rc);
token = tokenizer.tokenType();
// Continue reading elements till '}' is encountered
// Read element name
bslstl::StringRef nodeValue;
rc = tokenizer.value(&nodeValue);
assert(!rc);
bsl::string elementName = nodeValue;
// Read element value
int rc = tokenizer.advanceToNextToken();
assert(!rc);
token = tokenizer.tokenType();
rc = tokenizer.value(&nodeValue);
assert(!rc);
// Extract the simple type with the data
if (elementName == "street") {
rc = baljsn::ParserUtil::getValue(&address.d_street, nodeValue);
assert(!rc);
}
else if (elementName == "state") {
rc = baljsn::ParserUtil::getValue(&address.d_state, nodeValue);
assert(!rc);
}
else if (elementName == "zipcode") {
rc = baljsn::ParserUtil::getValue(&address.d_zipcode, nodeValue);
assert(!rc);
}
rc = tokenizer.advanceToNextToken();
assert(!rc);
token = tokenizer.tokenType();
}
TokenType tokenType() const
Return the token type of the current token.
Definition bdljsn_tokenizer.h:925
TokenType
Definition bdljsn_tokenizer.h:241
@ e_ELEMENT_NAME
Definition bdljsn_tokenizer.h:245
@ e_END_OBJECT
Definition bdljsn_tokenizer.h:247
@ e_ELEMENT_VALUE
Definition bdljsn_tokenizer.h:250
@ e_START_OBJECT
Definition bdljsn_tokenizer.h:246
int value(bsl::string_view *data) const
Definition bslstl_stringref.h:372
static int getValue(bool *value, const bsl::string_view &data)

Finally, we will verify that the address aggregate has the correct values:

assert("Lexington Ave" == address.d_street);
assert("New York" == address.d_state);
assert(10022 == address.d_zipcode);