json.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*************************************************************************/
  2. /* json.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "json.h"
  30. #include "print_string.h"
  31. const char * JSON::tk_name[TK_MAX] = {
  32. "'{'",
  33. "'}'",
  34. "'['",
  35. "']'",
  36. "identifier",
  37. "string",
  38. "number",
  39. "':'",
  40. "','",
  41. "EOF",
  42. };
  43. String JSON::_print_var(const Variant& p_var) {
  44. switch(p_var.get_type()) {
  45. case Variant::NIL: return "null";
  46. case Variant::BOOL: return p_var.operator bool() ? "true": "false";
  47. case Variant::INT: return itos(p_var);
  48. case Variant::REAL: return rtos(p_var);
  49. case Variant::INT_ARRAY:
  50. case Variant::REAL_ARRAY:
  51. case Variant::STRING_ARRAY:
  52. case Variant::ARRAY: {
  53. String s = "[";
  54. Array a = p_var;
  55. for(int i=0;i<a.size();i++) {
  56. if (i>0)
  57. s+=", ";
  58. s+=_print_var(a[i]);
  59. }
  60. s+="]";
  61. return s;
  62. };
  63. case Variant::DICTIONARY: {
  64. String s = "{";
  65. Dictionary d = p_var;
  66. List<Variant> keys;
  67. d.get_key_list(&keys);
  68. for (List<Variant>::Element *E=keys.front();E;E=E->next()) {
  69. if (E!=keys.front())
  70. s+=", ";
  71. s+=_print_var(String(E->get()));
  72. s+=":";
  73. s+=_print_var(d[E->get()]);
  74. }
  75. s+="}";
  76. return s;
  77. };
  78. default: return "\""+String(p_var).c_escape()+"\"";
  79. }
  80. }
  81. String JSON::print(const Dictionary& p_dict) {
  82. return _print_var(p_dict);
  83. }
  84. Error JSON::_get_token(const CharType *p_str, int &idx, int p_len, Token& r_token,int &line,String &r_err_str) {
  85. while (true) {
  86. switch(p_str[idx]) {
  87. case '\n': {
  88. line++;
  89. idx++;
  90. break;
  91. };
  92. case 0: {
  93. r_token.type=TK_EOF;
  94. return OK;
  95. } break;
  96. case '{': {
  97. r_token.type=TK_CURLY_BRACKET_OPEN;
  98. idx++;
  99. return OK;
  100. };
  101. case '}': {
  102. r_token.type=TK_CURLY_BRACKET_CLOSE;
  103. idx++;
  104. return OK;
  105. };
  106. case '[': {
  107. r_token.type=TK_BRACKET_OPEN;
  108. idx++;
  109. return OK;
  110. };
  111. case ']': {
  112. r_token.type=TK_BRACKET_CLOSE;
  113. idx++;
  114. return OK;
  115. };
  116. case ':': {
  117. r_token.type=TK_COLON;
  118. idx++;
  119. return OK;
  120. };
  121. case ',': {
  122. r_token.type=TK_COMMA;
  123. idx++;
  124. return OK;
  125. };
  126. case '"': {
  127. idx++;
  128. String str;
  129. while(true) {
  130. if (p_str[idx]==0) {
  131. r_err_str="Unterminated String";
  132. return ERR_PARSE_ERROR;
  133. } else if (p_str[idx]=='"') {
  134. idx++;
  135. break;
  136. } else if (p_str[idx]=='\\') {
  137. //escaped characters...
  138. idx++;
  139. CharType next = p_str[idx];
  140. if (next==0) {
  141. r_err_str="Unterminated String";
  142. return ERR_PARSE_ERROR;
  143. }
  144. CharType res=0;
  145. switch(next) {
  146. case 'b': res=8; break;
  147. case 't': res=9; break;
  148. case 'n': res=10; break;
  149. case 'f': res=12; break;
  150. case 'r': res=13; break;
  151. case '\"': res='\"'; break;
  152. case '\\': res='\\'; break;
  153. case '/': res='/'; break; //wtf
  154. case 'u': {
  155. //hexnumbarh - oct is deprecated
  156. for(int j=0;j<4;j++) {
  157. CharType c = p_str[idx+j+1];
  158. if (c==0) {
  159. r_err_str="Unterminated String";
  160. return ERR_PARSE_ERROR;
  161. }
  162. if (!((c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F'))) {
  163. r_err_str="Malformed hex constant in string";
  164. return ERR_PARSE_ERROR;
  165. }
  166. CharType v;
  167. if (c>='0' && c<='9') {
  168. v=c-'0';
  169. } else if (c>='a' && c<='f') {
  170. v=c-'a';
  171. v+=10;
  172. } else if (c>='A' && c<='F') {
  173. v=c-'A';
  174. v+=10;
  175. } else {
  176. ERR_PRINT("BUG");
  177. v=0;
  178. }
  179. res<<=4;
  180. res|=v;
  181. }
  182. idx+=4; //will add at the end anyway
  183. } break;
  184. default: {
  185. r_err_str="Invalid escape sequence";
  186. return ERR_PARSE_ERROR;
  187. } break;
  188. }
  189. str+=res;
  190. } else {
  191. if (p_str[idx]=='\n')
  192. line++;
  193. str+=p_str[idx];
  194. }
  195. idx++;
  196. }
  197. r_token.type=TK_STRING;
  198. r_token.value=str;
  199. return OK;
  200. } break;
  201. default: {
  202. if (p_str[idx]<=32) {
  203. idx++;
  204. break;
  205. }
  206. if (p_str[idx]=='-' || (p_str[idx]>='0' && p_str[idx]<='9')) {
  207. //a number
  208. const CharType *rptr;
  209. double number = String::to_double(&p_str[idx],&rptr);
  210. idx+=(rptr - &p_str[idx]);
  211. r_token.type=TK_NUMBER;
  212. r_token.value=number;
  213. return OK;
  214. } else if ((p_str[idx]>='A' && p_str[idx]<='Z') || (p_str[idx]>='a' && p_str[idx]<='z')) {
  215. String id;
  216. while((p_str[idx]>='A' && p_str[idx]<='Z') || (p_str[idx]>='a' && p_str[idx]<='z')) {
  217. id+=p_str[idx];
  218. idx++;
  219. }
  220. r_token.type=TK_IDENTIFIER;
  221. r_token.value=id;
  222. return OK;
  223. } else {
  224. r_err_str="Unexpected character.";
  225. return ERR_PARSE_ERROR;
  226. }
  227. }
  228. }
  229. }
  230. return ERR_PARSE_ERROR;
  231. }
  232. Error JSON::_parse_value(Variant &value,Token& token,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  233. if (token.type==TK_CURLY_BRACKET_OPEN) {
  234. Dictionary d;
  235. Error err = _parse_object(d,p_str,index,p_len,line,r_err_str);
  236. if (err)
  237. return err;
  238. value=d;
  239. return OK;
  240. } else if (token.type==TK_BRACKET_OPEN) {
  241. Array a;
  242. Error err = _parse_array(a,p_str,index,p_len,line,r_err_str);
  243. if (err)
  244. return err;
  245. value=a;
  246. return OK;
  247. } else if (token.type==TK_IDENTIFIER) {
  248. String id = token.value;
  249. if (id=="true")
  250. value=true;
  251. else if (id=="false")
  252. value=false;
  253. else if (id=="null")
  254. value=Variant();
  255. else {
  256. r_err_str="Expected 'true','false' or 'null', got '"+id+"'.";
  257. return ERR_PARSE_ERROR;
  258. }
  259. return OK;
  260. } else if (token.type==TK_NUMBER) {
  261. value=token.value;
  262. return OK;
  263. } else if (token.type==TK_STRING) {
  264. value=token.value;
  265. return OK;
  266. } else {
  267. r_err_str="Expected value, got "+String(tk_name[token.type])+".";
  268. return ERR_PARSE_ERROR;
  269. }
  270. return ERR_PARSE_ERROR;
  271. }
  272. Error JSON::_parse_array(Array &array,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  273. Token token;
  274. bool need_comma=false;
  275. while(index<p_len) {
  276. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  277. if (err!=OK)
  278. return err;
  279. if (token.type==TK_BRACKET_CLOSE) {
  280. return OK;
  281. }
  282. if (need_comma) {
  283. if (token.type!=TK_COMMA) {
  284. r_err_str="Expected ','";
  285. return ERR_PARSE_ERROR;
  286. } else {
  287. need_comma=false;
  288. continue;
  289. }
  290. }
  291. Variant v;
  292. err = _parse_value(v,token,p_str,index,p_len,line,r_err_str);
  293. if (err)
  294. return err;
  295. array.push_back(v);
  296. need_comma=true;
  297. }
  298. return OK;
  299. }
  300. Error JSON::_parse_object(Dictionary &object,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  301. bool at_key=true;
  302. String key;
  303. Token token;
  304. bool need_comma=false;
  305. while(index<p_len) {
  306. if (at_key) {
  307. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  308. if (err!=OK)
  309. return err;
  310. if (token.type==TK_CURLY_BRACKET_CLOSE) {
  311. return OK;
  312. }
  313. if (need_comma) {
  314. if (token.type!=TK_COMMA) {
  315. r_err_str="Expected '}' or ','";
  316. return ERR_PARSE_ERROR;
  317. } else {
  318. need_comma=false;
  319. continue;
  320. }
  321. }
  322. if (token.type!=TK_STRING) {
  323. r_err_str="Expected key";
  324. return ERR_PARSE_ERROR;
  325. }
  326. key=token.value;
  327. err = _get_token(p_str,index,p_len,token,line,r_err_str);
  328. if (err!=OK)
  329. return err;
  330. if (token.type!=TK_COLON) {
  331. r_err_str="Expected ':'";
  332. return ERR_PARSE_ERROR;
  333. }
  334. at_key=false;
  335. } else {
  336. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  337. if (err!=OK)
  338. return err;
  339. Variant v;
  340. err = _parse_value(v,token,p_str,index,p_len,line,r_err_str);
  341. if (err)
  342. return err;
  343. object[key]=v;
  344. need_comma=true;
  345. at_key=true;
  346. }
  347. }
  348. return OK;
  349. }
  350. Error JSON::parse(const String& p_json,Dictionary& r_ret,String &r_err_str,int &r_err_line) {
  351. const CharType *str = p_json.ptr();
  352. int idx = 0;
  353. int len = p_json.length();
  354. Token token;
  355. int line=0;
  356. String aux_key;
  357. Error err = _get_token(str,idx,len,token,line,r_err_str);
  358. if (err)
  359. return err;
  360. if (token.type!=TK_CURLY_BRACKET_OPEN) {
  361. r_err_str="Expected '{'";
  362. return ERR_PARSE_ERROR;
  363. }
  364. return _parse_object(r_ret,str,idx,len,r_err_line,r_err_str);
  365. }