00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Side Effects Software Inc 00008 * 123 Front Street West, Suite 1401 00009 * Toronto, Ontario 00010 * Canada M5J 2M2 00011 * 416-504-9876 00012 * 00013 * NAME: UT_JSONValue.h ( UT Library, C++) 00014 * 00015 * COMMENTS: This class represents a JSON object. 00016 */ 00017 00018 #ifndef __UT_JSONValue__ 00019 #define __UT_JSONValue__ 00020 00021 #include "UT_API.h" 00022 #include "UT_PtrArray.h" 00023 #include "UT_SymbolTable.h" 00024 00025 class UT_JSONValueArray; 00026 class UT_JSONValueMap; 00027 class UT_JSONParser; 00028 class UT_JSONWriter; 00029 class UT_JSONIStream; 00030 class UT_IStream; 00031 class UT_WorkBuffer; 00032 00033 /// @brief Class to store JSON objects as C++ objects 00034 /// 00035 /// This class is able to represent a JSON object in its entirety. Arrays and 00036 /// Maps store arrays and maps of JSON values. 00037 /// 00038 /// There are methods to load a JSONValue from a UT_JSONParser and to save to a 00039 /// UT_JSONWriter object. 00040 /// 00041 /// To load an entire JSON file into memory, you can do something like 00042 /// @code 00043 /// UT_JSONIStream *is = UT_JSONIStream::create(args); 00044 /// UT_JSONParser parser(); 00045 /// UT_JSONValue value; 00046 /// if (!value.load(parser, *is)) 00047 /// reportFailure(parser.getErrors()); 00048 /// @endcode 00049 /// 00050 /// To save a value: 00051 /// @code 00052 /// UT_JSONWriterSubclass &os; 00053 /// value.save(os); 00054 /// @endcode 00055 /// 00056 /// Alternatively, it's possible to use the UT_JSONValue class from within 00057 /// other UT_JSONHandle classes. For example, when loading key/value pairs for 00058 /// an arbitrary map: 00059 /// @code 00060 /// bool 00061 /// my_handle::jsonKey(UT_JSONParser &parser, const char *token, int64) 00062 /// { 00063 /// UT_String keyword; 00064 /// UT_JSONValue value; 00065 /// keyword.harden(token); 00066 /// if (!value.load(parser)) 00067 /// return false; 00068 /// process(keyword, value); 00069 /// return true; 00070 /// } 00071 /// @endcode 00072 /// @see UT_JSONParser, UT_JSONWriter, UT_JSONHandle, UT_JSONValueArray, 00073 /// UT_JSONValueMap 00074 00075 class UT_API UT_JSONValue { 00076 public: 00077 /// Types held in the UT_JSONValue. The number type has been broken out 00078 /// into an integer and a real. 00079 enum Type { 00080 JSON_NULL, // Null 00081 JSON_BOOL, // Bool 00082 JSON_INT, // Integer 00083 JSON_REAL, // Real 00084 JSON_STRING, // String value 00085 JSON_KEY, // Key value 00086 JSON_ARRAY, // An array of UT_JSON_VALUE 00087 JSON_MAP, // A map of keyword, UT_JSONValue object 00088 }; 00089 00090 UT_JSONValue(); 00091 UT_JSONValue(const UT_JSONValue &v); 00092 ~UT_JSONValue(); 00093 00094 UT_JSONValue &operator=(const UT_JSONValue &v) 00095 { 00096 copyFrom(v); 00097 return *this; 00098 } 00099 00100 /// Read the next value from the parser and store it in this object 00101 /// @param parser The parser 00102 /// @param is If specified, data will be read from this stream. 00103 /// Otherwise, the stream associated with the parser will be used. 00104 bool parseValue(UT_JSONParser &parser, UT_JSONIStream *is=0); 00105 00106 /// Convenience method to parse using a UT_IStream. 00107 /// @see UT_JSONIStream. 00108 bool parseValue(UT_JSONParser &parser, UT_IStream *is); 00109 00110 00111 /// Save the object the output stream 00112 bool save(UT_JSONWriter &os) const; 00113 00114 /// Get the type of data stored in the object 00115 Type getType() const { return myType; } 00116 00117 /// Get the bool value. Interprets integer/float as bool 00118 bool getB() const; 00119 /// Get the integer value. Intereprets bool/real as integer 00120 /// @note Real values are cast to integer. 00121 int64 getI() const; 00122 /// Get the real value. Interprets bool/int as reals. 00123 fpreal64 getF() const; 00124 /// Get the string value (may return a NULL pointer) 00125 const char *getS() const; 00126 /// Return the string length (returns -1 if not a string) 00127 int64 getSLength() const; 00128 /// Get a key value 00129 const char *getKey() const; 00130 /// Get the length of the key 00131 int64 getKeyLength() const; 00132 /// Get the array value (may return a NULL pointer) 00133 UT_JSONValueArray *getArray() const; 00134 /// Get the map value (may return a NULL pointer) 00135 UT_JSONValueMap *getMap() const; 00136 00137 /// Extract a bool (returns false if type is invalid) 00138 bool import(bool &result) const; 00139 /// Extract an integer (returns false if type is invalid) 00140 bool import(int64 &result) const; 00141 /// Extract an float (returns false if type is invalid) 00142 bool import(fpreal64 &result) const; 00143 /// Extract a string @b or key (returns false if type is invalid) 00144 bool import(UT_WorkBuffer &result) const; 00145 /// Extract a tuple of integers from an JSON_Array. If there aren't enough 00146 /// elements in the array, this method fails 00147 bool import(int64 *result, int size) const; 00148 /// Extract a tuple of floats from an JSON_Array. If there aren't enough 00149 /// elements in the array, this method fails 00150 bool import(fpreal64 *result, int size) const; 00151 00152 /// Returns whether the value can be interpreted as a number 00153 bool isNumber() const 00154 { 00155 return myType == JSON_REAL || myType == JSON_INT || 00156 myType == JSON_BOOL; 00157 } 00158 00159 /// Set value to a null 00160 void setNull() { clearValue(); } 00161 /// Set value to an bool 00162 void setBool(bool v); 00163 /// Set value to an int 00164 void setInt(int64 v); 00165 /// Set value to an int 00166 void setReal(fpreal64 v); 00167 /// Set string. If the length is not specified, the length of the string 00168 /// will be used. 00169 void setString(const char *s, int64 length=-1) 00170 { setStringType(s, length, JSON_STRING); } 00171 /// Set string. If the length is not specified, the length of the string 00172 /// will be used. 00173 void setKey(const char *s, int64 length=-1) 00174 { setStringType(s, length, JSON_KEY); } 00175 /// Set value to the array 00176 void setArray(UT_JSONValueArray *array); 00177 /// Set value to the map 00178 void setMap(UT_JSONValueMap *map); 00179 00180 /// Start building an array from scratch. @see appendArray 00181 void startArray(); 00182 /// Add an element to an array (returns false if operation fails) 00183 bool appendArray(const UT_JSONValue &v); 00184 00185 /// Start building a map from scratch . @see appendMap 00186 void startMap(); 00187 /// Add an element to a map (returns false if operation fails) 00188 bool appendMap(const char *key, const UT_JSONValue &v); 00189 00190 /// @{ 00191 /// Build a uniform array of values 00192 bool setUniformArray(int len, const int32 *data); 00193 bool setUniformArray(int len, const int64 *data); 00194 bool setUniformArray(int len, const fpreal32 *data); 00195 bool setUniformArray(int len, const fpreal64 *data); 00196 /// @} 00197 00198 /// Used internally by UT_JSONValueMap 00199 int getMapIndex() const { return myMapIndex; } 00200 /// Used internally bu UT_JSONValueMap 00201 void setMapIndex(int i) { myMapIndex = i; } 00202 00203 private: 00204 void clearValue(); 00205 void copyFrom(const UT_JSONValue &s); 00206 void setStringType(const char *s, int64 l, Type t); 00207 union { 00208 bool myBool; 00209 int64 myInt; 00210 fpreal64 myReal; 00211 } myData; 00212 union { 00213 // NOTE: Keep the pointers and scalars separate. The string is stored 00214 // as a pointer and a length so that null characters may be contained. 00215 char *myString; 00216 UT_JSONValueArray *myArray; 00217 UT_JSONValueMap *myMap; 00218 } myPointers; 00219 int myMapIndex; // Location in a map 00220 Type myType; 00221 }; 00222 00223 #endif 00224
1.5.9