streamConsumer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 JsonException(stream.tellg());
  54. if (consumeToken(parent, buf) != nullptr || buf != ":")
  55. throw JsonException(stream.tellg());
  56. if (result == nullptr)
  57. result = new JSonObject(parent);
  58. else if (result->contains(key->getValue()))
  59. throw JsonException(stream.tellg()); //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)
  77. throw JsonException(stream.tellg());
  78. if (result == nullptr)
  79. result = new JSonArray(parent);
  80. child->setParent(result);
  81. result->push_back(child);
  82. child = consumeToken(result, buf);
  83. if (child != nullptr)
  84. throw JsonException(stream.tellg());
  85. else if (buf == "]")
  86. break;
  87. else if (buf != ",")
  88. throw JsonException(stream.tellg());
  89. child = consumeToken(result, buf);
  90. } while (true);
  91. return result;
  92. }
  93. JSonElement *StreamConsumer::consumeToken(JSonContainer *parent, std::string &buf)
  94. {
  95. bool escaped = false;
  96. bool inString = false;
  97. bool inBool = false;
  98. bool inNumber = false;
  99. bool numberIsFloat = false;
  100. while (stream.good())
  101. {
  102. char c = stream.get();
  103. if (inString)
  104. {
  105. if (!escaped)
  106. {
  107. if (c == '"')
  108. return new JSonPrimitive<std::string>(parent, buf);
  109. else if (c == '\\')
  110. escaped = true;
  111. else
  112. buf += c;
  113. }
  114. else
  115. {
  116. if (c == '\\' || c == '"')
  117. buf += c;
  118. else
  119. throw JsonEscapedException(c, stream.tellg());
  120. }
  121. }
  122. else if (inBool)
  123. {
  124. if (c == 'a' || c == 'e' || c == 'l' || c == 'r' || c == 's' || c == 'u')
  125. buf += c;
  126. else if (buf == "true")
  127. {
  128. stream.unget();
  129. return new JSonPrimitive<bool>(parent, true);
  130. }
  131. else if (buf == "false")
  132. {
  133. stream.unget();
  134. return new JSonPrimitive<bool>(parent, false);
  135. }
  136. else if (ignoreChar(c))
  137. ;
  138. else
  139. throw JsonFormatException(c, stream.tellg());
  140. }
  141. else if (inNumber)
  142. {
  143. if (c >= '0' && c <= '9')
  144. buf += c;
  145. else if (c == '.' && !numberIsFloat)
  146. {
  147. numberIsFloat = true;
  148. buf += c;
  149. }
  150. else
  151. {
  152. stream.unget();
  153. if (numberIsFloat)
  154. return new JSonPrimitive<float>(parent, std::stof(buf));
  155. try
  156. {
  157. return new JSonPrimitive<int>(parent, std::stoi(buf));
  158. }
  159. catch(std::out_of_range e)
  160. {
  161. return new JSonPrimitive<long long>(parent, std::stol(buf));
  162. }
  163. }
  164. }
  165. else
  166. {
  167. //!InString, !inbool
  168. if (c == '"')
  169. {
  170. buf = "";
  171. inString = true;
  172. }
  173. else if (c == 't' || c == 'f')
  174. {
  175. buf = c;
  176. inBool = true;
  177. }
  178. else if (c == '{' || c == '[' || c == '}' || c == ']' || c == ':' || c == ',')
  179. {
  180. buf = c;
  181. return nullptr;
  182. }
  183. else if ((c >= '0' && c <= '9') || c == '.')
  184. {
  185. buf = c;
  186. inNumber = true;
  187. }
  188. else if (!ignoreChar(c))
  189. throw JsonFormatException(c, stream.tellg());
  190. }
  191. }
  192. buf = "";
  193. return nullptr;
  194. }
  195. bool StreamConsumer::ignoreChar(char c) const noexcept
  196. {
  197. return (c <= 32 || c >= 127 || c == '\n');
  198. }