json.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*************************************************************************/
  2. /* json.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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).json_escape()+"\"";
  79. }
  80. }
  81. String JSON::print(const Variant& p_var) {
  82. return _print_var(p_var);
  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 'u': {
  152. //hexnumbarh - oct is deprecated
  153. for(int j=0;j<4;j++) {
  154. CharType c = p_str[idx+j+1];
  155. if (c==0) {
  156. r_err_str="Unterminated String";
  157. return ERR_PARSE_ERROR;
  158. }
  159. if (!((c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F'))) {
  160. r_err_str="Malformed hex constant in string";
  161. return ERR_PARSE_ERROR;
  162. }
  163. CharType v;
  164. if (c>='0' && c<='9') {
  165. v=c-'0';
  166. } else if (c>='a' && c<='f') {
  167. v=c-'a';
  168. v+=10;
  169. } else if (c>='A' && c<='F') {
  170. v=c-'A';
  171. v+=10;
  172. } else {
  173. ERR_PRINT("BUG");
  174. v=0;
  175. }
  176. res<<=4;
  177. res|=v;
  178. }
  179. idx+=4; //will add at the end anyway
  180. } break;
  181. //case '\"': res='\"'; break;
  182. //case '\\': res='\\'; break;
  183. //case '/': res='/'; break;
  184. default: {
  185. res = next;
  186. //r_err_str="Invalid escape sequence";
  187. //return ERR_PARSE_ERROR;
  188. } break;
  189. }
  190. str+=res;
  191. } else {
  192. if (p_str[idx]=='\n')
  193. line++;
  194. str+=p_str[idx];
  195. }
  196. idx++;
  197. }
  198. r_token.type=TK_STRING;
  199. r_token.value=str;
  200. return OK;
  201. } break;
  202. default: {
  203. if (p_str[idx]<=32) {
  204. idx++;
  205. break;
  206. }
  207. if (p_str[idx]=='-' || (p_str[idx]>='0' && p_str[idx]<='9')) {
  208. //a number
  209. const CharType *rptr;
  210. double number = String::to_double(&p_str[idx],&rptr);
  211. idx+=(rptr - &p_str[idx]);
  212. r_token.type=TK_NUMBER;
  213. r_token.value=number;
  214. return OK;
  215. } else if ((p_str[idx]>='A' && p_str[idx]<='Z') || (p_str[idx]>='a' && p_str[idx]<='z')) {
  216. String id;
  217. while((p_str[idx]>='A' && p_str[idx]<='Z') || (p_str[idx]>='a' && p_str[idx]<='z')) {
  218. id+=p_str[idx];
  219. idx++;
  220. }
  221. r_token.type=TK_IDENTIFIER;
  222. r_token.value=id;
  223. return OK;
  224. } else {
  225. r_err_str="Unexpected character.";
  226. return ERR_PARSE_ERROR;
  227. }
  228. }
  229. }
  230. }
  231. return ERR_PARSE_ERROR;
  232. }
  233. Error JSON::_parse_value(Variant &value,Token& token,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  234. if (token.type==TK_CURLY_BRACKET_OPEN) {
  235. Dictionary d(true);
  236. Error err = _parse_object(d,p_str,index,p_len,line,r_err_str);
  237. if (err)
  238. return err;
  239. value=d;
  240. return OK;
  241. } else if (token.type==TK_BRACKET_OPEN) {
  242. Array a(true);
  243. Error err = _parse_array(a,p_str,index,p_len,line,r_err_str);
  244. if (err)
  245. return err;
  246. value=a;
  247. return OK;
  248. } else if (token.type==TK_IDENTIFIER) {
  249. String id = token.value;
  250. if (id=="true")
  251. value=true;
  252. else if (id=="false")
  253. value=false;
  254. else if (id=="null")
  255. value=Variant();
  256. else {
  257. r_err_str="Expected 'true','false' or 'null', got '"+id+"'.";
  258. return ERR_PARSE_ERROR;
  259. }
  260. return OK;
  261. } else if (token.type==TK_NUMBER) {
  262. value=token.value;
  263. return OK;
  264. } else if (token.type==TK_STRING) {
  265. value=token.value;
  266. return OK;
  267. } else {
  268. r_err_str="Expected value, got "+String(tk_name[token.type])+".";
  269. return ERR_PARSE_ERROR;
  270. }
  271. return ERR_PARSE_ERROR;
  272. }
  273. Error JSON::_parse_array(Array &array,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  274. Token token;
  275. bool need_comma=false;
  276. while(index<p_len) {
  277. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  278. if (err!=OK)
  279. return err;
  280. if (token.type==TK_BRACKET_CLOSE) {
  281. return OK;
  282. }
  283. if (need_comma) {
  284. if (token.type!=TK_COMMA) {
  285. r_err_str="Expected ','";
  286. return ERR_PARSE_ERROR;
  287. } else {
  288. need_comma=false;
  289. continue;
  290. }
  291. }
  292. Variant v;
  293. err = _parse_value(v,token,p_str,index,p_len,line,r_err_str);
  294. if (err)
  295. return err;
  296. array.push_back(v);
  297. need_comma=true;
  298. }
  299. return OK;
  300. }
  301. Error JSON::_parse_object(Dictionary &object,const CharType *p_str,int &index, int p_len,int &line,String &r_err_str) {
  302. bool at_key=true;
  303. String key;
  304. Token token;
  305. bool need_comma=false;
  306. while(index<p_len) {
  307. if (at_key) {
  308. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  309. if (err!=OK)
  310. return err;
  311. if (token.type==TK_CURLY_BRACKET_CLOSE) {
  312. return OK;
  313. }
  314. if (need_comma) {
  315. if (token.type!=TK_COMMA) {
  316. r_err_str="Expected '}' or ','";
  317. return ERR_PARSE_ERROR;
  318. } else {
  319. need_comma=false;
  320. continue;
  321. }
  322. }
  323. if (token.type!=TK_STRING) {
  324. r_err_str="Expected key";
  325. return ERR_PARSE_ERROR;
  326. }
  327. key=token.value;
  328. err = _get_token(p_str,index,p_len,token,line,r_err_str);
  329. if (err!=OK)
  330. return err;
  331. if (token.type!=TK_COLON) {
  332. r_err_str="Expected ':'";
  333. return ERR_PARSE_ERROR;
  334. }
  335. at_key=false;
  336. } else {
  337. Error err = _get_token(p_str,index,p_len,token,line,r_err_str);
  338. if (err!=OK)
  339. return err;
  340. Variant v;
  341. err = _parse_value(v,token,p_str,index,p_len,line,r_err_str);
  342. if (err)
  343. return err;
  344. object[key]=v;
  345. need_comma=true;
  346. at_key=true;
  347. }
  348. }
  349. return OK;
  350. }
  351. Error JSON::parse(const String& p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
  352. const CharType *str = p_json.ptr();
  353. int idx = 0;
  354. int len = p_json.length();
  355. Token token;
  356. r_err_line=0;
  357. String aux_key;
  358. Error err = _get_token(str,idx,len,token,r_err_line,r_err_str);
  359. if (err)
  360. return err;
  361. err = _parse_value(r_ret,token,str,idx,len,r_err_line,r_err_str);
  362. return err;
  363. }