BDE 4.39.x Production Release
Loading...
Searching...
No Matches
baljsn_jsonparserutil

Detailed Description

Provide a utility to get simple types from bdl::json objects.

Outline

Purpose

Provide a utility to get simple types from bdl::json objects

Classes

See also
baljsn_parserutil

Description

This component provides a struct of utility functions, baljsn::JsonParserUtil, for extracting data from bdljsn::Json objects format into a bdlat Simple type. The primary method is getValue which is is overloaded for all bdlat Simple types.

Usage

This section illustrates intended use of this component.

Example 1: Decoding into a Simple struct from bdljsn::Json Objects

Suppose we want extract some data from bdljsn::Json objects.

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:1252

Then, we create an Employee object:

Employee employee;

Next, we create bdljsn::Json objects of different (dynameic) types having the data of interest. Note that the string data does not have embedden double quote deliminters as required in JSON documents. Also note that the date information is represented in ISO 8601 format in a string.

const char *nameStr = "John Smith"; // No double quotes *in* string.
const char *dateStr = "1985-06-24"; // No double quotes *in* string.
bdljsn::Json name; name.makeString(nameStr);
bdljsn::Json date; date.makeString(dateStr); // ISO 8601
Definition bdljsn_jsonnumber.h:412
Definition bdljsn_json.h:1461
JsonNumber & makeNumber()
Definition bdljsn_json.h:4511
void makeString(const char *string)
Definition bdljsn_json.h:4551

Now, we use bdljsn::Json objects to populate the employee object:

assert(0 == baljsn::JsonParserUtil::getValue(&employee.d_name, name));
assert(0 == baljsn::JsonParserUtil::getValue(&employee.d_date, date));
assert(0 == baljsn::JsonParserUtil::getValue(&employee.d_age, age ));
static int getValue(bool *value, const bdljsn::Json &json)
Definition baljsn_jsonparserutil.h:238

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);