JSON.cpp 10 KB

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