Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component baljsn_parserutil
[Package baljsn]

Provide a utility for decoding JSON data into simple types. More...

Namespaces

namespace  baljsn

Detailed Description

Outline
Purpose:
Provide a utility for decoding JSON data into simple types.
Classes:
baljsn::ParserUtil utility for parsing JSON data into simple types
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:
  struct Employee {
      bsl::string d_name;
      bdlt::Date   d_date;
      int         d_age;
  };
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);
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));
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);