JSON.cpp 9.9 KB

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