JSON.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. int JSONObject::getFieldCount() const
  240. {
  241. return fields->getEintragAnzahl();
  242. }
  243. Text JSONObject::toString() const
  244. {
  245. Text str = "{";
  246. Iterator< Text > k = fields->getIterator();
  247. for( auto v = values->getIterator(); k && v; k++, v++ )
  248. {
  249. str += "\"";
  250. str += k._.getText();
  251. str += "\":";
  252. str += v->toString().getText();
  253. if( v.hasNext() )
  254. str += ",";
  255. }
  256. str += "}";
  257. return str;
  258. }
  259. int Parser::findObjectEndInArray( const char *str )
  260. {
  261. return findValueEndInObject( str );
  262. }
  263. Text Parser::removeWhitespace( const char *str )
  264. {
  265. int wsc = 0;
  266. int i = 0;
  267. bool esc = 0;
  268. bool strO = 0;
  269. for( ; str[ i ]; i++ )
  270. {
  271. switch( str[ i ] )
  272. {
  273. case '\\':
  274. if( strO )
  275. esc = !esc;
  276. else
  277. esc = 0;
  278. break;
  279. case '"':
  280. if( !esc )
  281. strO = !strO;
  282. esc = 0;
  283. break;
  284. case ' ':
  285. case '\n':
  286. case '\t':
  287. case '\r':
  288. if( !strO )
  289. wsc++;
  290. esc = 0;
  291. break;
  292. default:
  293. esc = 0;
  294. break;
  295. }
  296. }
  297. Text ret;
  298. ret.fillText( ' ', i - wsc );
  299. i = 0;
  300. esc = 0;
  301. strO = 0;
  302. int index = 0;
  303. for( ; str[ i ]; i++ )
  304. {
  305. switch( str[ i ] )
  306. {
  307. case '\\':
  308. if( strO )
  309. esc = !esc;
  310. else
  311. esc = 0;
  312. ret.getText()[ index++ ] = str[ i ];
  313. break;
  314. case '"':
  315. if( !esc )
  316. strO = !strO;
  317. esc = 0;
  318. ret.getText()[ index++ ] = str[ i ];
  319. break;
  320. case ' ':
  321. case '\n':
  322. case '\t':
  323. case '\r':
  324. if( strO )
  325. ret.getText()[ index++ ] = str[ i ];
  326. esc = 0;
  327. break;
  328. default:
  329. ret.getText()[ index++ ] = str[ i ];
  330. esc = 0;
  331. break;
  332. }
  333. }
  334. return ret;
  335. }
  336. JSONValue *Parser::getValue( const char *str )
  337. {
  338. Text string = Parser::removeWhitespace( str );
  339. if( string.istGleich( "true" ) )
  340. return new JSONBool( 1 );
  341. if( string.istGleich( "false" ) )
  342. return new JSONBool( 0 );
  343. if( string.getText()[ 0 ] == '"' )
  344. {
  345. string.remove( 0, 1 );
  346. string.remove( string.getLength() - 1, string.getLength() );
  347. return new JSONString( string );
  348. }
  349. if( string.getText()[ 0 ] == '[' )
  350. return new JSONArray( string );
  351. if( string.getText()[ 0 ] == '{' )
  352. return new JSONObject( string );
  353. if( Text( (double)string ).istGleich( string.getText() ) )
  354. return new JSONNumber( string );
  355. return new JSONValue();
  356. }
  357. int Parser::findFieldEndInObject( const char *str )
  358. {
  359. int i = 0;
  360. bool esc = 0;
  361. bool strO = 0;
  362. int objOc = 0;
  363. int arrayOc = 0;
  364. for( ; str[ i ]; i++ )
  365. {
  366. switch( str[ i ] )
  367. {
  368. case '\\':
  369. if( strO )
  370. esc = !esc;
  371. else
  372. esc = 0;
  373. break;
  374. case '"':
  375. if( !esc )
  376. strO = !strO;
  377. esc = 0;
  378. break;
  379. case '[':
  380. if( !strO )
  381. arrayOc++;
  382. esc = 0;
  383. break;
  384. case ']':
  385. if( !strO )
  386. arrayOc--;
  387. esc = 0;
  388. break;
  389. case '{':
  390. if( !strO )
  391. objOc++;
  392. esc = 0;
  393. break;
  394. case '}':
  395. if( !strO )
  396. objOc--;
  397. esc = 0;
  398. break;
  399. case ':':
  400. if( !strO && objOc == 0 && arrayOc == 0 )
  401. return i;
  402. esc = 0;
  403. break;
  404. default:
  405. esc = 0;
  406. break;
  407. }
  408. }
  409. return i;
  410. }
  411. int Parser::findValueEndInObject( const char *str )
  412. {
  413. int i = 0;
  414. bool esc = 0;
  415. bool strO = 0;
  416. int objOc = 0;
  417. int arrayOc = 0;
  418. for( ; str[ i ]; i++ )
  419. {
  420. switch( str[ i ] )
  421. {
  422. case '\\':
  423. if( strO )
  424. esc = !esc;
  425. else
  426. esc = 0;
  427. break;
  428. case '"':
  429. if( !esc )
  430. strO = !strO;
  431. esc = 0;
  432. break;
  433. case '[':
  434. if( !strO )
  435. arrayOc++;
  436. esc = 0;
  437. break;
  438. case ']':
  439. if( !strO )
  440. arrayOc--;
  441. esc = 0;
  442. break;
  443. case '{':
  444. if( !strO )
  445. objOc++;
  446. esc = 0;
  447. break;
  448. case '}':
  449. if( !strO )
  450. objOc--;
  451. esc = 0;
  452. break;
  453. case ',':
  454. if( !strO && objOc == 0 && arrayOc == 0 )
  455. return i;
  456. esc = 0;
  457. break;
  458. default:
  459. esc = 0;
  460. break;
  461. }
  462. }
  463. return i;
  464. }