JSON.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 = 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( 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( 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( 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( ARRAY )
  69. {
  70. array = new RCArray< JSONValue >();
  71. }
  72. JSONArray::JSONArray( Text string )
  73. : JSONValue( 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( 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. Text JSONArray::toString() const
  119. {
  120. Text str = "[";
  121. for( auto i = array->getIterator(); i; i++ )
  122. {
  123. str += i->toString();
  124. if( i.hasNext() )
  125. str += ",";
  126. }
  127. str += "]";
  128. return str;
  129. }
  130. JSONObject::JSONObject()
  131. : JSONValue( OBJECT )
  132. {
  133. fields = new Array< Text >();
  134. values = new RCArray< JSONValue >();
  135. }
  136. JSONObject::JSONObject( Text string )
  137. : JSONValue( OBJECT )
  138. {
  139. fields = new Array< Text >();
  140. values = new RCArray< JSONValue >();
  141. string = Parser::removeWhitespace( string );
  142. if( string.getText()[ 0 ] == '{' && string.getText()[ string.getLength() - 1 ] == '}' )
  143. {
  144. string.remove( 0, 1 );
  145. string.remove( string.getLength() - 1, string.getLength() );
  146. while( string.getLength() )
  147. {
  148. int endField = Parser::findFieldEndInObject( string );
  149. Text *fieldName = string.getTeilText( 0, endField );
  150. string.remove( 0, endField + 1 );
  151. fieldName->remove( 0, 1 );
  152. fieldName->remove( fieldName->getLength() - 1, fieldName->getLength() );
  153. int endValue = Parser::findValueEndInObject( string );
  154. Text *value = string.getTeilText( 0, endValue );
  155. string.remove( 0, endValue + 1 );
  156. fields->add( Text( fieldName->getText() ) );
  157. values->add( Parser::getValue( *value ) );
  158. fieldName->release();
  159. value->release();
  160. }
  161. }
  162. }
  163. JSONObject::JSONObject( const JSONObject &obj )
  164. : JSONValue( OBJECT )
  165. {
  166. fields = dynamic_cast<Array<Text> *>( obj.fields->getThis() );
  167. values = dynamic_cast<RCArray<JSONValue> *>( obj.values->getThis() );
  168. }
  169. JSONObject::~JSONObject()
  170. {
  171. fields->release();
  172. values->release();
  173. }
  174. JSONObject &JSONObject::operator=( const JSONObject &obj )
  175. {
  176. fields->release();
  177. values->release();
  178. fields = dynamic_cast<Array<Text> *>( obj.fields->getThis() );
  179. values = dynamic_cast<RCArray<JSONValue> *>( obj.values->getThis() );
  180. return *this;
  181. }
  182. bool JSONObject::addValue( Text field, JSONValue *value )
  183. {
  184. if( hasValue( field ) )
  185. return 0;
  186. fields->add( field );
  187. values->add( value );
  188. return 1;
  189. }
  190. bool JSONObject::removeValue( Text field )
  191. {
  192. for( int i = 0; i < fields->getEintragAnzahl(); i++ )
  193. {
  194. if( fields->get( i ).istGleich( field ) )
  195. {
  196. fields->remove( i );
  197. values->remove( i );
  198. return 1;
  199. }
  200. }
  201. return 0;
  202. }
  203. bool JSONObject::hasValue( Text field )
  204. {
  205. for( int i = 0; i < fields->getEintragAnzahl(); i++ )
  206. {
  207. if( fields->get( i ).istGleich( field ) )
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. JSONValue *JSONObject::getValue( Text field )
  213. {
  214. for( int i = 0; i < fields->getEintragAnzahl(); i++ )
  215. {
  216. if( fields->get( i ).istGleich( field ) )
  217. return values->get( i );
  218. }
  219. return new JSONValue();
  220. }
  221. Iterator< Text > JSONObject::getFields()
  222. {
  223. return fields->getIterator();
  224. }
  225. Iterator< JSONValue * > JSONObject::getValues()
  226. {
  227. return values->getIterator();
  228. }
  229. int JSONObject::getFieldCount() const
  230. {
  231. return fields->getEintragAnzahl();
  232. }
  233. Text JSONObject::toString() const
  234. {
  235. Text str = "{";
  236. Iterator< Text > k = fields->getIterator();
  237. for( auto v = values->getIterator(); k && v; k++, v++ )
  238. {
  239. str += "\"";
  240. str += k._.getText();
  241. str += "\":";
  242. str += v->toString().getText();
  243. if( v.hasNext() )
  244. str += ",";
  245. }
  246. str += "}";
  247. return str;
  248. }
  249. JSONValue *JSON::loadJSONFromFile( Text path )
  250. {
  251. Datei d;
  252. d.setDatei( path );
  253. d.open( Datei::Style::lesen );
  254. int size = (int)d.getSize();
  255. char *buffer = new char[ size + 1 ];
  256. buffer[ size ] = 0;
  257. d.lese( buffer, size );
  258. d.close();
  259. JSONValue *result = Parser::getValue( buffer );
  260. delete[] buffer;
  261. return result;
  262. }
  263. int Parser::findObjectEndInArray( const char *str )
  264. {
  265. return findValueEndInObject( str );
  266. }
  267. Text Parser::removeWhitespace( const char *str )
  268. {
  269. int wsc = 0;
  270. int i = 0;
  271. bool esc = 0;
  272. bool strO = 0;
  273. for( ; str[ i ]; i++ )
  274. {
  275. switch( str[ i ] )
  276. {
  277. case '\\':
  278. if( strO )
  279. esc = !esc;
  280. else
  281. esc = 0;
  282. break;
  283. case '"':
  284. if( !esc )
  285. strO = !strO;
  286. esc = 0;
  287. break;
  288. case ' ':
  289. case '\n':
  290. case '\t':
  291. case '\r':
  292. if( !strO )
  293. wsc++;
  294. esc = 0;
  295. break;
  296. default:
  297. esc = 0;
  298. break;
  299. }
  300. }
  301. Text ret;
  302. ret.fillText( ' ', i - wsc );
  303. i = 0;
  304. esc = 0;
  305. strO = 0;
  306. int index = 0;
  307. for( ; str[ i ]; i++ )
  308. {
  309. switch( str[ i ] )
  310. {
  311. case '\\':
  312. if( strO )
  313. esc = !esc;
  314. else
  315. esc = 0;
  316. ret.getText()[ index++ ] = str[ i ];
  317. break;
  318. case '"':
  319. if( !esc )
  320. strO = !strO;
  321. esc = 0;
  322. ret.getText()[ index++ ] = str[ i ];
  323. break;
  324. case ' ':
  325. case '\n':
  326. case '\t':
  327. case '\r':
  328. if( strO )
  329. ret.getText()[ index++ ] = str[ i ];
  330. esc = 0;
  331. break;
  332. default:
  333. ret.getText()[ index++ ] = str[ i ];
  334. esc = 0;
  335. break;
  336. }
  337. }
  338. return ret;
  339. }
  340. JSONValue *Parser::getValue( const char *str )
  341. {
  342. Text string = Parser::removeWhitespace( str );
  343. if( string.istGleich( "true" ) )
  344. return new JSONBool( 1 );
  345. if( string.istGleich( "false" ) )
  346. return new JSONBool( 0 );
  347. if( string.getText()[ 0 ] == '"' )
  348. {
  349. string.remove( 0, 1 );
  350. string.remove( string.getLength() - 1, string.getLength() );
  351. return new JSONString( string );
  352. }
  353. if( string.getText()[ 0 ] == '[' )
  354. return new JSONArray( string );
  355. if( string.getText()[ 0 ] == '{' )
  356. return new JSONObject( string );
  357. if( Text( (int)string ).istGleich( string.getText() ) )
  358. return new JSONNumber( string );
  359. if( string.anzahlVon( '.' ) == 1 )
  360. {
  361. bool isNumber = 1;
  362. for( char *c = ( *string.getText() == '-' ) ? string.getText() + 1 : string.getText(); *c; c++ )
  363. isNumber &= ( *c >= '0' && *c <= '9' ) || *c == '.';
  364. if( isNumber )
  365. return new JSONNumber( string );
  366. }
  367. return new JSONValue();
  368. }
  369. int Parser::findFieldEndInObject( const char *str )
  370. {
  371. int i = 0;
  372. bool esc = 0;
  373. bool strO = 0;
  374. int objOc = 0;
  375. int arrayOc = 0;
  376. for( ; str[ i ]; i++ )
  377. {
  378. switch( str[ i ] )
  379. {
  380. case '\\':
  381. if( strO )
  382. esc = !esc;
  383. else
  384. esc = 0;
  385. break;
  386. case '"':
  387. if( !esc )
  388. strO = !strO;
  389. esc = 0;
  390. break;
  391. case '[':
  392. if( !strO )
  393. arrayOc++;
  394. esc = 0;
  395. break;
  396. case ']':
  397. if( !strO )
  398. arrayOc--;
  399. esc = 0;
  400. break;
  401. case '{':
  402. if( !strO )
  403. objOc++;
  404. esc = 0;
  405. break;
  406. case '}':
  407. if( !strO )
  408. objOc--;
  409. esc = 0;
  410. break;
  411. case ':':
  412. if( !strO && objOc == 0 && arrayOc == 0 )
  413. return i;
  414. esc = 0;
  415. break;
  416. default:
  417. esc = 0;
  418. break;
  419. }
  420. }
  421. return i;
  422. }
  423. int Parser::findValueEndInObject( const char *str )
  424. {
  425. int i = 0;
  426. bool esc = 0;
  427. bool strO = 0;
  428. int objOc = 0;
  429. int arrayOc = 0;
  430. for( ; str[ i ]; i++ )
  431. {
  432. switch( str[ i ] )
  433. {
  434. case '\\':
  435. if( strO )
  436. esc = !esc;
  437. else
  438. esc = 0;
  439. break;
  440. case '"':
  441. if( !esc )
  442. strO = !strO;
  443. esc = 0;
  444. break;
  445. case '[':
  446. if( !strO )
  447. arrayOc++;
  448. esc = 0;
  449. break;
  450. case ']':
  451. if( !strO )
  452. arrayOc--;
  453. esc = 0;
  454. break;
  455. case '{':
  456. if( !strO )
  457. objOc++;
  458. esc = 0;
  459. break;
  460. case '}':
  461. if( !strO )
  462. objOc--;
  463. esc = 0;
  464. break;
  465. case ',':
  466. if( !strO && objOc == 0 && arrayOc == 0 )
  467. return i;
  468. esc = 0;
  469. break;
  470. default:
  471. esc = 0;
  472. break;
  473. }
  474. }
  475. return i;
  476. }