JSON.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. if( strO )
  321. ret.getText()[ index++ ] = str[ i ];
  322. esc = 0;
  323. break;
  324. default:
  325. ret.getText()[ index++ ] = str[ i ];
  326. esc = 0;
  327. break;
  328. }
  329. }
  330. return ret;
  331. }
  332. JSONValue *Parser::getValue( const char *str )
  333. {
  334. Text string = Parser::removeWhitespace( str );
  335. if( string.istGleich( "true" ) )
  336. return new JSONBool( 1 );
  337. if( string.istGleich( "false" ) )
  338. return new JSONBool( 0 );
  339. if( string.getText()[ 0 ] == '"' )
  340. {
  341. string.remove( 0, 1 );
  342. string.remove( string.getLength() - 1, string.getLength() );
  343. return new JSONString( string );
  344. }
  345. if( string.getText()[ 0 ] == '[' )
  346. return new JSONArray( string );
  347. if( string.getText()[ 0 ] == '{' )
  348. return new JSONObject( string );
  349. if( Text( (double)string ).istGleich( string.getText() ) )
  350. return new JSONNumber( string );
  351. return new JSONValue();
  352. }
  353. int Parser::findFieldEndInObject( const char *str )
  354. {
  355. int i = 0;
  356. bool esc = 0;
  357. bool strO = 0;
  358. int objOc = 0;
  359. int arrayOc = 0;
  360. for( ; str[ i ]; i++ )
  361. {
  362. switch( str[ i ] )
  363. {
  364. case '\\':
  365. if( strO )
  366. esc = !esc;
  367. else
  368. esc = 0;
  369. break;
  370. case '"':
  371. if( !esc )
  372. strO = !strO;
  373. esc = 0;
  374. break;
  375. case '[':
  376. if( !strO )
  377. arrayOc++;
  378. esc = 0;
  379. break;
  380. case ']':
  381. if( !strO )
  382. arrayOc--;
  383. esc = 0;
  384. break;
  385. case '{':
  386. if( !strO )
  387. objOc++;
  388. esc = 0;
  389. break;
  390. case '}':
  391. if( !strO )
  392. objOc--;
  393. esc = 0;
  394. break;
  395. case ':':
  396. if( !strO && objOc == 0 && arrayOc == 0 )
  397. return i;
  398. esc = 0;
  399. break;
  400. default:
  401. esc = 0;
  402. break;
  403. }
  404. }
  405. return i;
  406. }
  407. int Parser::findValueEndInObject( const char *str )
  408. {
  409. int i = 0;
  410. bool esc = 0;
  411. bool strO = 0;
  412. int objOc = 0;
  413. int arrayOc = 0;
  414. for( ; str[ i ]; i++ )
  415. {
  416. switch( str[ i ] )
  417. {
  418. case '\\':
  419. if( strO )
  420. esc = !esc;
  421. else
  422. esc = 0;
  423. break;
  424. case '"':
  425. if( !esc )
  426. strO = !strO;
  427. esc = 0;
  428. break;
  429. case '[':
  430. if( !strO )
  431. arrayOc++;
  432. esc = 0;
  433. break;
  434. case ']':
  435. if( !strO )
  436. arrayOc--;
  437. esc = 0;
  438. break;
  439. case '{':
  440. if( !strO )
  441. objOc++;
  442. esc = 0;
  443. break;
  444. case '}':
  445. if( !strO )
  446. objOc--;
  447. esc = 0;
  448. break;
  449. case ',':
  450. if( !strO && objOc == 0 && arrayOc == 0 )
  451. return i;
  452. esc = 0;
  453. break;
  454. default:
  455. esc = 0;
  456. break;
  457. }
  458. }
  459. return i;
  460. }