HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_JSONArchive.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_JSONArchive.h (UT Library, C++)
7  *
8  * COMMENTS: JSON archive for use with UT_Serialization
9  *
10  */
11 
12 #ifndef __UT_JSONARCHIVE_H_INCLUDED__
13 #define __UT_JSONARCHIVE_H_INCLUDED__
14 
15 /// @file
16 /// JSON archive for use with UT_Serialization
17 ///
18 
19 #include "UT_JSONParser.h"
20 #include "UT_JSONWriter.h"
21 #include "UT_UniquePtr.h"
22 #include "UT_Serialization.h"
23 
24 /// @class UT_JSONArchive
25 /// @brief Use this to create a class archive for serializing classes to/from
26 /// streams as JSON.
27 ///
28 /// To serialize your data, you first have to implement serialization functions
29 /// as described in UT_Serialize.h. Then just this class to perform it.
30 /// eg.
31 /// @code
32 /// // form 1
33 /// cout << UT_JSONArchive<MyClassA>::Output(a_object);
34 /// cin >> UT_JSONArchive<MyClassB>::Input(b_object);
35 ///
36 /// // form 2
37 /// UT_JSONArchive<MyClassA>::Output output_archive(my_object);
38 /// UT_JSONArchive<MyClassA>::Input input_archive(my_object);
39 /// output_archive(cout);
40 /// input_archive(cin);
41 /// @endcode
42 ///
43 
45 {
46  class Output : public UT_SaveArchiver<Output>
47  {
48  public:
50  : myOut(UT_JSONWriter::allocWriter(os.getOStream(), os.isBinary()))
51  {
52  }
53 
55  {
56  return myOut->jsonBeginMap();
57  }
59  {
60  return myOut->jsonEndMap();
61  }
62  bool serializeKey(const char *key)
63  {
64  return myOut->jsonKeyToken(key);
65  }
66 
67  template <typename T>
68  bool serializeValue(const T &val)
69  {
70  return myOut->jsonValue(val);
71  }
72  bool serializeStringValue(const char *str)
73  {
74  return myOut->jsonStringToken(str);
75  }
76 
78  {
79  if (!myOut->jsonBeginArray())
80  return false;
81  return serializeValue(count);
82  }
84  {
85  return myOut->jsonEndArray();
86  }
87  // serializeArray() provided by superclass
88 
89  template <typename T>
91  {
92  return myOut->jsonUniformArray(count, vec);
93  }
94 
95  private:
97  };
98 
99  class Input : public UT_LoadArchiver<Input>
100  {
101  public:
103  : myAutoParser(is)
104  {
105  }
106 
108  {
109  return *myAutoParser;
110  }
111 
113  {
114  myTraverser = parser().beginMap();
115  return !myTraverser.atEnd();
116  }
117  bool serializeMapEnd() // return true when we hit the end
118  {
119  myTraverser.advance();
120  return myTraverser.atEnd();
121  }
123  {
124  return myTraverser.getLowerKey(key);
125  }
126 
127  template <typename T>
129  {
130  if (!parser().parseValue(val))
131  return false; // end of file
132  return true;
133  }
135  {
136  if (!parser().parseString(str))
137  return false; // end of file
138  return true;
139  }
140 
142  {
143  bool error;
144  if (!parser().parseBeginArray(error))
145  return false;
146  return serializeValue(count);
147  }
149  {
150  bool error;
151  if (!parser().parseEndArray(error))
152  return false;
153  return true;
154  }
155  // serializeArray() provided by superclass
156 
157  template <typename T>
159  {
160  return (parser().parseUniformArray(vec, count) == count);
161  }
162 
163  private:
164  UT_AutoJSONParser myAutoParser;
165  UT_JSONParser::iterator myTraverser;
166  };
167 };
168 
169 /// Main class for starting serialization
170 template <typename OBJ_T>
172 {
175 };
176 
177 #endif // __UT_JSONARCHIVE_H_INCLUDED__
bool serializeUniformArray(T *vec, int64 count)
bool serializeStringValue(const char *str)
bool serializeValue(const T &val)
UT_SaveArchiverManip< OBJ_T, UT_JSONArchiver > Output
bool serializeArrayBegin(int64 &count)
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
bool serializeKey(UT_WorkBuffer &key)
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
Input(UT_IStream &is)
UT_JSONParser & parser()
Output(UT_OStream &os)
long long int64
Definition: SYS_Types.h:100
Base class for archivers.
Traverse an array object in the parser.
bool serializeStringValue(UT_WorkBuffer &str)
Main class for starting serialization.
UT_LoadArchiverManip< OBJ_T, UT_JSONArchiver > Input
GLint GLsizei count
Definition: glcorearb.h:404
bool serializeArrayBegin(int64 count)
bool serializeUniformArray(T *vec, int64 count)
bool serializeKey(const char *key)
GLuint GLfloat * val
Definition: glcorearb.h:1607
Manipulators for streaming archivers.
bool serializeValue(T &val)
iterator beginMap()
bool getLowerKey(T &key)
Get a lower case map key (for case insensitive maps)