microjson.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of microjson project https://git.semlanik.org/semlanik/microjson
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "microjson.h"
  26. #include <iostream>
  27. #include <functional>
  28. #ifdef MICROJSON_DEBUG
  29. #define micorJsonDebug std::cout
  30. #else
  31. struct micorJsonNull : public std::ostream {
  32. };
  33. static micorJsonNull nullout;
  34. #define micorJsonDebug nullout
  35. #endif
  36. size_t microjson::extractNextProperty(const char *buffer, size_t size, JsonProperty &property) {
  37. if (buffer == nullptr || size == 0 || size == SIZE_MAX) {
  38. return SIZE_MAX;
  39. }
  40. enum ParsingState {
  41. LookingForNameBegin,
  42. LookingForNameEnd,
  43. LookingForSeparator,
  44. LookingForValueBegin,
  45. LookingForValueEnd
  46. };
  47. property.nameBegin = SIZE_MAX;
  48. property.nameEnd = SIZE_MAX;
  49. property.valueBegin = SIZE_MAX;
  50. property.valueEnd = SIZE_MAX;
  51. property.type = JsonInvalidType;
  52. int valueBracesCounter = -1;
  53. int valueBracketsCounter = -1;
  54. int trueCounter = 3;
  55. int falseCounter = 4;
  56. std::function<bool(void)> valueEndMarker;
  57. ParsingState state = LookingForNameBegin;
  58. size_t i = 0;
  59. for (; i < size; i++) {
  60. const char byte = buffer[i];
  61. if (state == LookingForNameBegin) {
  62. if (byte == '\n' || byte == ' ' || byte == '\r' || byte == '\t') {
  63. micorJsonDebug << "Skip space" << std::endl;
  64. continue;
  65. }
  66. if (byte == '"') {
  67. micorJsonDebug << "Found name begin" << std::endl;
  68. property.nameBegin = i + 1;
  69. state = LookingForNameEnd;
  70. } else {
  71. std::cerr << "Not found name begin, unexpected" << std::endl;
  72. break;
  73. }
  74. } else if (state == LookingForNameEnd && byte == '"') {
  75. if (byte == '\n' || byte == ' ' || byte == '\r' || byte == '\t') {
  76. micorJsonDebug << "Skip space" << std::endl;
  77. continue;
  78. }
  79. if (i > 0 && buffer[i - 1] == '\\') {
  80. micorJsonDebug << "'\"' found in name" << std::endl;
  81. continue;
  82. }
  83. micorJsonDebug << "Found name end" << std::endl;
  84. property.nameEnd = i;
  85. state = LookingForSeparator;
  86. } else if (state == LookingForSeparator) {
  87. if (byte == '\n' || byte == ' ' || byte == '\r' || byte == '\t') {
  88. micorJsonDebug << "Skip space" << std::endl;
  89. continue;
  90. }
  91. if (byte == ':') {
  92. micorJsonDebug << "Found Separator" << std::endl;
  93. state = LookingForValueBegin;
  94. } else {
  95. break;
  96. }
  97. } else if (state == LookingForValueBegin) {
  98. if (byte == '\n' || byte == ' ' || byte == '\r' || byte == '\t') {
  99. micorJsonDebug << "Skip space" << std::endl;
  100. continue;
  101. }
  102. switch (byte) {
  103. case '"':
  104. valueEndMarker = [&](){
  105. return buffer[i] == '"' && buffer[i - 1] != '\\';
  106. };
  107. property.type = JsonStringType;
  108. break;
  109. case '-':
  110. case '0':
  111. case '1':
  112. case '2':
  113. case '3':
  114. case '4':
  115. case '5':
  116. case '6':
  117. case '7':
  118. case '8':
  119. case '9':
  120. valueEndMarker = [&buffer, &i](){
  121. if (buffer[i] != '+'
  122. && buffer[i] != '0'
  123. && buffer[i] != '1'
  124. && buffer[i] != '2'
  125. && buffer[i] != '3'
  126. && buffer[i] != '4'
  127. && buffer[i] != '5'
  128. && buffer[i] != '6'
  129. && buffer[i] != '7'
  130. && buffer[i] != '8'
  131. && buffer[i] != '9'
  132. && buffer[i] != 'e'
  133. && buffer[i] != '.') {
  134. i--;
  135. return true;
  136. }
  137. return false;
  138. };
  139. property.type = JsonNumberType;
  140. break;
  141. case '{':
  142. valueBracesCounter++;
  143. valueEndMarker = [&valueBracesCounter](){
  144. return valueBracesCounter < 0;
  145. };
  146. property.type = JsonObjectType;
  147. break;
  148. case 't':
  149. valueEndMarker = [&trueCounter](){
  150. return trueCounter == 0;
  151. };
  152. property.type = JsonBoolType;
  153. break;
  154. case 'f':
  155. valueEndMarker = [&falseCounter](){
  156. return falseCounter == 0;
  157. };
  158. property.type = JsonBoolType;
  159. break;
  160. case '[':
  161. valueBracketsCounter++;
  162. valueEndMarker = [&valueBracketsCounter](){
  163. return valueBracketsCounter < 0;
  164. };
  165. property.type = JsonArrayType;
  166. break;
  167. }
  168. if (valueEndMarker) {
  169. micorJsonDebug << "Found value begin" << std::endl;
  170. property.valueBegin = i;
  171. state = LookingForValueEnd;
  172. } else {
  173. break;
  174. }
  175. } else if (state == LookingForValueEnd) {
  176. switch (byte) {
  177. case '}':
  178. --valueBracesCounter;
  179. break;
  180. case '{':
  181. --valueBracesCounter;
  182. break;
  183. case ']':
  184. --valueBracketsCounter;
  185. break;
  186. case '[':
  187. ++valueBracketsCounter;
  188. break;
  189. default:
  190. --trueCounter;
  191. --falseCounter;
  192. break;
  193. }
  194. if (valueEndMarker()) {
  195. micorJsonDebug << "Found value end" << std::endl;
  196. property.valueEnd = i;
  197. break;
  198. }
  199. }
  200. }
  201. if (property.eofCheck()) {
  202. property.valueEnd = size - 1; //EOF case
  203. micorJsonDebug << "Found value end at EOF" << std::endl;
  204. }
  205. micorJsonDebug << property.nameBegin << " " << property.nameEnd << " " << property.valueBegin << " " << property.valueEnd << " " << property.type << std::endl;
  206. if (property.check()) {
  207. if (property.type == JsonStringType) {
  208. ++property.valueBegin;
  209. --property.valueEnd;
  210. }
  211. for (size_t j = i + 1; j < size; j++) {
  212. const char &byte = buffer[j];
  213. if (byte == ',') {
  214. return j + 1;
  215. } else if (byte != '\n' && byte != ' ' && byte != '\r' && byte != '\t') {
  216. micorJsonDebug << "Unexpected: " << byte;
  217. return SIZE_MAX;
  218. }
  219. }
  220. return size;
  221. } else {
  222. micorJsonDebug << "Property check failed" << std::endl;
  223. }
  224. return SIZE_MAX;
  225. }
  226. microjson::JsonObject microjson::parseObject(const char *buffer, size_t size) {
  227. JsonObject obj;
  228. if (buffer == nullptr || size == 0 || size == SIZE_MAX) {
  229. return obj;
  230. }
  231. size_t objectBeginPosition = SIZE_MAX;
  232. size_t objectEndPosition = SIZE_MAX;
  233. for (size_t i = 0; i < size; i++) {
  234. const char beginByte = buffer[i];
  235. const char endByte = buffer[size - i - 1];
  236. if (objectBeginPosition == SIZE_MAX) {
  237. if (beginByte == '{') {
  238. objectBeginPosition = i;
  239. } else if (beginByte != '\n' && beginByte != ' ' && beginByte != '\r' && beginByte != '\t') {
  240. std::cerr << "Unexpected begin byte" << beginByte << std::endl;
  241. break;
  242. }
  243. }
  244. if (objectEndPosition == SIZE_MAX) {
  245. if (endByte == '}') {
  246. objectEndPosition = size - i - 1;
  247. } else if (endByte != '\n' && endByte != ' ' && endByte != '\r' && endByte != '\t') {
  248. std::cerr << "Unexpected end byte" << endByte << std::endl;
  249. break;
  250. }
  251. }
  252. if (objectBeginPosition != SIZE_MAX && objectEndPosition != SIZE_MAX) {
  253. break;
  254. }
  255. }
  256. if (objectBeginPosition == SIZE_MAX || objectEndPosition == SIZE_MAX) {
  257. return obj;
  258. }
  259. JsonProperty property;
  260. buffer += objectBeginPosition + 1;//Skip '{'
  261. size = objectEndPosition - objectBeginPosition - 1;//Skip '}'
  262. micorJsonDebug << "Object buffer size: " << size << " buffer: " << std::string(buffer, size) << std::endl;
  263. for (size_t nextPropertyPosition = microjson::extractNextProperty(buffer, size, property); nextPropertyPosition != SIZE_MAX; ) {
  264. micorJsonDebug << "nextPropertyPosition: " << nextPropertyPosition << "size: " << size << std::endl;
  265. if (nextPropertyPosition != SIZE_MAX) {
  266. std::string name((buffer + property.nameBegin), property.nameSize());
  267. std::string value((buffer + property.valueBegin), property.valueSize());
  268. micorJsonDebug << "name: " << name << " value: " << value << std::endl;
  269. obj[name] = { value, property.type };
  270. }
  271. buffer += nextPropertyPosition;
  272. size = size - nextPropertyPosition;
  273. nextPropertyPosition = microjson::extractNextProperty(buffer, size, property);
  274. }
  275. return obj;
  276. }