microjson.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #ifdef MICROJSON_DEBUG
  28. #define microjsonDebug std::cout
  29. #else
  30. struct microjsonNull : public std::ostream {
  31. class nullBuffer : public std::streambuf {};
  32. microjsonNull() : std::ostream(new nullBuffer) {}
  33. };
  34. static microjsonNull nullout;
  35. #define microjsonDebug nullout
  36. #endif
  37. namespace {
  38. template<const char expectedBeginByte>
  39. void lookForBoundaries(const char *buffer, size_t size, size_t &begin, size_t &end) {
  40. const char expectedEndByte = expectedBeginByte + 2;
  41. begin = SIZE_MAX;
  42. end = SIZE_MAX;
  43. for (size_t i = 0; i < size; i++) {
  44. const char beginByte = buffer[i];
  45. const char endByte = buffer[size - i - 1];
  46. if (begin == SIZE_MAX) {
  47. if (beginByte == expectedBeginByte) {
  48. begin = i;
  49. } else if (beginByte != '\n' && beginByte != ' ' && beginByte != '\r' && beginByte != '\t') {
  50. std::cerr << "Unexpected begin byte" << beginByte << std::endl;
  51. break;
  52. }
  53. }
  54. if (end == SIZE_MAX) {
  55. if (endByte == expectedEndByte) {
  56. end = size - i - 1;
  57. } else if (endByte != '\n' && endByte != ' ' && endByte != '\r' && endByte != '\t') {
  58. std::cerr << "Unexpected end byte" << endByte << std::endl;
  59. break;
  60. }
  61. }
  62. if (begin != SIZE_MAX && end != SIZE_MAX) {
  63. break;
  64. }
  65. }
  66. }
  67. void lookForName(const char *buffer, size_t size, size_t &i, microjson::JsonProperty &property) {
  68. property.nameBegin = SIZE_MAX;
  69. property.nameEnd = SIZE_MAX;
  70. bool beginFound = false;
  71. for(; i < size; ++i) {
  72. const char byte = buffer[i];
  73. if (microjson::skipWhiteSpace(byte)) {
  74. microjsonDebug << "Skip space" << std::endl;
  75. continue;
  76. }
  77. if(!beginFound) {
  78. if (byte == '"') {
  79. microjsonDebug << "Found name begin" << std::endl;
  80. property.nameBegin = i + 1;
  81. beginFound = true;
  82. } else {
  83. std::cerr << "Not found name begin, unexpected" << std::endl;
  84. break;
  85. }
  86. } else if(byte == '"') {
  87. if (i > 0 && buffer[i - 1] == '\\') {
  88. microjsonDebug << "'\"' found in name" << std::endl;
  89. continue;
  90. }
  91. microjsonDebug << "Found name end" << std::endl;
  92. property.nameEnd = i;
  93. break;
  94. }
  95. }
  96. ++i;
  97. }
  98. bool lookForSeparator(const char* buffer, size_t size, size_t &i) {
  99. bool found = false;
  100. for(; i < size; ++i) {
  101. const char byte = buffer[i];
  102. if (microjson::skipWhiteSpace(byte)) {
  103. microjsonDebug << "Skip space" << std::endl;
  104. continue;
  105. }
  106. if (byte == ':') {
  107. microjsonDebug << "Found Separator" << std::endl;
  108. found = true;
  109. }
  110. break;
  111. }
  112. ++i;
  113. return found;
  114. }
  115. void lookForValue(const char *buffer, size_t size, size_t &i, microjson::JsonProperty &property) {
  116. int valueBracesCounter = -1;
  117. int valueBracketsCounter = -1;
  118. int trueCounter = 3;
  119. int falseCounter = 4;
  120. int nullCounter = 3;
  121. std::function<bool(void)> valueEndMarker;
  122. bool beginFound = false;
  123. bool stringScope = false;
  124. property.valueBegin = SIZE_MAX;
  125. property.valueEnd = SIZE_MAX;
  126. for(; i < size; i++) {
  127. const char byte = buffer[i];
  128. if (!beginFound) {
  129. microjsonDebug << "lookForValue at: " << i << " byte: " << byte << std::endl;
  130. if (microjson::skipWhiteSpace(byte)) {
  131. microjsonDebug << "Skip space" << std::endl;
  132. continue;
  133. }
  134. switch (byte) {
  135. case '"':
  136. valueEndMarker = [&](){
  137. return buffer[i] == '"' && buffer[i - 1] != '\\';
  138. };
  139. property.type = microjson::JsonStringType;
  140. break;
  141. case '-':
  142. case '0':
  143. case '1':
  144. case '2':
  145. case '3':
  146. case '4':
  147. case '5':
  148. case '6':
  149. case '7':
  150. case '8':
  151. case '9':
  152. valueEndMarker = [&buffer, &i](){
  153. if (buffer[i] != '+'
  154. && buffer[i] != '-'
  155. && buffer[i] != '0'
  156. && buffer[i] != '1'
  157. && buffer[i] != '2'
  158. && buffer[i] != '3'
  159. && buffer[i] != '4'
  160. && buffer[i] != '5'
  161. && buffer[i] != '6'
  162. && buffer[i] != '7'
  163. && buffer[i] != '8'
  164. && buffer[i] != '9'
  165. && buffer[i] != 'e'
  166. && buffer[i] != '.') {
  167. i--;
  168. return true;
  169. }
  170. return false;
  171. };
  172. property.type = microjson::JsonNumberType;
  173. break;
  174. case '{':
  175. valueBracesCounter++;
  176. valueEndMarker = [&stringScope, &byte, &valueBracesCounter](){
  177. if (!stringScope) {
  178. switch (byte) {
  179. case '}':
  180. --valueBracesCounter;
  181. break;
  182. case '{':
  183. ++valueBracesCounter;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. return valueBracesCounter < 0;
  190. };
  191. property.type = microjson::JsonObjectType;
  192. break;
  193. case 't':
  194. valueEndMarker = [&trueCounter](){
  195. --trueCounter;
  196. return trueCounter == 0;
  197. };
  198. property.type = microjson::JsonBoolType;
  199. break;
  200. case 'f':
  201. valueEndMarker = [&falseCounter](){
  202. --falseCounter;
  203. return falseCounter == 0;
  204. };
  205. property.type = microjson::JsonBoolType;
  206. break;
  207. case 'n':
  208. valueEndMarker = [&nullCounter](){
  209. --nullCounter;
  210. return nullCounter == 0;
  211. };
  212. property.type = microjson::JsonObjectType;
  213. break;
  214. case '[':
  215. valueBracketsCounter++;
  216. valueEndMarker = [&stringScope, &byte, &valueBracketsCounter](){
  217. if(!stringScope) {
  218. switch(byte) {
  219. case ']':
  220. --valueBracketsCounter;
  221. break;
  222. case '[':
  223. ++valueBracketsCounter;
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. return valueBracketsCounter < 0;
  230. };
  231. property.type = microjson::JsonArrayType;
  232. break;
  233. }
  234. if (valueEndMarker) {
  235. microjsonDebug << "Found value begin" << std::endl;
  236. property.valueBegin = i;
  237. beginFound = true;
  238. } else {
  239. break;
  240. }
  241. } else {
  242. if (byte == '"' && (i == 0 || buffer[i - 1] != '\\')) {
  243. stringScope = !stringScope;
  244. }
  245. if (valueEndMarker()) {
  246. microjsonDebug << "Found value end" << std::endl;
  247. property.valueEnd = i;
  248. break;
  249. }
  250. }
  251. }
  252. if (property.checkEof()) {
  253. property.valueEnd = size - 1; //EOF case
  254. microjsonDebug << "Found value end at EOF" << std::endl;
  255. }
  256. if (property.type == microjson::JsonStringType) {
  257. ++property.valueBegin;
  258. --property.valueEnd;
  259. }
  260. }
  261. void findSeparator(const char *buffer, size_t size, size_t &i, const char expectedEndByte) {
  262. while (++i < size) {
  263. if(!microjson::skipWhiteSpace(buffer[i])) {
  264. break;
  265. }
  266. }
  267. const char endByte = buffer[i];
  268. if (endByte == expectedEndByte) {
  269. if(++i != size) {
  270. i = SIZE_MAX;
  271. }
  272. } else if(endByte == ',') {
  273. ++i;
  274. } else {
  275. i = SIZE_MAX;
  276. }
  277. }
  278. using Extractor = bool(*)(const char *, size_t, size_t &, const char, microjson::JsonProperty &);
  279. template<typename R,
  280. const char expectedBeginByte,
  281. Extractor extract,
  282. void(* collect)(const char *, R &, const microjson::JsonProperty &property)>
  283. R parseJsonCommon(const char *buffer, size_t size) {
  284. R returnValue;
  285. if (buffer == nullptr || size == 0 || size == SIZE_MAX) {
  286. return returnValue;
  287. }
  288. size_t objectBeginPosition = SIZE_MAX;
  289. size_t objectEndPosition = SIZE_MAX;
  290. lookForBoundaries<expectedBeginByte>(buffer, size, objectBeginPosition, objectEndPosition);
  291. if (objectBeginPosition == SIZE_MAX || objectEndPosition == SIZE_MAX) {
  292. return returnValue;
  293. }
  294. microjson::JsonProperty property;
  295. buffer += objectBeginPosition + 1;//Skip '{'
  296. size = objectEndPosition - objectBeginPosition;//Do not skip '}'
  297. microjsonDebug << "Object buffer size: " << size << " buffer: " << std::string(buffer, size) << std::endl;
  298. size_t nextPosition = 0;
  299. while (nextPosition < size) {
  300. microjsonDebug << "nextPropertyPosition: " << nextPosition << "size: " << size << std::endl;
  301. if(extract(buffer, size, nextPosition, expectedBeginByte + 2, property)) {
  302. collect(buffer, returnValue, property);
  303. }
  304. }
  305. return returnValue;
  306. }
  307. void appendProperty(const char* buffer, microjson::JsonObject &obj, const microjson::JsonProperty &property){
  308. std::string name((buffer + property.nameBegin), property.nameSize());
  309. std::string value;
  310. if(property.valueSize() > 0) {
  311. value = std::string((buffer + property.valueBegin), property.valueSize());
  312. }
  313. microjsonDebug << "name: " << name << " value: " << value << std::endl;
  314. obj[name] = { value, property.type };
  315. };
  316. void appendValue(const char* buffer, microjson::JsonArray &values, const microjson::JsonProperty &property){
  317. std::string value = std::string((buffer + property.valueBegin), property.valueSize());
  318. microjsonDebug << "value: " << value << std::endl;
  319. values.push_back({value, property.type});
  320. };
  321. }
  322. bool microjson::extractValue(const char *buffer, size_t size, size_t &i, const char expectedEndByte, microjson::JsonProperty &property) {
  323. if (size == 0) {
  324. i = SIZE_MAX;
  325. return false;
  326. }
  327. lookForValue(buffer, size, i, property);
  328. findSeparator(buffer, size, i, expectedEndByte);
  329. return property.checkValue();
  330. }
  331. bool microjson::extractProperty(const char *buffer, size_t size, size_t &i, const char expectedEndByte, microjson::JsonProperty &property) {
  332. if (size == 0) {
  333. i = SIZE_MAX;
  334. return false;
  335. }
  336. lookForName(buffer, size, i, property);
  337. if(property.nameBegin == SIZE_MAX && property.nameEnd == SIZE_MAX) {
  338. std::cerr << "Name not found" << std::endl;
  339. return false;
  340. }
  341. microjsonDebug << "Found name: " << std::string(buffer + property.nameBegin, property.nameSize()) << std::endl;
  342. if(!lookForSeparator(buffer, size, i)) {
  343. std::cerr << "Separator not found" << std::endl;
  344. return false;
  345. }
  346. lookForValue(buffer, size, i, property);
  347. microjsonDebug << "Found value: " << std::string(buffer + property.valueBegin, property.valueSize()) << std::endl;
  348. findSeparator(buffer, size, i, expectedEndByte);
  349. return property.check();
  350. }
  351. microjson::JsonObject microjson::parseJsonObject(const char *buffer, size_t size) {
  352. return parseJsonCommon<JsonObject, '{', extractProperty, appendProperty>(buffer, size);
  353. }
  354. microjson::JsonArray microjson::parseJsonArray(const char *buffer, size_t size) {
  355. return parseJsonCommon<JsonArray, '[', extractValue, appendValue>(buffer, size);
  356. }