Skip to content
Evan Teran edited this page Jan 30, 2023 · 3 revisions

API

Parsing Interface

The following function takes an input strings, parses the contents and returns the result as a json::value. If a parsing error occurs, then a subclass of json::exception is thrown.

  • json::value json::parse(std::string_view s);

Type Testing

The following functions will return true if the passed object matches the type. Otherwise false

  • bool json::is_string(const value &v);
  • bool json::is_bool(const value &v);
  • bool json::is_number(const value &v);
  • bool json::is_object(const value &v);
  • bool json::is_array(const value &v);
  • bool json::is_null(const value &v);

Conversion

The following functions will return, by copy the json::value converted to the requested type. If conversion is not possible. json::invalid_type_cast is thrown.

  • std::string json::to_string(const value &v);
  • bool json::to_bool(const value &v);
  • double json::to_number(const value &v);
  • object json::to_object(const value &v);
  • array json::to_array(const value &v);

Interpretation

The following functions will return, by reference the json::value converted to the requested type. If conversion is not possible. json::invalid_type_cast is thrown. No copies are created. For non-const versions you may alter the contents of the json::value

  • object &json::as_object(value &v);
  • array &json::as_array(value &v);
  • std::string &json::as_string(value &v);
  • const object &json::as_object(const value &v);
  • const array &json::as_array(const value &v);
  • const std::string &json::as_string(const value &v);

Object Inspection

The following functions will return true if the given json::object has the specified key. Otherwise false is returns. The version which accepts a json::value is a convenience function which will return false if the provided json::value is not convertible to a json::object

  • bool json::has_key(const value &v, const std::string &key);
  • bool json::has_key(const object &o, const std::string &key);

Printing

The following functions are used to convert any json::value, json::array, or json::object to a string representing valid JSON. Optionally, you can pass one or more options (combined with bitwise OR). By default, when no options are provided, the output has no newlines or unnecessary spaces.

  • std::string stringify(const value &v, unsigned options);
  • std::string stringify(const array &a, unsigned options);
  • std::string stringify(const object &o, unsigned options);
  • std::string stringify(const value &v);
  • std::string stringify(const array &a);
  • std::string stringify(const object &o);

Printing Options

  • EscapeUnicode, when printing non-ascii codepoints, print them as unicode escapes instead of UTF-8 literals.

  • PrettyPrint, output will be formatting suitable for humans to read.