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  if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) {
202  /* Too deep: return 0 */
203  return 0;
204  }
205  js_ctx->depth++; /* step down to value... */
206  js_ctx->index[js_ctx->depth] = 0; /* and init index */
207  js_ctx->values[js_ctx->depth] = ov;
208  /* Continue on this new level */
209  return 1;
210  }
211  case JSON_TYPE_STRING:
212  jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value);
213  /* Default operation: back up one level! */
214  break;
215  case JSON_TYPE_UINT:
216  jsontree_write_uint(js_ctx, ((struct jsontree_uint *)v)->value);
217  /* Default operation: back up one level! */
218  break;
219  case JSON_TYPE_INT:
220  jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value);
221  /* Default operation: back up one level! */
222  break;
223  case JSON_TYPE_CALLBACK: { /* pre-formatted json string currently */
224  struct jsontree_callback *callback;
225 
226  callback = (struct jsontree_callback *)v;
227  if(js_ctx->index[js_ctx->depth] == 0) {
228  /* First call: reset the callback status */
229  js_ctx->callback_state = 0;
230  }
231  if(callback->output == NULL) {
232  jsontree_write_string(js_ctx, "");
233  } else if(callback->output(js_ctx)) {
234  /* The callback wants to output more */
235  js_ctx->index[js_ctx->depth]++;
236  return 1;
237  }
238  /* Default operation: back up one level! */
239  break;
240  case JSON_TYPE_S8PTR:
241  jsontree_write_int(js_ctx, *((int8_t *)((struct jsontree_ptr *)v)->value));
242  /* Default operation: back up one level! */
243  break;
244  case JSON_TYPE_U8PTR:
245  jsontree_write_uint(js_ctx, *((uint8_t *)((struct jsontree_ptr *)v)->value));
246  /* Default operation: back up one level! */
247  break;
248  case JSON_TYPE_S16PTR:
249  jsontree_write_int(js_ctx, *((int16_t *)((struct jsontree_ptr *)v)->value));
250  /* Default operation: back up one level! */
251  break;
252  case JSON_TYPE_U16PTR:
253  jsontree_write_uint(js_ctx, *((uint16_t *)((struct jsontree_ptr *)v)->value));
254  /* Default operation: back up one level! */
255  break;
256  case JSON_TYPE_S32PTR:
257  jsontree_write_int(js_ctx, *((int32_t *)((struct jsontree_ptr *)v)->value));
258  /* Default operation: back up one level! */
259  break;
260  case JSON_TYPE_U32PTR:
261  jsontree_write_uint(js_ctx, *((uint32_t *)((struct jsontree_ptr *)v)->value));
262  /* Default operation: back up one level! */
263  break;
264  }
265  default:
266  PRINTF("\nError: Illegal json type:'%c'\n", v->type);
267  return 0;
268  }
269  /* Done => back up one level! */
270  if(js_ctx->depth > 0) {
271  js_ctx->depth--;
272  js_ctx->index[js_ctx->depth]++;
273  return 1;
274  }
275  return 0;
276 }
277 /*---------------------------------------------------------------------------*/
278 static struct jsontree_value *
279 find_next(struct jsontree_context *js_ctx)
280 {
281  struct jsontree_value *v;
282  int index;
283 
284  do {
285  v = js_ctx->values[js_ctx->depth];
286 
287  /* Default operation after switch is to back up one level */
288  switch(v->type) {
289  case JSON_TYPE_OBJECT:
290  case JSON_TYPE_ARRAY: {
291  struct jsontree_array *o = (struct jsontree_array *)v;
292  struct jsontree_value *ov;
293 
294  index = js_ctx->index[js_ctx->depth];
295  if(index >= o->count) {
296  /* Default operation: back up one level! */
297  break;
298  }
299 
300  if(v->type == JSON_TYPE_OBJECT) {
301  ov = ((struct jsontree_object *)o)->pairs[index].value;
302  } else {
303  ov = o->values[index];
304  }
305  if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) {
306  /* Too deep: return NULL */
307  return NULL;
308  }
309  js_ctx->depth++; /* step down to value... */
310  js_ctx->index[js_ctx->depth] = 0; /* and init index */
311  js_ctx->values[js_ctx->depth] = ov;
312  /* Continue on this new level */
313  return ov;
314  }
315  default:
316  /* Default operation: back up one level! */
317  break;
318  }
319  /* Done => back up one level! */
320  if(js_ctx->depth > 0) {
321  js_ctx->depth--;
322  js_ctx->index[js_ctx->depth]++;
323  } else {
324  return NULL;
325  }
326  } while(1);
327 }
328 /*---------------------------------------------------------------------------*/
329 struct jsontree_value *
330 jsontree_find_next(struct jsontree_context *js_ctx, int type)
331 {
332  struct jsontree_value *v;
333 
334  while((v = find_next(js_ctx)) != NULL && v->type != type &&
335  js_ctx->path < js_ctx->depth) {
336  /* search */
337  }
338  js_ctx->callback_state = 0;
339  return js_ctx->path < js_ctx->depth ? v : NULL;
340 }
341 /*---------------------------------------------------------------------------*/
JSON output generation