HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  return myOut->jsonBeginArray() && serializeValue(count);
80  }
82  {
83  return myOut->jsonEndArray();
84  }
85  // serializeArray() provided by superclass
86 
87  template <typename T>
89  {
90  return myOut->jsonUniformArray(count, vec);
91  }
92 
93  private:
95  };
96 
97  class Input : public UT_LoadArchiver<Input>
98  {
99  public:
100  Input(UT_IStream &is) : myAutoParser(new UT_AutoJSONParser(is))
101  {
102  myParser = &myAutoParser.get()->parser();
103  }
104 
105  Input(UT_JSONParser &p) : myParser(&p)
106  {
107  }
108 
110  {
111  myTraverser = myParser->beginMap();
112  return !myTraverser.atEnd();
113  }
114  bool serializeMapEnd() // return true when we hit the end
115  {
116  myTraverser.advance();
117  return myTraverser.atEnd();
118  }
120  {
121  return myTraverser.getLowerKey(key);
122  }
123 
124  template <typename T>
126  {
127  return myParser->parseValue(val);
128  }
130  {
131  return myParser->parseString(str);
132  }
133 
135  {
136  bool error;
137  return myParser->parseBeginArray(error) && serializeValue(count);
138  }
140  {
141  bool error;
142  return myParser->parseEndArray(error);
143  }
144  // serializeArray() provided by superclass
145 
146  template <typename T>
148  {
149  return myParser->parseUniformArray(vec, count) == count;
150  }
151 
152  private:
153  UT_UniquePtr<UT_AutoJSONParser> myAutoParser;
154  UT_JSONParser *myParser;
155  UT_JSONParser::iterator myTraverser;
156  };
157 };
158 
159 /// Main class for starting serialization
160 template <typename OBJ_T>
162 {
165 };
166 
167 #endif // __UT_JSONARCHIVE_H_INCLUDED__
bool serializeUniformArray(T *vec, int64 count)
bool parseString(UT_WorkBuffer &v)
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:87
bool serializeKey(UT_WorkBuffer &key)
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
Input(UT_IStream &is)
Output(UT_OStream &os)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
< returns > If no error
Definition: snippets.dox:2
Base class for archivers.
Traverse an array object in the parser.
bool serializeStringValue(UT_WorkBuffer &str)
long long int64
Definition: SYS_Types.h:116
Main class for starting serialization.
UT_LoadArchiverManip< OBJ_T, UT_JSONArchiver > Input
bool serializeArrayBegin(int64 count)
int64 parseUniformArray(T *data, int64 len)
bool parseValue(UT_WorkBuffer &v)
bool parseEndArray(bool &error)
bool serializeUniformArray(T *vec, int64 count)
bool serializeKey(const char *key)
GLuint GLfloat * val
Definition: glcorearb.h:1608
bool parseBeginArray(bool &error)
Manipulators for streaming archivers.
Input(UT_JSONParser &p)
bool serializeValue(T &val)
iterator beginMap()
bool getLowerKey(T &key)
Get a lower case map key (for case insensitive maps)
GLint GLsizei count
Definition: glcorearb.h:405