JSON.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. #include "JSON.h"
  2. #include "File.h"
  3. using namespace Framework;
  4. using namespace JSON;
  5. #pragma region JSONValue
  6. JSONValue::JSONValue()
  7. : ReferenceCounter()
  8. {
  9. this->type = AbstractType::NULL_;
  10. }
  11. JSONValue::~JSONValue() {}
  12. JSONValue::JSONValue(AbstractType type)
  13. : ReferenceCounter()
  14. {
  15. this->type = type;
  16. }
  17. AbstractType JSONValue::getType() const
  18. {
  19. return type;
  20. }
  21. Text JSONValue::toString() const
  22. {
  23. return Text("null");
  24. }
  25. JSONValue* JSONValue::clone() const
  26. {
  27. return new JSONValue();
  28. }
  29. JSONBool* JSONValue::asBool() const
  30. {
  31. return (JSONBool*)this;
  32. }
  33. JSONNumber* JSONValue::asNumber() const
  34. {
  35. return (JSONNumber*)this;
  36. }
  37. JSONString* JSONValue::asString() const
  38. {
  39. return (JSONString*)this;
  40. }
  41. JSONArray* JSONValue::asArray() const
  42. {
  43. return (JSONArray*)this;
  44. }
  45. JSONObject* JSONValue::asObject() const
  46. {
  47. return (JSONObject*)this;
  48. }
  49. const AbstractBool* Framework::JSON::JSONValue::asAbstractBool() const
  50. {
  51. return dynamic_cast<const AbstractBool*>(this);
  52. }
  53. const AbstractNumber* Framework::JSON::JSONValue::asAbstractNumber() const
  54. {
  55. return dynamic_cast<const AbstractNumber*>(this);
  56. }
  57. const AbstractString* Framework::JSON::JSONValue::asAbstractString() const
  58. {
  59. return dynamic_cast<const AbstractString*>(this);
  60. }
  61. const AbstractArray* Framework::JSON::JSONValue::asAbstractArray() const
  62. {
  63. return dynamic_cast<const AbstractArray*>(this);
  64. }
  65. const AbstractObject* Framework::JSON::JSONValue::asAbstractObject() const
  66. {
  67. return dynamic_cast<const AbstractObject*>(this);
  68. }
  69. #pragma endregion Cotent
  70. #pragma region JSONBool
  71. JSONBool::JSONBool(bool b)
  72. : JSONValue(AbstractType::BOOLEAN)
  73. {
  74. this->b = b;
  75. }
  76. bool JSONBool::getBool() const
  77. {
  78. return b;
  79. }
  80. Text JSONBool::toString() const
  81. {
  82. if (b)
  83. return Text("true");
  84. else
  85. return Text("false");
  86. }
  87. JSONValue* JSONBool::clone() const
  88. {
  89. return new JSONBool(b);
  90. }
  91. #pragma endregion Cotent
  92. #pragma region JSONNumber
  93. JSONNumber::JSONNumber(double num)
  94. : JSONValue(AbstractType::NUMBER)
  95. {
  96. number = num;
  97. }
  98. double JSONNumber::getNumber() const
  99. {
  100. return number;
  101. }
  102. Text JSONNumber::toString() const
  103. {
  104. return Text(number);
  105. }
  106. JSONValue* JSONNumber::clone() const
  107. {
  108. return new JSONNumber(number);
  109. }
  110. #pragma endregion Cotent
  111. #pragma region JSONString
  112. JSONString::JSONString(Text string)
  113. : JSONValue(AbstractType::STRING)
  114. {
  115. this->string = string;
  116. this->string.replace("\\\"", "\"");
  117. this->string.replace("\\n", "\n");
  118. }
  119. Text JSONString::getString() const
  120. {
  121. return string;
  122. }
  123. Text JSONString::toString() const
  124. {
  125. Text esc = string;
  126. esc.replace("\"", "\\\"");
  127. esc.replace("\n", "\\n");
  128. return Text(Text("\"") += esc.getText()) += "\"";
  129. }
  130. JSONValue* JSONString::clone() const
  131. {
  132. Text esc = string;
  133. esc.replace("\"", "\\\"");
  134. esc.replace("\n", "\\n");
  135. return new JSONString(esc);
  136. }
  137. #pragma endregion Cotent
  138. #pragma region JSONArray
  139. JSONArray::JSONArray()
  140. : JSONValue(AbstractType::ARRAY)
  141. {
  142. array = new RCArray<JSONValue>();
  143. }
  144. JSONArray::JSONArray(Text string)
  145. : JSONValue(AbstractType::ARRAY)
  146. {
  147. array = new RCArray<JSONValue>();
  148. string = Parser::removeWhitespace(string);
  149. if (string.getText()[0] == '['
  150. && string.getText()[string.getLength() - 1] == ']')
  151. {
  152. string.remove(0, 1);
  153. string.remove(string.getLength() - 1, string.getLength());
  154. while (string.getLength())
  155. {
  156. int end = Parser::findObjectEndInArray(string);
  157. Text* objStr = string.getTeilText(0, end);
  158. string.remove(0, end + 1);
  159. array->add(Parser::getValue(*objStr));
  160. objStr->release();
  161. }
  162. }
  163. }
  164. JSONArray::JSONArray(const JSONArray& arr)
  165. : JSONValue(AbstractType::ARRAY)
  166. {
  167. array = dynamic_cast<RCArray<JSONValue>*>(arr.array->getThis());
  168. }
  169. JSONArray::~JSONArray()
  170. {
  171. array->release();
  172. }
  173. JSONArray& JSONArray::operator=(const JSONArray& arr)
  174. {
  175. array->release();
  176. array = dynamic_cast<RCArray<JSONValue>*>(arr.array->getThis());
  177. return *this;
  178. }
  179. void JSONArray::addValue(JSONValue* value)
  180. {
  181. array->add(value);
  182. }
  183. void JSONArray::setValue(int i, JSONValue* value)
  184. {
  185. array->set(value, i);
  186. }
  187. void JSONArray::removeValue(int i)
  188. {
  189. array->remove(i);
  190. }
  191. JSONValue* JSONArray::getValue(int i) const
  192. {
  193. return array->get(i);
  194. }
  195. JSONValue* JSONArray::zValue(int i) const
  196. {
  197. return array->z(i);
  198. }
  199. AbstractElement* JSONArray::zAbstractValue(int i) const
  200. {
  201. return zValue(i);
  202. }
  203. int JSONArray::getLength() const
  204. {
  205. return array->getEntryCount();
  206. }
  207. bool JSONArray::isValueOfType(int i, AbstractType type) const
  208. {
  209. return i >= 0 && i < array->getEntryCount()
  210. && array->z(i)->getType() == type;
  211. }
  212. ArrayIterator<JSONValue*> JSONArray::begin() const
  213. {
  214. return array->begin();
  215. }
  216. ArrayIterator<JSONValue*> JSONArray::end() const
  217. {
  218. return array->end();
  219. }
  220. Text JSONArray::toString() const
  221. {
  222. Text str = "[";
  223. for (auto i = array->begin(); i; i++)
  224. {
  225. str += i->toString();
  226. if (i.hasNext()) str += ",";
  227. }
  228. str += "]";
  229. return str;
  230. }
  231. JSONValue* JSONArray::clone() const
  232. {
  233. JSONArray* arr = new JSONArray();
  234. for (JSONValue* value : *this)
  235. {
  236. arr->addValue(value->clone());
  237. }
  238. return arr;
  239. }
  240. #pragma endregion Cotent
  241. #pragma region JSONObject
  242. JSONObject::JSONObject()
  243. : JSONValue(AbstractType::OBJECT)
  244. {
  245. fields = new Array<Text>();
  246. values = new RCArray<JSONValue>();
  247. }
  248. JSONObject::JSONObject(Text string)
  249. : JSONValue(AbstractType::OBJECT)
  250. {
  251. fields = new Array<Text>();
  252. values = new RCArray<JSONValue>();
  253. string = Parser::removeWhitespace(string);
  254. if (string.getText()[0] == '{'
  255. && string.getText()[string.getLength() - 1] == '}')
  256. {
  257. string.remove(0, 1);
  258. string.remove(string.getLength() - 1, string.getLength());
  259. while (string.getLength())
  260. {
  261. int endField = Parser::findFieldEndInObject(string);
  262. Text* fieldName = string.getTeilText(0, endField);
  263. string.remove(0, endField + 1);
  264. fieldName->remove(0, 1);
  265. fieldName->remove(
  266. fieldName->getLength() - 1, fieldName->getLength());
  267. int endValue = Parser::findValueEndInObject(string);
  268. Text* value = string.getTeilText(0, endValue);
  269. string.remove(0, endValue + 1);
  270. fields->add(Text(fieldName->getText()));
  271. values->add(Parser::getValue(*value));
  272. fieldName->release();
  273. value->release();
  274. }
  275. }
  276. }
  277. JSONObject::JSONObject(const JSONObject& obj)
  278. : JSONValue(AbstractType::OBJECT)
  279. {
  280. fields = dynamic_cast<Array<Text>*>(obj.fields->getThis());
  281. values = dynamic_cast<RCArray<JSONValue>*>(obj.values->getThis());
  282. }
  283. JSONObject::~JSONObject()
  284. {
  285. fields->release();
  286. values->release();
  287. }
  288. JSONObject& JSONObject::operator=(const JSONObject& obj)
  289. {
  290. fields->release();
  291. values->release();
  292. fields = dynamic_cast<Array<Text>*>(obj.fields->getThis());
  293. values = dynamic_cast<RCArray<JSONValue>*>(obj.values->getThis());
  294. return *this;
  295. }
  296. bool JSONObject::addValue(Text field, JSONValue* value)
  297. {
  298. if (hasValue(field))
  299. {
  300. value->release();
  301. return 0;
  302. }
  303. fields->add(field);
  304. values->add(value);
  305. return 1;
  306. }
  307. bool JSONObject::removeValue(Text field)
  308. {
  309. for (int i = 0; i < fields->getEntryCount(); i++)
  310. {
  311. if (fields->get(i).isEqual(field))
  312. {
  313. fields->remove(i);
  314. values->remove(i);
  315. return 1;
  316. }
  317. }
  318. return 0;
  319. }
  320. bool JSONObject::hasValue(Text field) const
  321. {
  322. return zValue(field);
  323. }
  324. JSONValue* JSONObject::getValue(Text field) const
  325. {
  326. for (int i = 0; i < fields->getEntryCount(); i++)
  327. {
  328. if (fields->get(i).isEqual(field)) return values->get(i);
  329. }
  330. return 0;
  331. }
  332. JSONValue* JSONObject::zValue(Text field) const
  333. {
  334. for (int i = 0; i < fields->getEntryCount(); i++)
  335. {
  336. if (fields->get(i).isEqual(field)) return values->z(i);
  337. }
  338. return 0;
  339. }
  340. AbstractElement* JSONObject::zAbstractValue(Text field) const
  341. {
  342. return zValue(field);
  343. }
  344. ArrayIterator<Text> JSONObject::getFields()
  345. {
  346. return fields->begin();
  347. }
  348. ArrayIterator<JSONValue*> JSONObject::getValues()
  349. {
  350. return values->begin();
  351. }
  352. Text Framework::JSON::JSONObject::getFieldKey(int i) const
  353. {
  354. return fields->get(i);
  355. }
  356. AbstractElement* Framework::JSON::JSONObject::zAbstractValue(int i) const
  357. {
  358. return values->z(i);
  359. }
  360. int JSONObject::getFieldCount() const
  361. {
  362. return fields->getEntryCount();
  363. }
  364. bool JSONObject::isValueOfType(Text field, AbstractType type) const
  365. {
  366. for (int i = 0; i < fields->getEntryCount(); i++)
  367. {
  368. if (fields->get(i).isEqual(field))
  369. return values->z(i)->getType() == type;
  370. }
  371. return 0;
  372. }
  373. Text JSONObject::toString() const
  374. {
  375. Text str = "{";
  376. ArrayIterator<Text> k = fields->begin();
  377. for (auto v = values->begin(); k && v; k++, v++)
  378. {
  379. str += "\"";
  380. str += k._.getText();
  381. str += "\":";
  382. str += v->toString().getText();
  383. if (v.hasNext()) str += ",";
  384. }
  385. str += "}";
  386. return str;
  387. }
  388. JSONValue* JSONObject::clone() const
  389. {
  390. JSONObject* obj = new JSONObject();
  391. auto field = fields->begin();
  392. auto value = values->begin();
  393. for (; field && value; field++, value++)
  394. {
  395. obj->addValue(field.val(), value->clone());
  396. }
  397. return obj;
  398. }
  399. #pragma endregion Cotent
  400. JSONValue* JSON::loadJSONFromFile(Text path)
  401. {
  402. File d;
  403. d.setFile(path);
  404. if (!d.open(File::Style::read))
  405. {
  406. return new JSONValue();
  407. }
  408. int size = (int)d.getSize();
  409. char* buffer = new char[size + 1];
  410. buffer[size] = 0;
  411. d.read(buffer, size);
  412. d.close();
  413. JSONValue* result = Parser::getValue(buffer);
  414. delete[] buffer;
  415. return result;
  416. }
  417. #pragma region Parser
  418. int Parser::findObjectEndInArray(const char* str)
  419. {
  420. return findValueEndInObject(str);
  421. }
  422. Text Parser::removeWhitespace(const char* str)
  423. {
  424. int i = 0;
  425. bool esc = 0;
  426. bool strO = 0;
  427. int l = 0;
  428. for (; str[i]; i++)
  429. {
  430. switch (str[i])
  431. {
  432. case '\\':
  433. if (strO)
  434. esc = !esc;
  435. else
  436. esc = 0;
  437. l++;
  438. break;
  439. case '"':
  440. if (!esc) strO = !strO;
  441. esc = 0;
  442. l++;
  443. break;
  444. case ' ':
  445. case '\n':
  446. case '\t':
  447. case '\r':
  448. if (strO) l++;
  449. esc = 0;
  450. break;
  451. default:
  452. esc = 0;
  453. l++;
  454. break;
  455. }
  456. }
  457. char* result = new char[l + 1];
  458. i = 0;
  459. esc = 0;
  460. strO = 0;
  461. l = 0;
  462. for (; str[i]; i++)
  463. {
  464. switch (str[i])
  465. {
  466. case '\\':
  467. if (strO)
  468. esc = !esc;
  469. else
  470. esc = 0;
  471. result[l++] = str[i];
  472. break;
  473. case '"':
  474. if (!esc) strO = !strO;
  475. esc = 0;
  476. result[l++] = str[i];
  477. break;
  478. case ' ':
  479. case '\n':
  480. case '\t':
  481. case '\r':
  482. if (strO) result[l++] = str[i];
  483. esc = 0;
  484. break;
  485. default:
  486. result[l++] = str[i];
  487. esc = 0;
  488. break;
  489. }
  490. }
  491. result[l] = 0;
  492. return Text::fromArray(result, l);
  493. }
  494. JSONValue* Parser::getValue(const char* str)
  495. {
  496. Text string = Parser::removeWhitespace(str);
  497. if (string.isEqual("true")) return new JSONBool(1);
  498. if (string.isEqual("false")) return new JSONBool(0);
  499. if (string.getText()[0] == '"')
  500. {
  501. string.remove(0, 1);
  502. string.remove(string.getLength() - 1, string.getLength());
  503. return new JSONString(string);
  504. }
  505. if (string.getText()[0] == '[') return new JSONArray(string);
  506. if (string.getText()[0] == '{') return new JSONObject(string);
  507. if (Text((int)string).isEqual(string.getText()))
  508. return new JSONNumber((double)string);
  509. if (string.countOf('.') == 1)
  510. {
  511. bool isNumber = 1;
  512. for (const char* c = (*string.getText() == '-') ? string.getText() + 1
  513. : string.getText();
  514. *c;
  515. c++)
  516. isNumber &= (*c >= '0' && *c <= '9') || *c == '.';
  517. if (isNumber) return new JSONNumber((double)string);
  518. }
  519. return new JSONValue();
  520. }
  521. int Parser::findFieldEndInObject(const char* str)
  522. {
  523. int i = 0;
  524. bool esc = 0;
  525. bool strO = 0;
  526. int objOc = 0;
  527. int arrayOc = 0;
  528. for (; str[i]; i++)
  529. {
  530. switch (str[i])
  531. {
  532. case '\\':
  533. if (strO)
  534. esc = !esc;
  535. else
  536. esc = 0;
  537. break;
  538. case '"':
  539. if (!esc) strO = !strO;
  540. esc = 0;
  541. break;
  542. case '[':
  543. if (!strO) arrayOc++;
  544. esc = 0;
  545. break;
  546. case ']':
  547. if (!strO) arrayOc--;
  548. esc = 0;
  549. break;
  550. case '{':
  551. if (!strO) objOc++;
  552. esc = 0;
  553. break;
  554. case '}':
  555. if (!strO) objOc--;
  556. esc = 0;
  557. break;
  558. case ':':
  559. if (!strO && objOc == 0 && arrayOc == 0) return i;
  560. esc = 0;
  561. break;
  562. default:
  563. esc = 0;
  564. break;
  565. }
  566. }
  567. return i;
  568. }
  569. int Parser::findValueEndInObject(const char* str)
  570. {
  571. int i = 0;
  572. bool esc = 0;
  573. bool strO = 0;
  574. int objOc = 0;
  575. int arrayOc = 0;
  576. for (; str[i]; i++)
  577. {
  578. switch (str[i])
  579. {
  580. case '\\':
  581. if (strO)
  582. esc = !esc;
  583. else
  584. esc = 0;
  585. break;
  586. case '"':
  587. if (!esc) strO = !strO;
  588. esc = 0;
  589. break;
  590. case '[':
  591. if (!strO) arrayOc++;
  592. esc = 0;
  593. break;
  594. case ']':
  595. if (!strO) arrayOc--;
  596. esc = 0;
  597. break;
  598. case '{':
  599. if (!strO) objOc++;
  600. esc = 0;
  601. break;
  602. case '}':
  603. if (!strO) objOc--;
  604. esc = 0;
  605. break;
  606. case ',':
  607. if (!strO && objOc == 0 && arrayOc == 0) return i;
  608. esc = 0;
  609. break;
  610. default:
  611. esc = 0;
  612. break;
  613. }
  614. }
  615. return i;
  616. }
  617. #pragma endregion Cotent