JSON.cpp 9.5 KB

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