jsonparse.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2011-2012, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the Contiki operating system.
  30. */
  31. #ifdef JSON_FORMAT
  32. #include "json/jsonparse.h"
  33. #include "osapi.h"
  34. //#include <stdlib.h>
  35. //#include <string.h>
  36. /*--------------------------------------------------------------------*/
  37. static int ICACHE_FLASH_ATTR
  38. push(struct jsonparse_state *state, char c)
  39. {
  40. state->stack[state->depth] = c;
  41. state->depth++;
  42. state->vtype = 0;
  43. return state->depth < JSONPARSE_MAX_DEPTH;
  44. }
  45. /*--------------------------------------------------------------------*/
  46. static char ICACHE_FLASH_ATTR
  47. pop(struct jsonparse_state *state)
  48. {
  49. if(state->depth == 0) {
  50. return JSON_TYPE_ERROR;
  51. }
  52. state->depth--;
  53. return state->stack[state->depth];
  54. }
  55. /*--------------------------------------------------------------------*/
  56. /* will pass by the value and store the start and length of the value for
  57. atomic types */
  58. /*--------------------------------------------------------------------*/
  59. static void ICACHE_FLASH_ATTR
  60. atomic(struct jsonparse_state *state, char type)
  61. {
  62. char c;
  63. state->vstart = state->pos;
  64. state->vtype = type;
  65. if(type == JSON_TYPE_STRING || type == JSON_TYPE_PAIR_NAME) {
  66. while((c = state->json[state->pos++]) && c != '"') {
  67. if(c == '\\') {
  68. state->pos++; /* skip current char */
  69. }
  70. }
  71. state->vlen = state->pos - state->vstart - 1;
  72. } else if(type == JSON_TYPE_NUMBER) {
  73. do {
  74. c = state->json[state->pos];
  75. if((c < '0' || c > '9') && c != '.') {
  76. c = 0;
  77. } else {
  78. state->pos++;
  79. }
  80. } while(c);
  81. /* need to back one step since first char is already gone */
  82. state->vstart--;
  83. state->vlen = state->pos - state->vstart;
  84. }
  85. /* no other types for now... */
  86. }
  87. /*--------------------------------------------------------------------*/
  88. static void ICACHE_FLASH_ATTR
  89. skip_ws(struct jsonparse_state *state)
  90. {
  91. char c;
  92. while(state->pos < state->len &&
  93. ((c = state->json[state->pos]) == ' ' || c == '\n')) {
  94. state->pos++;
  95. }
  96. }
  97. /*--------------------------------------------------------------------*/
  98. void ICACHE_FLASH_ATTR
  99. jsonparse_setup(struct jsonparse_state *state, const char *json, int len)
  100. {
  101. state->json = json;
  102. state->len = len;
  103. state->pos = 0;
  104. state->depth = 0;
  105. state->error = 0;
  106. state->stack[0] = 0;
  107. }
  108. /*--------------------------------------------------------------------*/
  109. int ICACHE_FLASH_ATTR
  110. jsonparse_next(struct jsonparse_state *state)
  111. {
  112. char c;
  113. char s;
  114. skip_ws(state);
  115. c = state->json[state->pos];
  116. s = jsonparse_get_type(state);
  117. state->pos++;
  118. switch(c) {
  119. case '{':
  120. push(state, c);
  121. return c;
  122. case '}':
  123. if(s == ':' && state->vtype != 0) {
  124. /* printf("Popping vtype: '%c'\n", state->vtype); */
  125. pop(state);
  126. s = jsonparse_get_type(state);
  127. }
  128. if(s == '{') {
  129. pop(state);
  130. } else {
  131. state->error = JSON_ERROR_SYNTAX;
  132. return JSON_TYPE_ERROR;
  133. }
  134. return c;
  135. case ']':
  136. if(s == '[') {
  137. pop(state);
  138. } else {
  139. state->error = JSON_ERROR_UNEXPECTED_END_OF_ARRAY;
  140. return JSON_TYPE_ERROR;
  141. }
  142. return c;
  143. case ':':
  144. push(state, c);
  145. return c;
  146. case ',':
  147. /* if x:y ... , */
  148. if(s == ':' && state->vtype != 0) {
  149. pop(state);
  150. } else if(s == '[') {
  151. /* ok! */
  152. } else {
  153. state->error = JSON_ERROR_SYNTAX;
  154. return JSON_TYPE_ERROR;
  155. }
  156. return c;
  157. case '"':
  158. if(s == '{' || s == '[' || s == ':') {
  159. atomic(state, c = (s == '{' ? JSON_TYPE_PAIR_NAME : c));
  160. } else {
  161. state->error = JSON_ERROR_UNEXPECTED_STRING;
  162. return JSON_TYPE_ERROR;
  163. }
  164. return c;
  165. case '[':
  166. if(s == '{' || s == '[' || s == ':') {
  167. push(state, c);
  168. } else {
  169. state->error = JSON_ERROR_UNEXPECTED_ARRAY;
  170. return JSON_TYPE_ERROR;
  171. }
  172. return c;
  173. default:
  174. if(s == ':' || s == '[') {
  175. if(c <= '9' && c >= '0') {
  176. atomic(state, JSON_TYPE_NUMBER);
  177. return JSON_TYPE_NUMBER;
  178. }
  179. }
  180. }
  181. return 0;
  182. }
  183. /*--------------------------------------------------------------------*/
  184. /* get the json value of the current position
  185. * works only on "atomic" values such as string, number, null, false, true
  186. */
  187. /*--------------------------------------------------------------------*/
  188. int ICACHE_FLASH_ATTR
  189. jsonparse_copy_value(struct jsonparse_state *state, char *str, int size)
  190. {
  191. int i;
  192. char z = 0;
  193. char y = 0;
  194. if(state->vtype == 0) {
  195. return 0;
  196. }
  197. size = size <= state->vlen ? (size - 1) : state->vlen;
  198. for(i = 0; i < size; i++) {
  199. if (y == 0 && state->json[state->vstart + i] == '\\') {
  200. y = 1;
  201. z++;
  202. continue;
  203. }
  204. y = 0;
  205. str[i - z] = state->json[state->vstart + i];
  206. }
  207. str[i - z] = 0;
  208. return state->vtype;
  209. }
  210. /*--------------------------------------------------------------------*/
  211. int ICACHE_FLASH_ATTR
  212. jsonparse_get_value_as_int(struct jsonparse_state *state)
  213. {
  214. if(state->vtype != JSON_TYPE_NUMBER) {
  215. return 0;
  216. }
  217. return atoi(&state->json[state->vstart]);
  218. }
  219. /*--------------------------------------------------------------------*/
  220. long ICACHE_FLASH_ATTR
  221. jsonparse_get_value_as_long(struct jsonparse_state *state)
  222. {
  223. if(state->vtype != JSON_TYPE_NUMBER) {
  224. return 0;
  225. }
  226. return atol(&state->json[state->vstart]);
  227. }
  228. /*--------------------------------------------------------------------*/
  229. unsigned long ICACHE_FLASH_ATTR
  230. jsonparse_get_value_as_ulong(struct jsonparse_state *state)
  231. {
  232. if(state->vtype != JSON_TYPE_NUMBER) {
  233. return 0;
  234. }
  235. return strtoul(&state->json[state->vstart], '\0', 0);
  236. }
  237. /*--------------------------------------------------------------------*/
  238. /* strcmp - assume no strange chars that needs to be stuffed in string... */
  239. /*--------------------------------------------------------------------*/
  240. int ICACHE_FLASH_ATTR
  241. jsonparse_strcmp_value(struct jsonparse_state *state, const char *str)
  242. {
  243. if(state->vtype == 0) {
  244. return -1;
  245. }
  246. return os_strncmp(str, &state->json[state->vstart], state->vlen);
  247. }
  248. /*--------------------------------------------------------------------*/
  249. int ICACHE_FLASH_ATTR
  250. jsonparse_get_len(struct jsonparse_state *state)
  251. {
  252. return state->vlen;
  253. }
  254. /*--------------------------------------------------------------------*/
  255. int ICACHE_FLASH_ATTR
  256. jsonparse_get_type(struct jsonparse_state *state)
  257. {
  258. if(state->depth == 0) {
  259. return 0;
  260. }
  261. return state->stack[state->depth - 1];
  262. }
  263. /*--------------------------------------------------------------------*/
  264. int ICACHE_FLASH_ATTR
  265. jsonparse_has_next(struct jsonparse_state *state)
  266. {
  267. return state->pos < state->len;
  268. }
  269. /*--------------------------------------------------------------------*/
  270. #endif