HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_JSONValue Class Reference

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
}
 

Public Member Functions

 UT_JSONValue ()
 
 UT_JSONValue (bool value)
 
 UT_JSONValue (int64 value)
 
 UT_JSONValue (fpreal64 value)
 
 UT_JSONValue (const char *string, int64 length=-1)
 
 UT_JSONValue (const UT_StringHolder &s)
 
 UT_JSONValue (const UT_JSONValue &v)
 
 ~UT_JSONValue ()
 
UT_JSONValueoperator= (const UT_JSONValue &v)
 
bool parseValue (UT_JSONParser &parser, UT_IStream *is=0, bool record_source_offsets=false)
 
bool parseValue (const UT_StringRef &json_source)
 
bool loadFromFile (const char *file_name, bool record_source_offsets=false)
 Loads an entire JSON file into this value. More...
 
bool saveToFile (const char *file_name, bool binary=false, UT_CompressionType compressionType=UT_COMPRESSION_TYPE_NONE)
 Saves an entire JSON value to the specified file. More...
 
bool save (UT_JSONWriter &os) const
 Save the object the output stream. More...
 
void dump () const
 Dump the value to stdout (same as save to cout) More...
 
SYS_NO_DISCARD_RESULT Type getType () const
 Get the type of data stored in the object. More...
 
SYS_NO_DISCARD_RESULT bool getB () const
 Get the bool value. Interprets integer/float as bool. More...
 
SYS_NO_DISCARD_RESULT int64 getI () const
 
SYS_NO_DISCARD_RESULT fpreal64 getF () const
 Get the real value. Interprets bool/int as reals. More...
 
SYS_NO_DISCARD_RESULT const char * getS () const
 Get the string value (may return a NULL pointer) More...
 
SYS_NO_DISCARD_RESULT int64 getSLength () const
 Return the string length (returns -1 if not a string) More...
 
SYS_NO_DISCARD_RESULT const
UT_StringHolder
getStringHolder () const
 Return the string value. More...
 
SYS_NO_DISCARD_RESULT const char * getKey () const
 Get a key value. More...
 
SYS_NO_DISCARD_RESULT int64 getKeyLength () const
 Get the length of the key. More...
 
SYS_NO_DISCARD_RESULT const
UT_StringHolder
getKeyHolder () const
 Return a string holder for the key. More...
 
SYS_NO_DISCARD_RESULT
UT_JSONValueArray
getArray () const
 Get the array value (may return a NULL pointer) More...
 
SYS_NO_DISCARD_RESULT
UT_JSONValueMap
getMap () const
 Get the map value (may return a NULL pointer) More...
 
SYS_NO_DISCARD_RESULT Type getUniformArrayType () const
 
SYS_FORCE_INLINE bool import (bool &result) const
 Extract a bool (returns false if type is invalid) More...
 
SYS_FORCE_INLINE bool import (int64 &result) const
 Extract an integer (returns false if type is invalid) More...
 
SYS_FORCE_INLINE bool import (fpreal64 &result) const
 Extract an float (returns false if type is invalid) More...
 
bool import (UT_WorkBuffer &result) const
 Extract a string or key (returns false if type is invalid) More...
 
bool import (UT_StringHolder &result) const
 
bool import (int64 *result, int size) const
 
bool import (fpreal64 *result, int size) const
 
bool import (UT_Array< int64 > &array) const
 Extract a vector of integers from a JSON_Array. More...
 
bool import (UT_Array< fpreal64 > &array) const
 Extract a vector of reals from a JSON_Array. More...
 
bool import (UT_StringArray &array) const
 Extract a vector of strings from a JSON_Array. More...
 
bool isNumber () const
 Returns whether the value can be interpreted as a number. More...
 
void setNull ()
 Set value to a null. More...
 
void setBool (bool v)
 Set value to an bool. More...
 
void setInt (int64 v)
 Set value to an int. More...
 
void setReal (fpreal64 v)
 Set value to an int. More...
 
void setString (const char *s, int64 length=-1)
 
void setString (const UT_StringHolder &s)
 
void setKey (const char *s, int64 length=-1)
 
void setKey (const UT_StringHolder &s)
 
