JSON.cpp 11 KB

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