Wt: JSON Library (Wt::Json)

Wt 4.10.4

Loading...
Searching...
No Matches
Classes | Enumerations | Functions
JSON Library (Wt::Json)

A JSON representation and parsing library. More...

## Classes
class | Wt::Json::Array
| A JSON array. More...
class | Wt::Json::Object
| A JSON object. More...
class | Wt::Json::ParseError
| A parse error. More...
class | Wt::Json::Value
| A JSON value. More...
class | Wt::Json::TypeException
| Exception that indicates a type error. More...

## Enumerations
enum class | Wt::Json::Type { Wt::Json::Type::Null , Wt::Json::Type::String , Wt::Json::Type::Bool , Wt::Json::Type::Number , Wt::Json::Type::Object , Wt::Json::Type::Array }
| Enumeration for the type of a JSON value. More...

## Functions
WT_API void | Wt::Json::parse (const std::string &input, Value &result, bool validateUTF8=true)
| Parse function.
WT_API bool | Wt::Json::parse (const std::string &input, Value &result, ParseError &error, bool validateUTF8=true)
| Parse function.
WT_API void | Wt::Json::parse (const std::string &input, Object &result, bool validateUTF8=true)
| Parse function.
WT_API bool | Wt::Json::parse (const std::string &input, Object &result, ParseError &error, bool validateUTF8=true)
| Parse function.
WT_API void | Wt::Json::parse (const std::string &input, Array &result, bool validateUTF8=true)
| Parse function.
WT_API bool | Wt::Json::parse (const std::string &input, Array &result, ParseError &error, bool validateUTF8=true)
| Parse function.
std::string WT_API | Wt::Json::serialize (const Object &obj, int indentation=1)
| Serialization function for an Object.
std::string WT_API | Wt::Json::serialize (const Array &arr, int indentation=1)
| Serialization function for an Array.

## Detailed Description

A JSON representation and parsing library.

The JSON library contains data types to represent a JSON data structure (Value, Object and Array), a JSON parser, and a JSON serializer.

Usage example:

Json::Object result;
Json::parse("{ "
" \"a\": \"That's great\", "
" \"b\": true "
"}",
result);
std::cerr << "Size: " << result.size(); << std::endl; // Size: 2
WString s = result.get("a");
bool b = result.get("b");
std::cerr << "a: " << s << ", b: " << b << std::endl; // a: That's great, b: true
Wt::Json::parse
WT_API void parse(const std::string &input, Value &result, bool validateUTF8=true)
Parse function.
Wt::Json::Type::Object
@ Object
an Object

Include the <Wt/Json/Parser.h> header for Wt::Json::parse(), and the <Wt/Json/Serializer.h> header for Wt::Json::serialize().

## Enumeration Type Documentation

## ◆ Type

enum class Wt::Json::Type
| strong

Enumeration for the type of a JSON value.

See also
  Value::type()

Enumerator
Null | "null" or missing value
String | a (unicode) string
Bool | "true" or "false"
Number | a number (integer or floating point)
Object | an Object
Array | an Array

## Function Documentation

## ◆ parse() [1/6]

WT_API void Wt::Json::parse | ( | const std::string & | input,
|  | Array & | result,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

Exceptions

  ParseError | when the input is not a correct JSON structure.
  TypeException | when the JSON structure does not represent an Object.

## ◆ parse() [2/6]

WT_API bool Wt::Json::parse | ( | const std::string & | input,
|  | Array & | result,
|  | ParseError & | error,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` value. On success, the result value contains either an Array or Object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

This method returns `true` if the parse was succesful, or reports an error in into the `error` value otherwise.

## ◆ parse() [3/6]

WT_API void Wt::Json::parse | ( | const std::string & | input,
|  | Object & | result,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

Exceptions

  ParseError | when the input is not a correct JSON structure.
  TypeException | when the JSON structure does not represent an Object.

## ◆ parse() [4/6]

WT_API bool Wt::Json::parse | ( | const std::string & | input,
|  | Object & | result,
|  | ParseError & | error,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` value. On success, the result value contains either an Array or Object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

This method returns `true` if the parse was succesful, or reports an error in into the `error` value otherwise.

## ◆ parse() [5/6]

WT_API void Wt::Json::parse | ( | const std::string & | input,
|  | Value & | result,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` value. On success, the result value contains either an Array or Object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

Exceptions

  ParseError | when the input is not a correct JSON structure.

## ◆ parse() [6/6]

WT_API bool Wt::Json::parse | ( | const std::string & | input,
|  | Value & | result,
|  | ParseError & | error,
|  | bool | validateUTF8 = true )
| extern

Parse function.

This function parses the input string (which represents a UTF-8 JSON-encoded data structure) into the `result` value. On success, the result value contains either an Array or Object.

If validateUTF8 is true, the parser will sanitize (security scan for invalid UTF-8) the UTF-8 input string before parsing starts.

This method returns `true` if the parse was succesful, or reports an error in into the `error` value otherwise.

## ◆ serialize() [1/2]

std::string WT_API Wt::Json::serialize | ( | const Array & | arr,
|  | int | indentation = 1 )

Serialization function for an Array.

Serializes a Array into a string. All unicode in the object is UTF-8 encoded in the output. The output is indented to make it readable. The indentation argument to this function is used in recursive calls and should not be set.

Exceptions

  WException | when the trying to serialize a number which is a Nan

## ◆ serialize() [2/2]

std::string WT_API Wt::Json::serialize | ( | const Object & | obj,
|  | int | indentation = 1 )

Serialization function for an Object.

Serializes a Object into a string. All unicode in the object is UTF-8 encoded in the output. The output is indented to make it readable. The indentation argument to this function is used in recursive calls and should not be set.

Exceptions

  WException | when the trying to serialize a number which is a Nan