void setArray (UT_JSONValueArray *array)
 Set value to the array. More...
 
void setMap (UT_JSONValueMap *map)
 Set value to the map. More...
 
void startArray ()
 Start building an array from scratch. More...
 
bool appendArray (const UT_JSONValue &v)
 Add an element to an array (returns false if operation fails) More...
 
void startMap ()
 Start building a map from scratch . More...
 
bool appendMap (const UT_StringHolder &key, const UT_JSONValue &v)
 Add an element to a map (returns false if operation fails) More...
 
UT_JSONValueMapaddMapChild (const UT_StringHolder &map_key)
 
UT_JSONValueArrayaddArrayChild (const UT_StringHolder &map_key)
 
SYS_NO_DISCARD_RESULT int getMapIndex () const
 Used internally by UT_JSONValueMap. More...
 
void setMapIndex (int i)
 Used internally by UT_JSONValueMap. More...
 
SYS_NO_DISCARD_RESULT int getSourceLine () const
 Used by UT_JSONValue parsing from a stream. More...
 
SYS_NO_DISCARD_RESULT int getSourceOffset () const
 
void setSourceOffset (int line, int offset)
 
SYS_NO_DISCARD_RESULT traverser beginTraversal ()
 
SYS_NO_DISCARD_RESULT
UT_JSONValue
findValueRecursive (const UT_StringHolder &map_key, const char *key_value)
 
SYS_NO_DISCARD_RESULT
UT_JSONValue
findParentValueRecursive (const UT_StringHolder &map_key, const char *key_value)
 
void removeNullValuesRecursive ()
 
SYS_NO_DISCARD_RESULT SYS_HashType hash () const
 Compute a hash. More...
 
bool setUniformArray (int nvalues, const int32 *data)
 
bool setUniformArray (int nvalues, const int64 *data)
 
bool setUniformArray (int nvalues, const fpreal16 *data)
 
bool setUniformArray (int nvalues, const fpreal32 *data)
 
bool setUniformArray (int nvalues, const fpreal64 *data)
 
bool setUniformArray (int nvalues, const UT_StringHolder *data)
 
SYS_NO_DISCARD_RESULT bool operator== (const UT_JSONValue &src) const
 
SYS_NO_DISCARD_RESULT bool operator!= (const UT_JSONValue &src) const
 

Detailed Description

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

UT_JSONIStream *is(utistream);
if (!value.load(parser, *is))
reportFailure(parser.getErrors());

To save a value:

UT_JSONWriterSubclass &os;
value.save(os);

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:

bool
my_handle::jsonKey(UT_JSONParser &parser, const char *token, int64)
{
UT_String keyword;
keyword.harden(token);
if (!value.load(parser))
return false;
process(keyword, value);
return true;
}
See Also
UT_JSONParser, UT_JSONWriter, UT_JSONHandle, UT_JSONValueArray, UT_JSONValueMap
Examples:
packedshareddata/GU_PackedSharedData.C, tetprim/GEO_PrimTetra.C, and tetprim/GEO_PrimTetra.h.

Definition at line 77 of file UT_JSONValue.h.

Member Enumeration Documentation

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.

Constructor & Destructor Documentation

UT_JSONValue::UT_JSONValue ( )
UT_JSONValue::UT_JSONValue ( bool  value)
explicit
UT_JSONValue::UT_JSONValue ( int64  value)
explicit
UT_JSONValue::UT_JSONValue ( fpreal64  value)
explicit
UT_JSONValue::UT_JSONValue ( const char *  string,
int64  length = -1 
)
UT_JSONValue::UT_JSONValue ( const UT_StringHolder s)
UT_JSONValue::UT_JSONValue ( const UT_JSONValue v)
UT_JSONValue::~UT_JSONValue ( )

Member Function Documentation

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)

SYS_NO_DISCARD_RESULT traverser UT_JSONValue::beginTraversal ( )
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

Note
Real values are cast to 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)

Examples:
packedshareddata/GU_PackedSharedData.C.
SYS_NO_DISCARD_RESULT int UT_JSONValue::getMapIndex ( ) const
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)

SYS_NO_DISCARD_RESULT int UT_JSONValue::getSourceLine ( ) const
inline

Used by UT_JSONValue parsing from a stream.

Definition at line 316 of file UT_JSONValue.h.

SYS_NO_DISCARD_RESULT int UT_JSONValue::getSourceOffset ( ) const
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.

Examples:
packedshareddata/GU_PackedSharedData.C.
SYS_NO_DISCARD_RESULT Type UT_JSONValue::getType ( ) const
inline

Get the type of data stored in the object.

Examples:
packedshareddata/GU_PackedSharedData.C.

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:

  • JSON_NULL: The array is heterogeneous (or not an array)
  • JSON_BOOL: The array is only bool values
  • JSON_INT: The array is all bool or int values
  • JSON_REAL: The array is bool, int or real values
  • JSON_STRING: The array is all strings
SYS_NO_DISCARD_RESULT SYS_HashType UT_JSONValue::hash ( ) const

Compute a hash.

SYS_FORCE_INLINE bool UT_JSONValue::import ( bool &  result) const
inline

Extract a bool (returns false if type is invalid)

Definition at line 172 of file UT_JSONValue.h.

SYS_FORCE_INLINE bool UT_JSONValue::import ( int64 result) const
inline

Extract an integer (returns false if type is invalid)

Definition at line 191 of file UT_JSONValue.h.

SYS_FORCE_INLINE bool UT_JSONValue::import ( fpreal64 result) const
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
bool UT_JSONValue::import ( int64 result,
int  size 
) const

Extract a tuple of integers from an JSON_Array. If there aren't enough elements in the array, this method fails

bool UT_JSONValue::import ( fpreal64 result,
int  size 
) const

Extract a tuple of floats from an JSON_Array. If there aren't enough elements in the array, this method fails

bool UT_JSONValue::import ( UT_Array< int64 > &  array) const

Extract a vector of integers from a JSON_Array.

bool UT_JSONValue::import ( UT_Array< fpreal64 > &  array) const

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.

bool UT_JSONValue::isNumber ( ) const
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.

Test equality

Definition at line 521 of file UT_JSONValue.h.

UT_JSONValue& UT_JSONValue::operator= ( const UT_JSONValue v)
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

Parameters
parserThe parser
isIf specified, data will be read from this stream. Otherwise, the stream associated with the parser will be used.
Examples:
packedshareddata/GU_PackedSharedData.C.
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.

void UT_JSONValue::setInt ( int64  v)

Set value to an int.

void UT_JSONValue::setKey ( const char *  s,
int64  length = -1 
)
inline

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.

void UT_JSONValue::setKey ( const UT_StringHolder s)
inline

Definition at line 269 of file UT_JSONValue.h.

void UT_JSONValue::setMap ( UT_JSONValueMap map)

Set value to the map.

void UT_JSONValue::setMapIndex ( int  i)
inline

Used internally by UT_JSONValueMap.

Definition at line 312 of file UT_JSONValue.h.

void UT_JSONValue::setNull ( )
inline

Set value to a null.

Definition at line 252 of file UT_JSONValue.h.

void UT_JSONValue::setReal ( fpreal64  v)

Set value to an int.

void UT_JSONValue::setSourceOffset ( int  line,
int  offset 
)
inline

Definition at line 320 of file UT_JSONValue.h.

void UT_JSONValue::setString ( const char *  s,
int64  length = -1 
)
inline

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.

void UT_JSONValue::setString ( const UT_StringHolder s)
inline

Definition at line 263 of file UT_JSONValue.h.

bool UT_JSONValue::setUniformArray ( int  nvalues,
const int32 data 
)

Build a uniform array of values

bool UT_JSONValue::setUniformArray ( int  nvalues,
const int64 data 
)

Build a uniform array of values

bool UT_JSONValue::setUniformArray ( int  nvalues,
const fpreal16 data 
)

Build a uniform array of values

bool UT_JSONValue::setUniformArray ( int  nvalues,
const fpreal32 data 
)

Build a uniform array of values

bool UT_JSONValue::setUniformArray ( int  nvalues,
const fpreal64 data 
)

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.

See Also
appendArray
void UT_JSONValue::startMap ( )

Start building a map from scratch .

See Also
appendMap

Member Data Documentation

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.


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