JSON.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #include "JSON.h"
  2. #include "Datei.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.ersetzen("\\\"", "\"");
  117. this->string.ersetzen("\\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.ersetzen("\"", "\\\"");
  127. esc.ersetzen("\n", "\\n");
  128. return Text(Text("\"") += esc.getText()) += "\"";
  129. }
  130. JSONValue* JSONString::clone() const
  131. {
  132. Text esc = string;
  133. esc.ersetzen("\"", "\\\"");
  134. esc.ersetzen("\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->getEintragAnzahl();
  206. }
  207. bool JSONArray::isValueOfType(int i, AbstractType type) const
  208. {
  209. return i >= 0 && i < array->getEintragAnzahl()
  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)) return 0;
  299. fields->add(field);
  300. values->add(value);
  301. return 1;
  302. }
  303. bool JSONObject::removeValue(Text field)
  304. {
  305. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  306. {
  307. if (fields->get(i).istGleich(field))
  308. {
  309. fields->remove(i);
  310. values->remove(i);
  311. return 1;
  312. }
  313. }
  314. return 0;
  315. }
  316. bool JSONObject::hasValue(Text field) const
  317. {
  318. return zValue(field);
  319. }
  320. JSONValue* JSONObject::getValue(Text field) const
  321. {
  322. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  323. {
  324. if (fields->get(i).istGleich(field)) return values->get(i);
  325. }
  326. return 0;
  327. }
  328. JSONValue* JSONObject::zValue(Text field) const
  329. {
  330. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  331. {
  332. if (fields->get(i).istGleich(field)) return values->z(i);
  333. }
  334. return 0;
  335. }
  336. AbstractElement* JSONObject::zAbstractValue(Text field) const
  337. {
  338. return zValue(field);
  339. }
  340. ArrayIterator<Text> JSONObject::getFields()
  341. {
  342. return fields->begin();
  343. }
  344. ArrayIterator<JSONValue*> JSONObject::getValues()
  345. {
  346. return values->begin();
  347. }
  348. Text Framework::JSON::JSONObject::getFieldKey(int i) const
  349. {
  350. return fields->get(i);
  351. }
  352. AbstractElement* Framework::JSON::JSONObject::zAbstractValue(int i) const
  353. {
  354. return values->z(i);
  355. }
  356. int JSONObject::getFieldCount() const
  357. {
  358. return fields->getEintragAnzahl();
  359. }
  360. bool JSONObject::isValueOfType(Text field, AbstractType type) const
  361. {
  362. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  363. {
  364. if (fields->get(i).istGleich(field))
  365. return values->z(i)->getType() == type;
  366. }
  367. return 0;
  368. }
  369. Text JSONObject::toString() const
  370. {
  371. Text str = "{";
  372. ArrayIterator<Text> k = fields->begin();
  373. for (auto v = values->begin(); k && v; k++, v++)
  374. {
  375. str += "\"";
  376. str += k._.getText();
  377. str += "\":";
  378. str += v->toString().getText();
  379. if (v.hasNext()) str += ",";
  380. }
  381. str += "}";
  382. return str;
  383. }
  384. JSONValue* JSONObject::clone() const
  385. {
  386. JSONObject* obj = new JSONObject();
  387. auto field = fields->begin();
  388. auto value = values->begin();
  389. for (; field && value; field++, value++)
  390. {
  391. obj->addValue(field.val(), value->clone());
  392. }
  393. return obj;
  394. }
  395. #pragma endregion Cotent
  396. JSONValue* JSON::loadJSONFromFile(Text path)
  397. {
  398. Datei d;
  399. d.setDatei(path);
  400. if (!d.open(Datei::Style::lesen))
  401. {
  402. return new JSONValue();
  403. }
  404. int size = (int)d.getSize();
  405. char* buffer = new char[size + 1];
  406. buffer[size] = 0;
  407. d.lese(buffer, size);
  408. d.close();
  409. JSONValue* result = Parser::getValue(buffer);
  410. delete[] buffer;
  411. return result;
  412. }
  413. #pragma region Parser
  414. int Parser::findObjectEndInArray(const char* str)
  415. {
  416. return findValueEndInObject(str);
  417. }
  418. Text Parser::removeWhitespace(const char* str)
  419. {
  420. int wsc = 0;
  421. int i = 0;
  422. bool esc = 0;
  423. bool strO = 0;
  424. for (; str[i]; i++)
  425. {
  426. switch (str[i])
  427. {
  428. case '\\':
  429. if (strO)
  430. esc = !esc;
  431. else
  432. esc = 0;
  433. break;
  434. case '"':
  435. if (!esc) strO = !strO;
  436. esc = 0;
  437. break;
  438. case ' ':
  439. case '\n':
  440. case '\t':
  441. case '\r':
  442. if (!strO) wsc++;
  443. esc = 0;
  444. break;
  445. default:
  446. esc = 0;
  447. break;
  448. }
  449. }
  450. Text ret;
  451. i = 0;
  452. esc = 0;
  453. strO = 0;
  454. for (; str[i]; i++)
  455. {
  456. switch (str[i])
  457. {
  458. case '\\':
  459. if (strO)
  460. esc = !esc;
  461. else
  462. esc = 0;
  463. ret.append(str[i]);
  464. break;
  465. case '"':
  466. if (!esc) strO = !strO;
  467. esc = 0;
  468. ret.append(str[i]);
  469. break;
  470. case ' ':
  471. case '\n':
  472. case '\t':
  473. case '\r':
  474. if (strO) ret.append(str[i]);
  475. esc = 0;
  476. break;
  477. default:
  478. ret.append(str[i]);
  479. esc = 0;
  480. break;
  481. }
  482. }
  483. return ret;
  484. }
  485. JSONValue* Parser::getValue(const char* str)
  486. {
  487. Text string = Parser::removeWhitespace(str);
  488. if (string.istGleich("true")) return new JSONBool(1);
  489. if (string.istGleich("false")) return new JSONBool(0);
  490. if (string.getText()[0] == '"')
  491. {
  492. string.remove(0, 1);
  493. string.remove(string.getLength() - 1, string.getLength());
  494. return new JSONString(string);
  495. }
  496. if (string.getText()[0] == '[') return new JSONArray(string);
  497. if (string.getText()[0] == '{') return new JSONObject(string);
  498. if (Text((int)string).istGleich(string.getText()))
  499. return new JSONNumber((double)string);
  500. if (string.anzahlVon('.') == 1)
  501. {
  502. bool isNumber = 1;
  503. for (const char* c = (*string.getText() == '-') ? string.getText() + 1
  504. : string.getText();
  505. *c;
  506. c++)
  507. isNumber &= (*c >= '0' && *c <= '9') || *c == '.';
  508. if (isNumber) return new JSONNumber((double)string);
  509. }
  510. return new JSONValue();
  511. }
  512. int Parser::findFieldEndInObject(const char* str)
  513. {
  514. int i = 0;
  515. bool esc = 0;
  516. bool strO = 0;
  517. int objOc = 0;
  518. int arrayOc = 0;
  519. for (; str[i]; i++)
  520. {
  521. switch (str[i])
  522. {
  523. case '\\':
  524. if (strO)
  525. esc = !esc;
  526. else
  527. esc = 0;
  528. break;
  529. case '"':
  530. if (!esc) strO = !strO;
  531. esc = 0;
  532. break;
  533. case '[':
  534. if (!strO) arrayOc++;
  535. esc = 0;
  536. break;
  537. case ']':
  538. if (!strO) arrayOc--;
  539. esc = 0;
  540. break;
  541. case '{':
  542. if (!strO) objOc++;
  543. esc = 0;
  544. break;
  545. case '}':
  546. if (!strO) objOc--;
  547. esc = 0;
  548. break;
  549. case ':':
  550. if (!strO && objOc == 0 && arrayOc == 0) return i;
  551. esc = 0;
  552. break;
  553. default:
  554. esc = 0;
  555. break;
  556. }
  557. }
  558. return i;
  559. }
  560. int Parser::findValueEndInObject(const char* str)
  561. {
  562. int i = 0;
  563. bool esc = 0;
  564. bool strO = 0;
  565. int objOc = 0;
  566. int arrayOc = 0;
  567. for (; str[i]; i++)
  568. {
  569. switch (str[i])
  570. {
  571. case '\\':
  572. if (strO)
  573. esc = !esc;
  574. else
  575. esc = 0;
  576. break;
  577. case '"':
  578. if (!esc) strO = !strO;
  579. esc = 0;
  580. break;
  581. case '[':
  582. if (!strO) arrayOc++;
  583. esc = 0;
  584. break;
  585. case ']':
  586. if (!strO) arrayOc--;
  587. esc = 0;
  588. break;
  589. case '{':
  590. if (!strO) objOc++;
  591. esc = 0;
  592. break;
  593. case '}':
  594. if (!strO) objOc--;
  595. esc = 0;
  596. break;
  597. case ',':
  598. if (!strO && objOc == 0 && arrayOc == 0) return i;
  599. esc = 0;
  600. break;
  601. default:
  602. esc = 0;
  603. break;
  604. }
  605. }
  606. return i;
  607. }
  608. #pragma endregion Cotent