jsontree.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /**
  32. * \file
  33. * JSON output generation
  34. * \author
  35. * Niclas Finne <nfi@sics.se>
  36. * Joakim Eriksson <joakime@sics.se>
  37. */
  38. //#include "contiki.h"
  39. #include "jsontree.h"
  40. #include "jsonparse.h"
  41. //#include "osapi.h"
  42. //#include <string.h>
  43. #define DEBUG 0
  44. #if DEBUG
  45. //#include <stdio.h>
  46. #define PRINTF(...) os_printf(__VA_ARGS__)
  47. #else
  48. #define PRINTF(...)
  49. #endif
  50. /*---------------------------------------------------------------------------*/
  51. void FUNCTION_ATTRIBUTE
  52. jsontree_write_atom(const struct jsontree_context *js_ctx, const char *text)
  53. {
  54. if(text == NULL) {
  55. js_ctx->putinchar('0');
  56. } else {
  57. while(*text != '\0') {
  58. js_ctx->putinchar(*text++);
  59. }
  60. }
  61. }
  62. /*---------------------------------------------------------------------------*/
  63. void FUNCTION_ATTRIBUTE
  64. jsontree_write_string(const struct jsontree_context *js_ctx, const char *text)
  65. {
  66. js_ctx->putinchar('"');
  67. if(text != NULL) {
  68. while(*text != '\0') {
  69. if(*text == '"') {
  70. js_ctx->putinchar('\\');
  71. }
  72. js_ctx->putinchar(*text++);
  73. }
  74. }
  75. js_ctx->putinchar('"');
  76. }
  77. /*---------------------------------------------------------------------------*/
  78. void FUNCTION_ATTRIBUTE
  79. jsontree_write_int(const struct jsontree_context *js_ctx, int value)
  80. {
  81. char buf[10];
  82. int l;
  83. if(value < 0) {
  84. js_ctx->putinchar('-');
  85. value = -value;
  86. }
  87. l = sizeof(buf) - 1;
  88. do {
  89. buf[l--] = '0' + (value % 10);
  90. value /= 10;
  91. } while(value > 0 && l >= 0);
  92. while(++l < sizeof(buf)) {
  93. js_ctx->putinchar(buf[l]);
  94. }
  95. }
  96. /*---------------------------------------------------------------------------*/
  97. void FUNCTION_ATTRIBUTE
  98. jsontree_write_int_array(const struct jsontree_context *js_ctx, const int *text, uint32_t length)
  99. {
  100. uint32_t i = 0;
  101. if(text == NULL) {
  102. js_ctx->putinchar('0');
  103. } else {
  104. for (i = 0; i < length - 1; i ++) {
  105. jsontree_write_int(js_ctx, *text++);
  106. js_ctx->putinchar(',');
  107. }
  108. jsontree_write_int(js_ctx, *text);
  109. }
  110. }
  111. /*---------------------------------------------------------------------------*/
  112. void FUNCTION_ATTRIBUTE
  113. jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root,
  114. int (* putchar)(int))
  115. {
  116. js_ctx->values[0] = root;
  117. js_ctx->putinchar = putchar;
  118. js_ctx->path = 0;
  119. jsontree_reset(js_ctx);
  120. }
  121. /*---------------------------------------------------------------------------*/
  122. void FUNCTION_ATTRIBUTE
  123. jsontree_reset(struct jsontree_context *js_ctx)
  124. {
  125. js_ctx->depth = 0;
  126. js_ctx->index[0] = 0;
  127. }
  128. /*---------------------------------------------------------------------------*/
  129. const char *FUNCTION_ATTRIBUTE
  130. jsontree_path_name(const struct jsontree_context *js_ctx, int depth)
  131. {
  132. if(depth < js_ctx->depth && js_ctx->values[depth]->type == JSON_TYPE_OBJECT) {
  133. return ((struct jsontree_object *)js_ctx->values[depth])->
  134. pairs[js_ctx->index[depth]].name;
  135. }
  136. return "";
  137. }
  138. /*---------------------------------------------------------------------------*/
  139. int FUNCTION_ATTRIBUTE
  140. jsontree_print_next(struct jsontree_context *js_ctx)
  141. {
  142. struct jsontree_value *v;
  143. int index;
  144. v = js_ctx->values[js_ctx->depth];
  145. /* Default operation after switch is to back up one level */
  146. switch(v->type) {
  147. case JSON_TYPE_OBJECT:
  148. case JSON_TYPE_ARRAY: {
  149. struct jsontree_array *o = (struct jsontree_array *)v;
  150. struct jsontree_value *ov;
  151. index = js_ctx->index[js_ctx->depth];
  152. if(index == 0) {
  153. js_ctx->putinchar(v->type);
  154. js_ctx->putinchar('\n');
  155. }
  156. if(index >= o->count) {
  157. js_ctx->putinchar('\n');
  158. js_ctx->putinchar(v->type + 2);
  159. /* Default operation: back up one level! */
  160. break;
  161. }
  162. if(index > 0) {
  163. js_ctx->putinchar(',');
  164. js_ctx->putinchar('\n');
  165. }
  166. if(v->type == JSON_TYPE_OBJECT) {
  167. jsontree_write_string(js_ctx,
  168. ((struct jsontree_object *)o)->pairs[index].name);
  169. js_ctx->putinchar(':');
  170. ov = ((struct jsontree_object *)o)->pairs[index].value;
  171. } else {
  172. ov = o->values[index];
  173. }
  174. /* TODO check max depth */
  175. js_ctx->depth++; /* step down to value... */
  176. js_ctx->index[js_ctx->depth] = 0; /* and init index */
  177. js_ctx->values[js_ctx->depth] = ov;
  178. /* Continue on this new level */
  179. return 1;
  180. }
  181. case JSON_TYPE_STRING:
  182. jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value);
  183. /* Default operation: back up one level! */
  184. break;
  185. case JSON_TYPE_INT:
  186. jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value);
  187. /* Default operation: back up one level! */
  188. break;
  189. case JSON_TYPE_CALLBACK: { /* pre-formatted json string currently */
  190. struct jsontree_callback *callback;
  191. callback = (struct jsontree_callback *)v;
  192. if(js_ctx->index[js_ctx->depth] == 0) {
  193. /* First call: reset the callback status */
  194. js_ctx->callback_state = 0;
  195. }
  196. if(callback->output == NULL) {
  197. jsontree_write_string(js_ctx, "");
  198. } else if(callback->output(js_ctx)) {
  199. /* The callback wants to output more */
  200. js_ctx->index[js_ctx->depth]++;
  201. return 1;
  202. }
  203. /* Default operation: back up one level! */
  204. break;
  205. }
  206. default:
  207. PRINTF("\nError: Illegal json type:'%c'\n", v->type);
  208. return 0;
  209. }
  210. /* Done => back up one level! */
  211. if(js_ctx->depth > 0) {
  212. js_ctx->depth--;
  213. js_ctx->index[js_ctx->depth]++;
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. /*---------------------------------------------------------------------------*/
  219. static struct jsontree_value *FUNCTION_ATTRIBUTE
  220. find_next(struct jsontree_context *js_ctx)
  221. {
  222. struct jsontree_value *v;
  223. int index;
  224. do {
  225. v = js_ctx->values[js_ctx->depth];
  226. /* Default operation after switch is to back up one level */
  227. switch(v->type) {
  228. case JSON_TYPE_OBJECT:
  229. case JSON_TYPE_ARRAY: {
  230. struct jsontree_array *o = (struct jsontree_array *)v;
  231. struct jsontree_value *ov;
  232. index = js_ctx->index[js_ctx->depth];
  233. if(index >= o->count) {
  234. /* Default operation: back up one level! */
  235. break;
  236. }
  237. if(v->type == JSON_TYPE_OBJECT) {
  238. ov = ((struct jsontree_object *)o)->pairs[index].value;
  239. } else {
  240. ov = o->values[index];
  241. }
  242. /* TODO check max depth */
  243. js_ctx->depth++; /* step down to value... */
  244. js_ctx->index[js_ctx->depth] = 0; /* and init index */
  245. js_ctx->values[js_ctx->depth] = ov;
  246. /* Continue on this new level */
  247. return ov;
  248. }
  249. default:
  250. /* Default operation: back up one level! */
  251. break;
  252. }
  253. /* Done => back up one level! */
  254. if(js_ctx->depth > 0) {
  255. js_ctx->depth--;
  256. js_ctx->index[js_ctx->depth]++;
  257. } else {
  258. return NULL;
  259. }
  260. } while(1);
  261. }
  262. /*---------------------------------------------------------------------------*/
  263. struct jsontree_value *FUNCTION_ATTRIBUTE
  264. jsontree_find_next(struct jsontree_context *js_ctx, int type)
  265. {
  266. struct jsontree_value *v;
  267. while((v = find_next(js_ctx)) != NULL && v->type != type &&
  268. js_ctx->path < js_ctx->depth) {
  269. /* search */
  270. }
  271. js_ctx->callback_state = 0;
  272. return js_ctx->path < js_ctx->depth ? v : NULL;
  273. }
  274. /*---------------------------------------------------------------------------*/