UT_JSONValue Class Reference

Class to store JSON objects as C++ objects. More...

#include <UT_JSONValue.h>

List of all members.

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 (const UT_JSONValue &v)
 ~UT_JSONValue ()
UT_JSONValueoperator= (const UT_JSONValue &v)
bool parseValue (UT_JSONParser &parser, UT_JSONIStream *is=0)
bool parseValue (UT_JSONParser &parser, UT_IStream *is)
bool save (UT_JSONWriter &os) const
 Save the object the output stream.
Type getType () const
 Get the type of data stored in the object.
bool getB () const
 Get the bool value. Interprets integer/float as bool.
int64 getI () const
fpreal64 getF () const
 Get the real value. Interprets bool/int as reals.
const char * getS () const
 Get the string value (may return a NULL pointer).
int64 getSLength () const
 Return the string length (returns -1 if not a string).
const char * getKey () const
 Get a key value.
int64 getKeyLength () const
 Get the length of the key.
UT_JSONValueArraygetArray () const
 Get the array value (may return a NULL pointer).
UT_JSONValueMapgetMap () const
 Get the map value (may return a NULL pointer).
bool import (bool &result) const
 Extract a bool (returns false if type is invalid).
bool import (int64 &result) const
 Extract an integer (returns false if type is invalid).
bool import (fpreal64 &result) const
 Extract an float (returns false if type is invalid).
bool import (UT_WorkBuffer &result) const
 Extract a string or key (returns false if type is invalid).
bool import (int64 *result, int size) const
bool import (fpreal64 *result, int size) const
bool isNumber () const
 Returns whether the value can be interpreted as a number.
void setNull ()
 Set value to a null.
void setBool (bool v)
 Set value to an bool.
void setInt (int64 v)
 Set value to an int.
void setReal (fpreal64 v)
 Set value to an int.
void setString (const char *s, int64 length=-1)
void setKey (const char *s, int64 length=-1)
void setArray (UT_JSONValueArray *array)
 Set value to the array.
void setMap (UT_JSONValueMap *map)
 Set value to the map.
void startArray ()
 Start building an array from scratch.
bool appendArray (const UT_JSONValue &v)
 Add an element to an array (returns false if operation fails).
void startMap ()
 Start building a map from scratch .
bool appendMap (const char *key, const UT_JSONValue &v)
 Add an element to a map (returns false if operation fails).
int getMapIndex () const
 Used internally by UT_JSONValueMap.
void setMapIndex (int i)
 Used internally bu UT_JSONValueMap.
bool setUniformArray (int len, const int32 *data)
bool setUniformArray (int len, const int64 *data)
bool setUniformArray (int len, const fpreal32 *data)
bool setUniformArray (int len, const fpreal64 *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 = UT_JSONIStream::create(args);
     UT_JSONParser   parser();
     UT_JSONValue    value;
     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;
          UT_JSONValue       value;
          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

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 ( const UT_JSONValue v  ) 

UT_JSONValue::~UT_JSONValue (  ) 


Member Function Documentation

bool UT_JSONValue::appendArray ( const UT_JSONValue v  ) 

Add an element to an array (returns false if operation fails).

bool UT_JSONValue::appendMap ( const char *  key,
const UT_JSONValue v 
)

Add an element to a map (returns false if operation fails).

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.

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).

int UT_JSONValue::getMapIndex (  )  const [inline]

Used internally by UT_JSONValueMap.

Definition at line 199 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).

Type UT_JSONValue::getType ( void   )  const [inline]

Get the type of data stored in the object.

Definition at line 115 of file UT_JSONValue.h.

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 ( 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 ( UT_WorkBuffer result  )  const

Extract a string or key (returns false if type is invalid).

bool UT_JSONValue::import ( fpreal64 result  )  const

Extract an float (returns false if type is invalid).

bool UT_JSONValue::import ( int64 result  )  const

Extract an integer (returns false if type is invalid).

bool UT_JSONValue::import ( bool &  result  )  const

Extract a bool (returns false if type is invalid).

bool UT_JSONValue::isNumber (  )  const [inline]

Returns whether the value can be interpreted as a number.

Definition at line 153 of file UT_JSONValue.h.

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

Definition at line 94 of file UT_JSONValue.h.

bool UT_JSONValue::parseValue ( UT_JSONParser parser,
UT_IStream is 
)

Convenience method to parse using a UT_IStream.

See also:
UT_JSONIStream.

bool UT_JSONValue::parseValue ( UT_JSONParser parser,
UT_JSONIStream is = 0 
)

Read the next value from the parser and store it in this object

Parameters:
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::save ( UT_JSONWriter os  )  const

Save the object the output stream.

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 173 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 bu UT_JSONValueMap.

Definition at line 201 of file UT_JSONValue.h.

void UT_JSONValue::setNull (  )  [inline]

Set value to a null.

Definition at line 160 of file UT_JSONValue.h.

void UT_JSONValue::setReal ( fpreal64  v  ) 

Set value to an int.

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

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

Build a uniform array of values

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

Build a uniform array of values

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

Build a uniform array of values

bool UT_JSONValue::setUniformArray ( int  len,
const int32 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

Definition at line 216 of file UT_JSONValue.h.

Definition at line 208 of file UT_JSONValue.h.

Definition at line 209 of file UT_JSONValue.h.

Definition at line 217 of file UT_JSONValue.h.

Definition at line 210 of file UT_JSONValue.h.

Definition at line 215 of file UT_JSONValue.h.


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

Generated on Mon Jan 28 00:30:11 2013 for HDK by  doxygen 1.5.9