JSON.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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( (double)string ).istGleich( string.getText() ) )
  369. return new JSONNumber( string );
  370. return new JSONValue();
  371. }
  372. int Parser::findFieldEndInObject( const char *str )
  373. {
  374. int i = 0;
  375. bool esc = 0;
  376. bool strO = 0;
  377. int objOc = 0;
  378. int arrayOc = 0;
  379. for( ; str[ i ]; i++ )
  380. {
  381. switch( str[ i ] )
  382. {
  383. case '\\':
  384. if( strO )
  385. esc = !esc;
  386. else
  387. esc = 0;
  388. break;
  389. case '"':
  390. if( !esc )
  391. strO = !strO;
  392. esc = 0;
  393. break;
  394. case '[':
  395. if( !strO )
  396. arrayOc++;
  397. esc = 0;
  398. break;
  399. case ']':
  400. if( !strO )
  401. arrayOc--;
  402. esc = 0;
  403. break;
  404. case '{':
  405. if( !strO )
  406. objOc++;
  407. esc = 0;
  408. break;
  409. case '}':
  410. if( !strO )
  411. objOc--;
  412. esc = 0;
  413. break;
  414. case ':':
  415. if( !strO && objOc == 0 && arrayOc == 0 )
  416. return i;
  417. esc = 0;
  418. break;
  419. default:
  420. esc = 0;
  421. break;
  422. }
  423. }
  424. return i;
  425. }
  426. int Parser::findValueEndInObject( const char *str )
  427. {
  428. int i = 0;
  429. bool esc = 0;
  430. bool strO = 0;
  431. int objOc = 0;
  432. int arrayOc = 0;
  433. for( ; str[ i ]; i++ )
  434. {
  435. switch( str[ i ] )
  436. {
  437. case '\\':
  438. if( strO )
  439. esc = !esc;
  440. else
  441. esc = 0;
  442. break;
  443. case '"':
  444. if( !esc )
  445. strO = !strO;
  446. esc = 0;
  447. break;
  448. case '[':
  449. if( !strO )
  450. arrayOc++;
  451. esc = 0;
  452. break;
  453. case ']':
  454. if( !strO )
  455. arrayOc--;
  456. esc = 0;
  457. break;
  458. case '{':
  459. if( !strO )
  460. objOc++;
  461. esc = 0;
  462. break;
  463. case '}':
  464. if( !strO )
  465. objOc--;
  466. esc = 0;
  467. break;
  468. case ',':
  469. if( !strO && objOc == 0 && arrayOc == 0 )
  470. return i;
  471. esc = 0;
  472. break;
  473. default:
  474. esc = 0;
  475. break;
  476. }
  477. }
  478. return i;
  479. }