1
1

streamConsumer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <iostream>
  2. #include "jsonException.hh"
  3. #include "jsonElement.hh"
  4. #include "streamConsumer.hh"
  5. StreamConsumer::StreamConsumer(std::istream &s): stream(s)
  6. { }
  7. StreamConsumer::~StreamConsumer()
  8. {
  9. if (root)
  10. delete root;
  11. }
  12. StreamConsumer *StreamConsumer::read(std::istream &stream)
  13. {
  14. StreamConsumer *inst = new StreamConsumer(stream);
  15. inst->root = inst->readNext(nullptr);
  16. return inst;
  17. }
  18. JSonElement *StreamConsumer::readNext(JSonContainer *parent)
  19. {
  20. std::string buf;
  21. JSonElement *root = consumeToken(parent, buf);
  22. if (root == nullptr)
  23. {
  24. if (buf == "{")
  25. {
  26. return readObject(parent);
  27. }
  28. else if (buf == "[")
  29. {
  30. return readArray(parent);
  31. }
  32. else
  33. return nullptr;
  34. }
  35. return root;
  36. }
  37. JSonElement * const StreamConsumer::getRoot() const
  38. {
  39. return root;
  40. }
  41. JSonObject *StreamConsumer::readObject(JSonContainer *parent)
  42. {
  43. JSonElement *keyObj;
  44. JSonObject *result = nullptr;
  45. std::string buf;
  46. do
  47. {
  48. keyObj = consumeToken(result, buf);
  49. if (result == nullptr && keyObj == nullptr && buf == "}")
  50. return new JSonObject(parent);
  51. JSonPrimitive<std::string> *key = dynamic_cast<JSonPrimitive<std::string> *>(keyObj);
  52. if (key == nullptr)
  53. throw JSonObject::NotAKeyException(stream.tellg(), history);
  54. if (consumeToken(parent, buf) != nullptr || buf != ":")
  55. throw JsonUnexpectedException(':', stream.tellg(), history);
  56. if (result == nullptr)
  57. result = new JSonObject(parent);
  58. else if (result->contains(key->getValue()))
  59. throw JSonObject::DoubleKeyException(stream.tellg(), key->getValue(), history); //Double key
  60. JSonElement *child = readNext(result);
  61. result->push(key->getValue(), child);
  62. delete keyObj;
  63. keyObj = consumeToken(result, buf);
  64. } while (!keyObj && buf != "}");
  65. return result;
  66. }
  67. JSonArray *StreamConsumer::readArray(JSonContainer *parent)
  68. {
  69. JSonArray *result = nullptr;
  70. std::string buf;
  71. JSonElement *child = consumeToken(parent, buf);
  72. if (child == nullptr && buf == "]")
  73. return new JSonArray(parent); //Empty object
  74. do
  75. {
  76. if (child == nullptr && buf == "[")
  77. child = readArray(nullptr);
  78. if (child == nullptr && buf == "{")
  79. child = readObject(nullptr);
  80. if (child == nullptr)
  81. throw JsonNotJsonException(stream.tellg(), history);
  82. if (result == nullptr)
  83. result = new JSonArray(parent);
  84. child->setParent(result);
  85. result->push_back(child);
  86. child = consumeToken(result, buf);
  87. if (child != nullptr)
  88. throw JsonUnexpectedException(']', stream.tellg(), history);
  89. else if (buf == "]")
  90. break;
  91. else if (buf != ",")
  92. throw JsonUnexpectedException(']', stream.tellg(), history);
  93. child = consumeToken(result, buf);
  94. } while (true);
  95. return result;
  96. }
  97. JSonElement *StreamConsumer::consumeToken(JSonContainer *parent, std::string &buf)
  98. {
  99. bool escaped = false;
  100. bool inString = false;
  101. bool inBool = false;
  102. bool inNumber = false;
  103. bool numberIsFloat = false;
  104. while (stream.good())
  105. {
  106. char c = stream.get();
  107. history.put(c);
  108. if (inString)
  109. {
  110. if (!escaped)
  111. {
  112. if (c == '"')
  113. return new JSonPrimitive<std::string>(parent, buf);
  114. else if (c == '\\')
  115. escaped = true;
  116. else
  117. buf += c;
  118. }
  119. else
  120. {
  121. if (c == '\\' || c == '"')
  122. {
  123. buf += c;
  124. escaped = false;
  125. }
  126. else
  127. throw JsonEscapedException(c, stream.tellg(), history);
  128. }
  129. }
  130. else if (inBool)
  131. {
  132. if (c == 'a' || c == 'e' || c == 'l' || c == 'r' || c == 's' || c == 'u')
  133. buf += c;
  134. else if (buf == "true")
  135. {
  136. history.pop_back();
  137. stream.unget();
  138. return new JSonPrimitive<bool>(parent, true);
  139. }
  140. else if (buf == "false")
  141. {
  142. history.pop_back();
  143. stream.unget();
  144. return new JSonPrimitive<bool>(parent, false);
  145. }
  146. else if (ignoreChar(c))
  147. ;
  148. else
  149. throw JsonFormatException(stream.tellg(), history);
  150. }
  151. else if (inNumber)
  152. {
  153. if (c >= '0' && c <= '9')
  154. buf += c;
  155. else if (c == '.' && !numberIsFloat)
  156. {
  157. numberIsFloat = true;
  158. buf += c;
  159. }
  160. else
  161. {
  162. history.pop_back();
  163. stream.unget();
  164. if (numberIsFloat)
  165. return new JSonPrimitive<float>(parent, std::stof(buf));
  166. try
  167. {
  168. return new JSonPrimitive<int>(parent, std::stoi(buf));
  169. }
  170. catch(std::out_of_range e)
  171. {
  172. return new JSonPrimitive<long long>(parent, std::stol(buf));
  173. }
  174. }
  175. }
  176. else
  177. {
  178. //!InString, !inbool
  179. if (c == '"')
  180. {
  181. buf = "";
  182. inString = true;
  183. }
  184. else if (c == 't' || c == 'f')
  185. {
  186. buf = c;
  187. inBool = true;
  188. }
  189. else if (c == '{' || c == '[' || c == '}' || c == ']' || c == ':' || c == ',')
  190. {
  191. buf = c;
  192. return nullptr;
  193. }
  194. else if ((c >= '0' && c <= '9') || c == '.')
  195. {
  196. buf = c;
  197. inNumber = true;
  198. }
  199. else if (!ignoreChar(c))
  200. throw JsonFormatException(stream.tellg(), history);
  201. }
  202. }
  203. buf = "";
  204. return nullptr;
  205. }
  206. bool StreamConsumer::ignoreChar(char c) const noexcept
  207. {
  208. return (c <= 32 || c >= 127 || c == '\n');
  209. }