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 * Edward Lam 00008 * Side Effects Software Inc 00009 * 123 Front Street West, Suite 1401 00010 * Toronto, Ontario 00011 * Canada M5J 2M2 00012 * 416-504-9876 00013 * 00014 * NAME: UT_JSONArchive.h (UT Library, C++) 00015 * 00016 * COMMENTS: JSON archive for use with UT_Serialization 00017 * 00018 */ 00019 00020 #ifndef __UT_JSONARCHIVE_H_INCLUDED__ 00021 #define __UT_JSONARCHIVE_H_INCLUDED__ 00022 00023 /// @file 00024 /// JSON archive for use with UT_Serialization 00025 /// 00026 00027 #include "UT_Serialization.h" 00028 #include "UT_JSONIStream.h" 00029 #include "UT_JSONParser.h" 00030 #include "UT_JSONWriter.h" 00031 #include <iostream.h> 00032 00033 /// @class UT_JSONArchive 00034 /// @brief Use this to create a class archive for serializing classes to/from 00035 /// streams as JSON. 00036 /// 00037 /// To serialize your data, you first have to implement serialization functions 00038 /// as described in UT_Serialize.h. Then just this class to perform it. 00039 /// eg. 00040 /// @code 00041 /// // form 1 00042 /// cout << UT_JSONArchive<MyClassA>::Output(a_object); 00043 /// cin >> UT_JSONArchive<MyClassB>::Input(b_object); 00044 /// 00045 /// // form 2 00046 /// UT_JSONArchive<MyClassA>::Output output_archive(my_object); 00047 /// UT_JSONArchive<MyClassA>::Input input_archive(my_object); 00048 /// output_archive(cout); 00049 /// input_archive(cin); 00050 /// @endcode 00051 /// 00052 00053 struct UT_JSONArchiver 00054 { 00055 class Output : public UT_SaveArchiver<Output> 00056 { 00057 public: 00058 Output(std::ostream &os) 00059 { 00060 myOut = UT_JSONWriter::allocWriter(os, /*binary*/false); 00061 } 00062 00063 bool serializeMapBegin() 00064 { 00065 return myOut->jsonBeginMap(); 00066 } 00067 bool serializeMapEnd() 00068 { 00069 return myOut->jsonEndMap(); 00070 } 00071 bool serializeKey(const char *key) 00072 { 00073 return myOut->jsonKeyToken(key); 00074 } 00075 00076 template <typename T> 00077 bool serializeValue(const T &val) 00078 { 00079 return myOut->jsonValue(val); 00080 } 00081 bool serializeStringValue(const char *str) 00082 { 00083 return myOut->jsonStringToken(str); 00084 } 00085 00086 bool serializeArrayBegin(int64 count) 00087 { 00088 if (!myOut->jsonBeginArray()) 00089 return false; 00090 return serializeValue(count); 00091 } 00092 bool serializeArrayEnd() 00093 { 00094 return myOut->jsonEndArray(); 00095 } 00096 // serializeArray() provided by superclass 00097 00098 template <typename T> 00099 bool serializeUniformArray(T *vec, int64 count) 00100 { 00101 return myOut->jsonUniformArray(count, vec); 00102 } 00103 00104 private: 00105 UT_JSONWriter * myOut; 00106 }; 00107 00108 class Input : public UT_LoadArchiver<Input> 00109 { 00110 public: 00111 Input(UT_IStream &is) 00112 : myIn() 00113 , myAutoIStream(myIn, is) 00114 { 00115 } 00116 00117 bool serializeMapBegin() 00118 { 00119 myTraverser = myIn.beginMap(); 00120 return !myTraverser.atEnd(); 00121 } 00122 bool serializeMapEnd() // return true when we hit the end 00123 { 00124 myTraverser.advance(); 00125 return myTraverser.atEnd(); 00126 } 00127 bool serializeKey(UT_WorkBuffer &key) 00128 { 00129 return myTraverser.getLowerKey(key); 00130 } 00131 00132 template <typename T> 00133 bool serializeValue(T &val) 00134 { 00135 if (!myIn.parseValue(val)) 00136 return false; // end of file 00137 return true; 00138 } 00139 bool serializeStringValue(UT_WorkBuffer &str) 00140 { 00141 if (!myIn.parseString(str)) 00142 return false; // end of file 00143 return true; 00144 } 00145 00146 bool serializeArrayBegin(int64 &count) 00147 { 00148 bool error; 00149 if (!myIn.parseBeginArray(error)) 00150 return false; 00151 return serializeValue(count); 00152 } 00153 bool serializeArrayEnd() 00154 { 00155 bool error; 00156 if (!myIn.parseEndArray(error)) 00157 return false; 00158 return true; 00159 } 00160 // serializeArray() provided by superclass 00161 00162 template <typename T> 00163 bool serializeUniformArray(T *vec, int64 count) 00164 { 00165 return (myIn.parseUniformArray(vec, count) == count); 00166 } 00167 00168 private: 00169 UT_JSONParser myIn; 00170 UT_AutoJSONIStream myAutoIStream; 00171 UT_JSONParser::traverser myTraverser; 00172 }; 00173 }; 00174 00175 /// Main class for starting serialization 00176 template <typename OBJ_T> 00177 struct UT_JSONArchive 00178 { 00179 typedef UT_LoadArchiverManip<OBJ_T,UT_JSONArchiver> Input; 00180 typedef UT_SaveArchiverManip<OBJ_T,UT_JSONArchiver> Output; 00181 }; 00182 00183 #endif // __UT_JSONARCHIVE_H_INCLUDED__
1.5.9