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

Detailed Description

Outline

Purpose

Provide a utility for decoding JSON data into simple types.

Classes

See also
baljsn_decoder, baljsn_printutil

Description

This component provides a struct of utility functions, baljsn::ParserUtil, for decoding data in the JSON format into a bdeat Simple type. The primary method is getValue, which decodes into a specified object and is overloaded for all bdeat Simple types.

Refer to the details of the JSON encoding format supported by this utility in the package documentation file (doc/baljsn.txt).

Usage

This section illustrates intended use of this component.

Example 1: Decoding into a Simple struct from JSON data

Suppose we want to de-serialize some JSON data into an object.

First, we define a struct, Employee, to contain the data:

struct Employee {
bsl::string d_name;
bdlt::Date d_date;
int d_age;
};
Definition bdlt_date.h:294
Definition bslstl_string.h:1281

Then, we create an Employee object:

Employee employee;

Next, we specify the string values in JSON format used to represent the object data. Note that the birth date is specified in the ISO 8601 format:

const char *name = "\"John Smith\"";
const char *date = "\"1985-06-24\"";
const char *age = "21";
const bsl::string_view nameRef(name);
const bsl::string_view dateRef(date);
const bsl::string_view ageRef(age);
Definition bslstl_stringview.h:441

Now, we use the created string refs to populate the employee object:

assert(0 == baljsn::ParserUtil::getValue(&employee.d_name, nameRef));
assert(0 == baljsn::ParserUtil::getValue(&employee.d_date, dateRef));
assert(0 == baljsn::ParserUtil::getValue(&employee.d_age, ageRef));
static int getValue(bool *value, const bsl::string_view &data)

Finally, we will verify that the values are as expected:

assert("John Smith" == employee.d_name);
assert(bdlt::Date(1985, 06, 24) == employee.d_date);
assert(21 == employee.d_age);