Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
archiver.cpp
Go to the documentation of this file.
1#include "archiver.h"
2#include <cassert>
3#include <stack>
7
8using namespace rapidjson;
9
16
17 JsonReaderStackItem(const Value* value, State state) : value(value), state(state), index() {}
18
19 const Value* value;
21 SizeType index; // For array iteration
22};
23
24typedef 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
31JsonReader::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;
38 STACK->push(JsonReaderStackItem(DOCUMENT, JsonReaderStackItem::BeforeStart));
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
83bool 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
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
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
185void 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
216JsonWriter::JsonWriter() : mWriter(), mStream() {
217 mStream = new StringBuffer;
218 mWriter = new PrettyWriter<StringBuffer>(*STREAM);
219}
220
222 delete WRITER;
223 delete STREAM;
224}
225
226const char* JsonWriter::GetString() const {
227 return STREAM->GetString();
228}
229
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
245bool 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
267 WRITER->Uint(u);
268 return *this;
269}
270
272 WRITER->Int(i);
273 return *this;
274}
275
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 WRITER
Definition archiver.cpp:213
#define DOCUMENT
Definition archiver.cpp:26
#define CURRENT
Definition archiver.cpp:29
#define STACK
Definition archiver.cpp:27
#define STREAM
Definition archiver.cpp:214
std::stack< JsonReaderStackItem > JsonReaderStack
Definition archiver.cpp:24
#define TOP
Definition archiver.cpp:28
std::string name
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition document.h:2325
(Constant) member iterator for a JSON object value
Definition document.h:102
Represents a JSON reader which implements Archiver concept.
Definition archiver.h:56
JsonReader & EndArray()
Definition archiver.cpp:109
bool HasMember(const char *name) const
Definition archiver.cpp:83
~JsonReader()
Destructor.
Definition archiver.cpp:42
JsonReader & StartArray(size_t *size=0)
Definition archiver.cpp:89
JsonReader(const char *json)
Constructor.
Definition archiver.cpp:31
JsonReader & EndObject()
Definition archiver.cpp:58
JsonReader & operator&(bool &b)
Definition archiver.cpp:119
JsonReader & Member(const char *name)
Definition archiver.cpp:68
JsonReader & SetNull()
Definition archiver.cpp:179
JsonReader & StartObject()
Definition archiver.cpp:48
JsonWriter & operator&(bool &b)
Definition archiver.cpp:261
JsonWriter & SetNull()
Definition archiver.cpp:286
JsonWriter & Member(const char *name)
Definition archiver.cpp:240
const char * GetString() const
Obtains the serialized JSON string.
Definition archiver.cpp:226
JsonWriter & StartArray(size_t *size=0)
Definition archiver.cpp:251
JsonWriter & EndObject()
Definition archiver.cpp:235
JsonWriter & EndArray()
Definition archiver.cpp:256
JsonWriter()
Constructor.
Definition archiver.cpp:216
~JsonWriter()
Destructor.
Definition archiver.cpp:221
JsonWriter & StartObject()
Definition archiver.cpp:230
bool HasMember(const char *name) const
Definition archiver.cpp:245
Writer with indentation and spacing.
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition document.h:2506
GenericStringBuffer< UTF8< char >, CrtAllocator > StringBuffer
Definition fwd.h:61
main RapidJSON namespace
#define value
Definition pkcs11.h:157
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
JsonReaderStackItem(const Value *value, State state)
Definition archiver.cpp:17
@ BeforeStart
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition archiver.cpp:12
@ Closed
An array is closed after read all element, but before EndArray().
Definition archiver.cpp:14
@ Started
An object/array is called by StartObject()/StartArray().
Definition archiver.cpp:13
const Value * value
Definition archiver.cpp:19
CK_ULONG d
char * s