Provide a utility for decoding JSON data into simple types.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a utility for decoding JSON data into simple types.
-
- Classes:
-
- See also:
- Component baljsn_decoder, Component 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: Then, we create an Employee
object: 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);
Now, we use the created string refs to populate the employee object: 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);