C++ TargetRTS
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Friends | List of all members
RTJsonResult Class Reference

Represents the result of parsing a JSON string. More...

#include <RTJsonResult.h>

Public Types

enum  RTJsonType {
  RTJSON_NULL , RTJSON_OBJ , RTJSON_ARR , RTJSON_STR ,
  RTJSON_NUM , RTJSON_BOOL
}
 The type of JSON value represented by the RTJsonResult object. More...
 

Public Member Functions

const RTJsonResultoperator[] (const std::string &key) const
 For a JSON object, return the value for the key with the given name.
 
const RTJsonResultoperator[] (size_t index) const
 For a JSON array, return the value at the given index.
 
size_t get_size () const
 For a JSON array, return the number of array elements.
 
std::map< std::string, RTJsonResult >::const_iterator keys_begin () const
 For a JSON object, return its keys begin iterator.
 
std::map< std::string, RTJsonResult >::const_iterator keys_end () const
 For a JSON object, return its keys end iterator.
 
RTJsonResultoperator= (const RTJsonResult &other)
 Assignment operator.
 
 RTJsonResult (const RTJsonResult &other)
 Copy constructor.
 
bool ok () const
 Returns false if this object represents an error result, true otherwise.
 
const std::string & get_string () const
 Get a string representation of the JSON value.
 
RTJsonType get_type () const
 Get the type of JSON value represented by the RTJsonResult.
 
bool get_bool () const
 Interpret the JSON value as a bool.
 
int get_int () const
 Interpret the JSON value as an int.
 
float get_float () const
 Interpret the JSON value as a float.
 
double get_double () const
 Interpret the JSON value as a double.
 
bool operator== (const RTJsonResult &) const
 Determines if the JSON value is equal (but not necessarily the same) as another JSON value.
 
bool operator== (const bool) const
 Determines if the JSON value is boolean and equal to the given bool.
 
bool operator== (const int) const
 Determines if the JSON value is numeric and equal to the given int.
 
bool operator== (const float) const
 Determines if the JSON value is numeric and equal to the given float.
 
bool operator== (const double) const
 Determines if the JSON value is numeric and equal to the given double.
 
bool operator== (const char *) const
 Determines if the JSON value is a string and equal to the given string.
 

Static Public Attributes

static const RTJsonResult ERROR
 This special object represents an error result.
 

Friends

class RTJsonParser
 
class RTJsonParserUtils
 

Detailed Description

Represents the result of parsing a JSON string.

The result from parsing is a tree of RTJsonResult objects where the root is representing a JSON object or array. Leaf objects of the tree are RTJsonResult objects that represent JSON values.

See also
RTJsonParser

Member Enumeration Documentation

◆ RTJsonType

The type of JSON value represented by the RTJsonResult object.

Enumerator
RTJSON_NULL 

The special null value.

RTJSON_OBJ 

JSON object value.

RTJSON_ARR 

JSON array value.

RTJSON_STR 

JSON string value.

RTJSON_NUM 

JSON numeric value.

RTJSON_BOOL 

JSON boolean value.

Member Function Documentation

◆ get_bool()

bool RTJsonResult::get_bool ( ) const

Interpret the JSON value as a bool.

Only call this for JSON boolean values.

◆ get_double()

double RTJsonResult::get_double ( ) const

Interpret the JSON value as a double.

Only call this for JSON numeric values.

Returns
JSON value as double, or 0 if it cannot be represented as a double

◆ get_float()

float RTJsonResult::get_float ( ) const

Interpret the JSON value as a float.

Only call this for JSON numeric values.

Returns
JSON value as float, or 0 if it cannot be represented as a float

◆ get_int()

int RTJsonResult::get_int ( ) const

Interpret the JSON value as an int.

Only call this for JSON numeric values.

Returns
JSON value as int, or 0 if it cannot be represented as an int

◆ get_size()

size_t RTJsonResult::get_size ( ) const

For a JSON array, return the number of array elements.

For a JSON object, return the number of keys. For other JSON values, return 0.

◆ get_string()

const std::string & RTJsonResult::get_string ( ) const

Get a string representation of the JSON value.

Note that all values have a string representation except arrays and objects for which an empty string is returned.

◆ operator[]() [1/2]

const RTJsonResult & RTJsonResult::operator[] ( const std::string &  key) const

For a JSON object, return the value for the key with the given name.

If the RTJsonResult does not represent a JSON object, or a key with the specified name does not exist, the ERROR result is returned.

Parameters
[in]objectkey name
Returns
value for object key if successful, otherwise the ERROR result

◆ operator[]() [2/2]

const RTJsonResult & RTJsonResult::operator[] ( size_t  index) const

For a JSON array, return the value at the given index.

For a JSON object, return the value for the key at the given index. Since the order of keys is not significant in JSON it's not recommended to call this function for a JSON object. In other cases the ERROR result is returned.

Parameters
[in]indexof value to obtain
Returns
value at array or key index if successful, otherwise the ERROR result

The documentation for this class was generated from the following files: