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 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...
 
Type getType () const
 Get the type of data stored in the object. More...
 
bool getB () const
 Get the bool value. Interprets integer/float as bool. More...
 
int64 getI () const
 
fpreal64 getF () const
 Get the real value. Interprets bool/int as reals. More...
 
const char * getS () const
 Get the string value (may return a NULL pointer) More...
 
int64 getSLength () const
 Return the string length (returns -1 if not a string) More...
 
const UT_StringHoldergetStringHolder () const
 Return the string value. More...
 
const char * getKey () const
 Get a key value. More...
 
int64 getKeyLength () const
 Get the length of the key. More...
 
const UT_StringHoldergetKeyHolder () const
 Return a string holder for the key. More...
 
UT_JSONValueArraygetArray () const
 Get the array value (may return a NULL pointer) More...
 
UT_JSONValueMapgetMap () const
 Get the map value (may return a NULL pointer) More...
 
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)
 
int getMapIndex () const
 Used internally by UT_JSONValueMap. More...
 
void setMapIndex (int i)
 Used internally by UT_JSONValueMap. More...
 
int getSourceLine () const
 Used by UT_JSONValue parsing from a stream. More...
 
int getSourceOffset () const
 
void setSourceOffset (int line, int offset)
 
traverser beginTraversal ()
 
UT_JSONValuefindValueRecursive (const UT_StringHolder &map_key, const char *key_value)
 
UT_JSONValuefindParentValueRecursive (const UT_StringHolder &map_key, const char *key_value)
 
void removeNullValuesRecursive ()
 
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)
 

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 75 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 79 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)

traverser UT_JSONValue::beginTraversal ( )
inline

Definition at line 496 of file UT_JSONValue.h.

void UT_JSONValue::dump ( ) const

Dump the value to stdout (same as save to cout)

UT_JSONValue* UT_JSONValue::findParentValueRecursive ( const UT_StringHolder map_key,
const char *  key_value 
)
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.

UT_JSONValueArray* UT_JSONValue::getArray ( ) const

Get the array value (may return a NULL pointer)

bool UT_JSONValue::getB ( ) const

Get the bool value. Interprets integer/float as bool.

fpreal64 UT_JSONValue::getF ( ) const

Get the real value. Interprets bool/int as reals.

int64 UT_JSONValue::getI ( ) const

Get the integer value. Intereprets bool/real as integer

Note
Real values are cast to integer.
const char* UT_JSONValue::getKey ( ) const

Get a key value.

const UT_StringHolder* UT_JSONValue::getKeyHolder ( ) const

Return a string holder for the key.

int64 UT_JSONValue::getKeyLength ( ) const

Get the length of the key.

UT_JSONValueMap* UT_JSONValue::getMap ( ) const

Get the map value (may return a NULL pointer)

Examples:
packedshareddata/GU_PackedSharedData.C.
int UT_JSONValue::getMapIndex ( ) const
inline

Used internally by UT_JSONValueMap.

Definition at line 305 of file UT_JSONValue.h.

const char* UT_JSONValue::getS ( ) const

Get the string value (may return a NULL pointer)

int64 UT_JSONValue::getSLength ( ) const

Return the string length (returns -1 if not a string)

int UT_JSONValue::getSourceLine ( ) const
inline

Used by UT_JSONValue parsing from a stream.

Definition at line 312 of file UT_JSONValue.h.

int UT_JSONValue::getSourceOffset ( ) const
inline

Definition at line 314 of file UT_JSONValue.h.

const UT_StringHolder* UT_JSONValue::getStringHolder ( ) const

Return the string value.

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

Get the type of data stored in the object.

Examples:
packedshareddata/GU_PackedSharedData.C.

Definition at line 130 of file UT_JSONValue.h.

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_FORCE_INLINE bool UT_JSONValue::import ( bool &  result) const
inline

Extract a bool (returns false if type is invalid)

Definition at line 168 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 187 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 206 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 241 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.

UT_JSONValue& UT_JSONValue::operator= ( const UT_JSONValue v)
inline

Definition at line 99 of file UT_JSONValue.h.

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.
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 263 of file UT_JSONValue.h.

void UT_JSONValue::setKey ( const UT_StringHolder s)
inline

Definition at line 265 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 308 of file UT_JSONValue.h.

void UT_JSONValue::setNull ( )
inline

Set value to a null.

Definition at line 248 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 316 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 257 of file UT_JSONValue.h.

void UT_JSONValue::setString ( const UT_StringHolder s)
inline

Definition at line 259 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 523 of file UT_JSONValue.h.

bool UT_JSONValue::myBool

Definition at line 516 of file UT_JSONValue.h.

int64 UT_JSONValue::myInt

Definition at line 517 of file UT_JSONValue.h.

UT_JSONValueMap* UT_JSONValue::myMap

Definition at line 524 of file UT_JSONValue.h.

fpreal64 UT_JSONValue::myReal

Definition at line 518 of file UT_JSONValue.h.


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