JSON.cpp 11 KB

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