HDK
|
Class to store JSON objects as C++ objects. More...
#include <UT_JSONValue.h>
Classes | |
class | traverser |
Traverse the children of the current value. More... | |
Public Types | |
enum | Type { JSON_NULL, JSON_BOOL, JSON_INT, JSON_REAL, JSON_STRING, JSON_KEY, JSON_ARRAY, JSON_MAP } |
Class to store JSON objects as C++ objects.
This class is able to represent a JSON object in its entirety. Arrays and Maps store arrays and maps of JSON values.
There are methods to load a JSONValue from a UT_JSONParser and to save to a UT_JSONWriter object.
To load an entire JSON file into memory, you can do something like
To save a value:
Alternatively, it's possible to use the UT_JSONValue class from within other UT_JSONHandle classes. For example, when loading key/value pairs for an arbitrary map:
Definition at line 77 of file UT_JSONValue.h.
enum UT_JSONValue::Type |
Types held in the UT_JSONValue. The number type has been broken out into an integer and a real.
Enumerator | |
---|---|
JSON_NULL | |
JSON_BOOL | |
JSON_INT | |
JSON_REAL | |
JSON_STRING | |
JSON_KEY | |
JSON_ARRAY | |
JSON_MAP |
Definition at line 81 of file UT_JSONValue.h.
UT_JSONValue::UT_JSONValue | ( | ) |
|
explicit |
|
explicit |
|
explicit |
UT_JSONValue::UT_JSONValue | ( | const UT_StringHolder & | s | ) |
UT_JSONValue::UT_JSONValue | ( | const UT_JSONValue & | v | ) |
UT_JSONValue::~UT_JSONValue | ( | ) |
UT_JSONValueArray* UT_JSONValue::addArrayChild | ( | const UT_StringHolder & | map_key | ) |
Adds a new array child to this value. The current value must be either an array or a map. If this value is an array, the map_key parameter is ignored. This is due to overly convoluted JSON syntax design.
UT_JSONValueMap* UT_JSONValue::addMapChild | ( | const UT_StringHolder & | map_key | ) |
Adds a new map child to this value. The current value must be either an array or a map. If this value is an array, the map_key parameter is ignored. This is due to overly convoluted JSON syntax design.
bool UT_JSONValue::appendArray | ( | const UT_JSONValue & | v | ) |
Add an element to an array (returns false if operation fails)
bool UT_JSONValue::appendMap | ( | const UT_StringHolder & | key, |
const UT_JSONValue & | v | ||
) |
Add an element to a map (returns false if operation fails)
|
inline |
Definition at line 500 of file UT_JSONValue.h.
void UT_JSONValue::dump | ( | ) | const |
Dump the value to stdout (same as save to cout)
SYS_NO_DISCARD_RESULT UT_JSONValue* UT_JSONValue::findParentValueRecursive | ( | const UT_StringHolder & | map_key, |
const char * | key_value | ||
) |
SYS_NO_DISCARD_RESULT UT_JSONValue* UT_JSONValue::findValueRecursive | ( | const UT_StringHolder & | map_key, |
const char * | key_value | ||
) |
Searches all children of this value recursively for another value with a child that contains the given map key set to the given value.
SYS_NO_DISCARD_RESULT UT_JSONValueArray* UT_JSONValue::getArray | ( | ) | const |
Get the array value (may return a NULL pointer)
SYS_NO_DISCARD_RESULT bool UT_JSONValue::getB | ( | ) | const |
Get the bool value. Interprets integer/float as bool.
SYS_NO_DISCARD_RESULT fpreal64 UT_JSONValue::getF | ( | ) | const |
Get the real value. Interprets bool/int as reals.
SYS_NO_DISCARD_RESULT int64 UT_JSONValue::getI | ( | ) | const |
Get the integer value. Intereprets bool/real as integer
SYS_NO_DISCARD_RESULT const char* UT_JSONValue::getKey | ( | ) | const |
Get a key value.
SYS_NO_DISCARD_RESULT const UT_StringHolder* UT_JSONValue::getKeyHolder | ( | ) | const |
Return a string holder for the key.
SYS_NO_DISCARD_RESULT int64 UT_JSONValue::getKeyLength | ( | ) | const |
Get the length of the key.
SYS_NO_DISCARD_RESULT UT_JSONValueMap* UT_JSONValue::getMap | ( | ) | const |
Get the map value (may return a NULL pointer)
|
inline |
Used internally by UT_JSONValueMap.
Definition at line 309 of file UT_JSONValue.h.
SYS_NO_DISCARD_RESULT const char* UT_JSONValue::getS | ( | ) | const |
Get the string value (may return a NULL pointer)
SYS_NO_DISCARD_RESULT int64 UT_JSONValue::getSLength | ( | ) | const |
Return the string length (returns -1 if not a string)
|
inline |
Used by UT_JSONValue parsing from a stream.
Definition at line 316 of file UT_JSONValue.h.
|
inline |
Definition at line 318 of file UT_JSONValue.h.
SYS_NO_DISCARD_RESULT const UT_StringHolder* UT_JSONValue::getStringHolder | ( | ) | const |
Return the string value.
|
inline |
Get the type of data stored in the object.
Definition at line 134 of file UT_JSONValue.h.
SYS_NO_DISCARD_RESULT Type UT_JSONValue::getUniformArrayType | ( | ) | const |
Get the "uniform" type of an array. This will return:
SYS_NO_DISCARD_RESULT SYS_HashType UT_JSONValue::hash | ( | ) | const |
Compute a hash.
|
inline |
Extract a bool (returns false if type is invalid)
Definition at line 172 of file UT_JSONValue.h.
|
inline |
Extract an integer (returns false if type is invalid)
Definition at line 191 of file UT_JSONValue.h.
|
inline |
Extract an float (returns false if type is invalid)
Definition at line 210 of file UT_JSONValue.h.
bool UT_JSONValue::import | ( | UT_WorkBuffer & | result | ) | const |
Extract a string or key (returns false if type is invalid)
bool UT_JSONValue::import | ( | UT_StringHolder & | result | ) | const |
Extract a tuple of integers from an JSON_Array. If there aren't enough elements in the array, this method fails
Extract a tuple of floats from an JSON_Array. If there aren't enough elements in the array, this method fails
Extract a vector of integers from a JSON_Array.
Extract a vector of reals from a JSON_Array.
bool UT_JSONValue::import | ( | UT_StringArray & | array | ) | const |
Extract a vector of strings from a JSON_Array.
|
inline |
Returns whether the value can be interpreted as a number.
Definition at line 245 of file UT_JSONValue.h.
bool UT_JSONValue::loadFromFile | ( | const char * | file_name, |
bool | record_source_offsets = false |
||
) |
Loads an entire JSON file into this value.
|
inline |
Test equality
Definition at line 521 of file UT_JSONValue.h.
|
inline |
Definition at line 101 of file UT_JSONValue.h.
SYS_NO_DISCARD_RESULT bool UT_JSONValue::operator== | ( | const UT_JSONValue & | src | ) | const |
Test equality
bool UT_JSONValue::parseValue | ( | UT_JSONParser & | parser, |
UT_IStream * | is = 0 , |
||
bool | record_source_offsets = false |
||
) |
Read the next value from the parser and store it in this object
parser | The parser |
is | If specified, data will be read from this stream. Otherwise, the stream associated with the parser will be used. |
bool UT_JSONValue::parseValue | ( | const UT_StringRef & | json_source | ) |
void UT_JSONValue::removeNullValuesRecursive | ( | ) |
Removes NULL values from all arrays recursively. Note that maps are currently unsupported, and null values will not be removed from them.
bool UT_JSONValue::save | ( | UT_JSONWriter & | os | ) | const |
Save the object the output stream.
bool UT_JSONValue::saveToFile | ( | const char * | file_name, |
bool | binary = false , |
||
UT_CompressionType | compressionType = UT_COMPRESSION_TYPE_NONE |
||
) |
Saves an entire JSON value to the specified file.
void UT_JSONValue::setArray | ( | UT_JSONValueArray * | array | ) |
Set value to the array.
void UT_JSONValue::setBool | ( | bool | v | ) |
Set value to an bool.
Set string. If the length is not specified, the length of the string will be used.
Definition at line 267 of file UT_JSONValue.h.
|
inline |
Definition at line 269 of file UT_JSONValue.h.
void UT_JSONValue::setMap | ( | UT_JSONValueMap * | map | ) |
Set value to the map.
Used internally by UT_JSONValueMap.
Definition at line 312 of file UT_JSONValue.h.
|
inline |
Set value to a null.
Definition at line 252 of file UT_JSONValue.h.
Definition at line 320 of file UT_JSONValue.h.
Set string. If the length is not specified, the length of the string will be used.
Definition at line 261 of file UT_JSONValue.h.
|
inline |
Definition at line 263 of file UT_JSONValue.h.
Build a uniform array of values
Build a uniform array of values
Build a uniform array of values
Build a uniform array of values
Build a uniform array of values
bool UT_JSONValue::setUniformArray | ( | int | nvalues, |
const UT_StringHolder * | data | ||
) |
Build a uniform array of values
void UT_JSONValue::startArray | ( | ) |
Start building an array from scratch.
UT_JSONValueArray* UT_JSONValue::myArray |
Definition at line 538 of file UT_JSONValue.h.
bool UT_JSONValue::myBool |
Definition at line 531 of file UT_JSONValue.h.
int64 UT_JSONValue::myInt |
Definition at line 532 of file UT_JSONValue.h.
UT_JSONValueMap* UT_JSONValue::myMap |
Definition at line 539 of file UT_JSONValue.h.
fpreal64 UT_JSONValue::myReal |
Definition at line 533 of file UT_JSONValue.h.