streamConsumer.cpp 5.6 KB

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