Electroneum
archiver.cpp
Go to the documentation of this file.
1 #include "archiver.h"
2 #include <cassert>
3 #include <stack>
4 #include "rapidjson/document.h"
7 
8 using namespace rapidjson;
9 
11  enum State {
14  Closed
15  };
16 
18 
19  const Value* value;
21  SizeType index; // For array iteration
22 };
23 
24 typedef std::stack<JsonReaderStackItem> JsonReaderStack;
25 
26 #define DOCUMENT reinterpret_cast<Document*>(mDocument)
27 #define STACK (reinterpret_cast<JsonReaderStack*>(mStack))
28 #define TOP (STACK->top())
29 #define CURRENT (*TOP.value)
30 
31 JsonReader::JsonReader(const char* json) : mDocument(), mStack(), mError(false) {
32  mDocument = new Document;
33  DOCUMENT->Parse(json);
34  if (DOCUMENT->HasParseError())
35  mError = true;
36  else {
37  mStack = new JsonReaderStack;
39  }
40 }
41 
43  delete DOCUMENT;
44  delete STACK;
45 }
46 
47 // Archive concept
49  if (!mError) {
50  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart)
52  else
53  mError = true;
54  }
55  return *this;
56 }
57 
59  if (!mError) {
60  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
61  Next();
62  else
63  mError = true;
64  }
65  return *this;
66 }
67 
69  if (!mError) {
70  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) {
71  Value::ConstMemberIterator memberItr = CURRENT.FindMember(name);
72  if (memberItr != CURRENT.MemberEnd())
74  else
75  mError = true;
76  }
77  else
78  mError = true;
79  }
80  return *this;
81 }
82 
83 bool JsonReader::HasMember(const char* name) const {
84  if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
85  return CURRENT.HasMember(name);
86  return false;
87 }
88 
90  if (!mError) {
91  if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) {
93  if (size)
94  *size = CURRENT.Size();
95 
96  if (!CURRENT.Empty()) {
97  const Value* value = &CURRENT[TOP.index];
99  }
100  else
102  }
103  else
104  mError = true;
105  }
106  return *this;
107 }
108 
110  if (!mError) {
111  if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed)
112  Next();
113  else
114  mError = true;
115  }
116  return *this;
117 }
118 
120  if (!mError) {
121  if (CURRENT.IsBool()) {
122  b = CURRENT.GetBool();
123  Next();
124  }
125  else
126  mError = true;
127  }
128  return *this;
129 }
130 
131 JsonReader& JsonReader::operator&(unsigned& u) {
132  if (!mError) {
133  if (CURRENT.IsUint()) {
134  u = CURRENT.GetUint();
135  Next();
136  }
137  else
138  mError = true;
139  }
140  return *this;
141 }
142 
144  if (!mError) {
145  if (CURRENT.IsInt()) {
146  i = CURRENT.GetInt();
147  Next();
148  }
149  else
150  mError = true;
151  }
152  return *this;
153 }
154 
155 JsonReader& JsonReader::operator&(double& d) {
156  if (!mError) {
157  if (CURRENT.IsNumber()) {
158  d = CURRENT.GetDouble();
159  Next();
160  }
161  else
162  mError = true;
163  }
164  return *this;
165 }
166 
168  if (!mError) {
169  if (CURRENT.IsString()) {
170  s = CURRENT.GetString();
171  Next();
172  }
173  else
174  mError = true;
175  }
176  return *this;
177 }
178 
180  // This function is for JsonWriter only.
181  mError = true;
182  return *this;
183 }
184 
185 void JsonReader::Next() {
186  if (!mError) {
187  assert(!STACK->empty());
188  STACK->pop();
189 
190  if (!STACK->empty() && CURRENT.IsArray()) {
191  if (TOP.state == JsonReaderStackItem::Started) { // Otherwise means reading array item pass end
192  if (TOP.index < CURRENT.Size() - 1) {
193  const Value* value = &CURRENT[++TOP.index];
195  }
196  else
198  }
199  else
200  mError = true;
201  }
202  }
203 }
204 
205 #undef DOCUMENT
206 #undef STACK
207 #undef TOP
208 #undef CURRENT
209 
211 // JsonWriter
212 
213 #define WRITER reinterpret_cast<PrettyWriter<StringBuffer>*>(mWriter)
214 #define STREAM reinterpret_cast<StringBuffer*>(mStream)
215 
216 JsonWriter::JsonWriter() : mWriter(), mStream() {
217  mStream = new StringBuffer;
218  mWriter = new PrettyWriter<StringBuffer>(*STREAM);
219 }
220 
222  delete WRITER;
223  delete STREAM;
224 }
225 
226 const char* JsonWriter::GetString() const {
227  return STREAM->GetString();
228 }
229 
231  WRITER->StartObject();
232  return *this;
233 }
234 
236  WRITER->EndObject();
237  return *this;
238 }
239 
241  WRITER->String(name, static_cast<SizeType>(strlen(name)));
242  return *this;
243 }
244 
245 bool JsonWriter::HasMember(const char*) const {
246  // This function is for JsonReader only.
247  assert(false);
248  return false;
249 }
250 
252  WRITER->StartArray();
253  return *this;
254 }
255 
257  WRITER->EndArray();
258  return *this;
259 }
260 
262  WRITER->Bool(b);
263  return *this;
264 }
265 
266 JsonWriter& JsonWriter::operator&(unsigned& u) {
267  WRITER->Uint(u);
268  return *this;
269 }
270 
272  WRITER->Int(i);
273  return *this;
274 }
275 
276 JsonWriter& JsonWriter::operator&(double& d) {
277  WRITER->Double(d);
278  return *this;
279 }
280 
282  WRITER->String(s.c_str(), static_cast<SizeType>(s.size()));
283  return *this;
284 }
285 
287  WRITER->Null();
288  return *this;
289 }
290 
291 #undef STREAM
292 #undef WRITER
#define DOCUMENT
Definition: archiver.cpp:26
#define STACK
Definition: archiver.cpp:27
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
JsonWriter & StartArray(size_t *size=0)
Definition: archiver.cpp:251
JsonReader & StartObject()
Definition: archiver.cpp:48
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition: archiver.cpp:12
const char * GetString() const
Obtains the serialized JSON string.
Definition: archiver.cpp:226
JsonReader(const char *json)
Constructor.
Definition: archiver.cpp:31
::std::string string
Definition: gtest-port.h:1097
An array is closed after read all element, but before EndArray().
Definition: archiver.cpp:14
#define STREAM
Definition: archiver.cpp:214
const Value * value
Definition: archiver.cpp:19
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:57
const char * name
JsonWriter & StartObject()
Definition: archiver.cpp:230
JsonWriter & operator &(bool &b)
JsonReader & Member(const char *name)
Definition: archiver.cpp:68
JsonReaderStackItem(const Value *value, State state)
Definition: archiver.cpp:17
JsonReader & operator &(bool &b)
JsonWriter & EndArray()
Definition: archiver.cpp:256
Represents a JSON reader which implements Archiver concept.
Definition: archiver.h:56
Writer with indentation and spacing.
Definition: fwd.h:100
#define WRITER
Definition: archiver.cpp:213
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2512
#define TOP
Definition: archiver.cpp:28
bool HasMember(const char *name) const
Definition: archiver.cpp:83
#define false
Definition: stdbool.h:38
GenericStringBuffer< UTF8< char >, CrtAllocator > StringBuffer
Definition: fwd.h:59
JsonWriter & Member(const char *name)
Definition: archiver.cpp:240
main RapidJSON namespace
bool HasMember(const char *name) const
Definition: archiver.cpp:245
JsonWriter()
Constructor.
Definition: archiver.cpp:216
~JsonWriter()
Destructor.
Definition: archiver.cpp:221
std::stack< JsonReaderStackItem > JsonReaderStack
Definition: archiver.cpp:24
#define CURRENT
Definition: archiver.cpp:29
Definition: blake256.h:37
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
JsonWriter & EndObject()
Definition: archiver.cpp:235
JsonReader & EndObject()
Definition: archiver.cpp:58
JsonReader & EndArray()
Definition: archiver.cpp:109
~JsonReader()
Destructor.
Definition: archiver.cpp:42
JsonReader & StartArray(size_t *size=0)
Definition: archiver.cpp:89
JsonReader & SetNull()
Definition: archiver.cpp:179
(Constant) member iterator for a JSON object value
Definition: document.h:99
JsonWriter & SetNull()
Definition: archiver.cpp:286
rapidjson::Document json
Definition: transport.cpp:49