archiver.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "archiver.h"
  2. #include <cassert>
  3. #include <stack>
  4. #include "rapidjson/document.h"
  5. #include "rapidjson/prettywriter.h"
  6. #include "rapidjson/stringbuffer.h"
  7. using namespace rapidjson;
  8. struct JsonReaderStackItem {
  9. enum State {
  10. BeforeStart, //!< An object/array is in the stack but it is not yet called by StartObject()/StartArray().
  11. Started, //!< An object/array is called by StartObject()/StartArray().
  12. Closed //!< An array is closed after read all element, but before EndArray().
  13. };
  14. JsonReaderStackItem(const Value* value, State state) : value(value), state(state), index() {}
  15. const Value* value;
  16. State state;
  17. SizeType index; // For array iteration
  18. };
  19. typedef std::stack<JsonReaderStackItem> JsonReaderStack;
  20. #define DOCUMENT reinterpret_cast<Document*>(mDocument)
  21. #define STACK (reinterpret_cast<JsonReaderStack*>(mStack))
  22. #define TOP (STACK->top())
  23. #define CURRENT (*TOP.value)
  24. JsonReader::JsonReader(const char* json) : mDocument(), mStack(), mError(false) {
  25. mDocument = new Document;
  26. DOCUMENT->Parse(json);
  27. if (DOCUMENT->HasParseError())
  28. mError = true;
  29. else {
  30. mStack = new JsonReaderStack;
  31. STACK->push(JsonReaderStackItem(DOCUMENT, JsonReaderStackItem::BeforeStart));
  32. }
  33. }
  34. JsonReader::~JsonReader() {
  35. delete DOCUMENT;
  36. delete STACK;
  37. }
  38. // Archive concept
  39. JsonReader& JsonReader::StartObject() {
  40. if (!mError) {
  41. if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart)
  42. TOP.state = JsonReaderStackItem::Started;
  43. else
  44. mError = true;
  45. }
  46. return *this;
  47. }
  48. JsonReader& JsonReader::EndObject() {
  49. if (!mError) {
  50. if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
  51. Next();
  52. else
  53. mError = true;
  54. }
  55. return *this;
  56. }
  57. JsonReader& JsonReader::Member(const char* name) {
  58. if (!mError) {
  59. if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) {
  60. Value::ConstMemberIterator memberItr = CURRENT.FindMember(name);
  61. if (memberItr != CURRENT.MemberEnd())
  62. STACK->push(JsonReaderStackItem(&memberItr->value, JsonReaderStackItem::BeforeStart));
  63. else
  64. mError = true;
  65. }
  66. else
  67. mError = true;
  68. }
  69. return *this;
  70. }
  71. bool JsonReader::HasMember(const char* name) const {
  72. if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
  73. return CURRENT.HasMember(name);
  74. return false;
  75. }
  76. JsonReader& JsonReader::StartArray(size_t* size) {
  77. if (!mError) {
  78. if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) {
  79. TOP.state = JsonReaderStackItem::Started;
  80. if (size)
  81. *size = CURRENT.Size();
  82. if (!CURRENT.Empty()) {
  83. const Value* value = &CURRENT[TOP.index];
  84. STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
  85. }
  86. else
  87. TOP.state = JsonReaderStackItem::Closed;
  88. }
  89. else
  90. mError = true;
  91. }
  92. return *this;
  93. }
  94. JsonReader& JsonReader::EndArray() {
  95. if (!mError) {
  96. if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed)
  97. Next();
  98. else
  99. mError = true;
  100. }
  101. return *this;
  102. }
  103. JsonReader& JsonReader::operator&(bool& b) {
  104. if (!mError) {
  105. if (CURRENT.IsBool()) {
  106. b = CURRENT.GetBool();
  107. Next();
  108. }
  109. else
  110. mError = true;
  111. }
  112. return *this;
  113. }
  114. JsonReader& JsonReader::operator&(unsigned& u) {
  115. if (!mError) {
  116. if (CURRENT.IsUint()) {
  117. u = CURRENT.GetUint();
  118. Next();
  119. }
  120. else
  121. mError = true;
  122. }
  123. return *this;
  124. }
  125. JsonReader& JsonReader::operator&(int& i) {
  126. if (!mError) {
  127. if (CURRENT.IsInt()) {
  128. i = CURRENT.GetInt();
  129. Next();
  130. }
  131. else
  132. mError = true;
  133. }
  134. return *this;
  135. }
  136. JsonReader& JsonReader::operator&(double& d) {
  137. if (!mError) {
  138. if (CURRENT.IsNumber()) {
  139. d = CURRENT.GetDouble();
  140. Next();
  141. }
  142. else
  143. mError = true;
  144. }
  145. return *this;
  146. }
  147. JsonReader& JsonReader::operator&(std::string& s) {
  148. if (!mError) {
  149. if (CURRENT.IsString()) {
  150. s = CURRENT.GetString();
  151. Next();
  152. }
  153. else
  154. mError = true;
  155. }
  156. return *this;
  157. }
  158. JsonReader& JsonReader::SetNull() {
  159. // This function is for JsonWriter only.
  160. mError = true;
  161. return *this;
  162. }
  163. void JsonReader::Next() {
  164. if (!mError) {
  165. assert(!STACK->empty());
  166. STACK->pop();
  167. if (!STACK->empty() && CURRENT.IsArray()) {
  168. if (TOP.state == JsonReaderStackItem::Started) { // Otherwise means reading array item pass end
  169. if (TOP.index < CURRENT.Size() - 1) {
  170. const Value* value = &CURRENT[++TOP.index];
  171. STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
  172. }
  173. else
  174. TOP.state = JsonReaderStackItem::Closed;
  175. }
  176. else
  177. mError = true;
  178. }
  179. }
  180. }
  181. #undef DOCUMENT
  182. #undef STACK
  183. #undef TOP
  184. #undef CURRENT
  185. ////////////////////////////////////////////////////////////////////////////////
  186. // JsonWriter
  187. #define WRITER reinterpret_cast<PrettyWriter<StringBuffer>*>(mWriter)
  188. #define STREAM reinterpret_cast<StringBuffer*>(mStream)
  189. JsonWriter::JsonWriter() : mWriter(), mStream() {
  190. mStream = new StringBuffer;
  191. mWriter = new PrettyWriter<StringBuffer>(*STREAM);
  192. }
  193. JsonWriter::~JsonWriter() {
  194. delete WRITER;
  195. delete STREAM;
  196. }
  197. const char* JsonWriter::GetString() const {
  198. return STREAM->GetString();
  199. }
  200. JsonWriter& JsonWriter::StartObject() {
  201. WRITER->StartObject();
  202. return *this;
  203. }
  204. JsonWriter& JsonWriter::EndObject() {
  205. WRITER->EndObject();
  206. return *this;
  207. }
  208. JsonWriter& JsonWriter::Member(const char* name) {
  209. WRITER->String(name, static_cast<SizeType>(strlen(name)));
  210. return *this;
  211. }
  212. bool JsonWriter::HasMember(const char*) const {
  213. // This function is for JsonReader only.
  214. assert(false);
  215. return false;
  216. }
  217. JsonWriter& JsonWriter::StartArray(size_t*) {
  218. WRITER->StartArray();
  219. return *this;
  220. }
  221. JsonWriter& JsonWriter::EndArray() {
  222. WRITER->EndArray();
  223. return *this;
  224. }
  225. JsonWriter& JsonWriter::operator&(bool& b) {
  226. WRITER->Bool(b);
  227. return *this;
  228. }
  229. JsonWriter& JsonWriter::operator&(unsigned& u) {
  230. WRITER->Uint(u);
  231. return *this;
  232. }
  233. JsonWriter& JsonWriter::operator&(int& i) {
  234. WRITER->Int(i);
  235. return *this;
  236. }
  237. JsonWriter& JsonWriter::operator&(double& d) {
  238. WRITER->Double(d);
  239. return *this;
  240. }
  241. JsonWriter& JsonWriter::operator&(std::string& s) {
  242. WRITER->String(s.c_str(), static_cast<SizeType>(s.size()));
  243. return *this;
  244. }
  245. JsonWriter& JsonWriter::SetNull() {
  246. WRITER->Null();
  247. return *this;
  248. }
  249. #undef STREAM
  250. #undef WRITER