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