Contiki 3.x
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_int(const struct jsontree_context *js_ctx, int value)
83 {
84  char buf[10];
85  int l;
86 
87  if(value < 0) {
88  js_ctx->putchar('-');
89  value = -value;
90  }
91 
92  l = sizeof(buf) - 1;
93  do {
94  buf[l--] = '0' + (value % 10);
95  value /= 10;
96  } while(value > 0 && l >= 0);
97 
98  while(++l < sizeof(buf)) {
99  js_ctx->putchar(buf[l]);
100  }
101 }
102 /*---------------------------------------------------------------------------*/
103 void
104 jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root,
105  int (* putchar)(int))
106 {
107  js_ctx->values[0] = root;
108  js_ctx->putchar = putchar;
109  js_ctx->path = 0;
110  jsontree_reset(js_ctx);
111 }
112 /*---------------------------------------------------------------------------*/
113 void
114 jsontree_reset(struct jsontree_context *js_ctx)
115 {
116  js_ctx->depth = 0;
117  js_ctx->index[0] = 0;
118 }
119 /*---------------------------------------------------------------------------*/
120 const char *
121 jsontree_path_name(const struct jsontree_context *js_ctx, int depth)
122 {
123  if(depth < js_ctx->depth && js_ctx->values[depth]->type == JSON_TYPE_OBJECT) {
124  return ((struct jsontree_object *)js_ctx->values[depth])->
125  pairs[js_ctx->index[depth]].name;
126  }
127  return "";
128 }
129 /*---------------------------------------------------------------------------*/
130 int
131 jsontree_print_next(struct jsontree_context *js_ctx)
132 {
133  struct jsontree_value *v;
134  int index;
135 
136  v = js_ctx->values[js_ctx->depth];
137 
138  /* Default operation after switch is to back up one level */
139  switch(v->type) {
140  case JSON_TYPE_OBJECT:
141  case JSON_TYPE_ARRAY: {
142  struct jsontree_array *o = (struct jsontree_array *)v;
143  struct jsontree_value *ov;
144 
145  index = js_ctx->index[js_ctx->depth];
146  if(index == 0) {
147  js_ctx->putchar(v->type);
148  js_ctx->putchar('\n');
149  }
150  if(index >= o->count) {
151  js_ctx->putchar('\n');
152  js_ctx->putchar(v->type + 2);
153  /* Default operation: back up one level! */
154  break;
155  }
156 
157  if(index > 0) {
158  js_ctx->putchar(',');
159  js_ctx->putchar('\n');
160  }
161  if(v->type == JSON_TYPE_OBJECT) {
162  jsontree_write_string(js_ctx,
163  ((struct jsontree_object *)o)->pairs[index].name);
164  js_ctx->putchar(':');
165  ov = ((struct jsontree_object *)o)->pairs[index].value;
166  } else {
167  ov = o->values[index];
168  }
169  /* TODO check max depth */
170  js_ctx->depth++; /* step down to value... */
171  js_ctx->index[js_ctx->depth] = 0; /* and init index */
172  js_ctx->values[js_ctx->depth] = ov;
173  /* Continue on this new level */
174  return 1;
175  }
176  case JSON_TYPE_STRING:
177  jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value);
178  /* Default operation: back up one level! */
179  break;
180  case JSON_TYPE_INT:
181  jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value);
182  /* Default operation: back up one level! */
183  break;
184  case JSON_TYPE_CALLBACK: { /* pre-formatted json string currently */
185  struct jsontree_callback *callback;
186 
187  callback = (struct jsontree_callback *)v;
188  if(js_ctx->index[js_ctx->depth] == 0) {
189  /* First call: reset the callback status */
190  js_ctx->callback_state = 0;
191  }
192  if(callback->output == NULL) {
193  jsontree_write_string(js_ctx, "");
194  } else if(callback->output(js_ctx)) {
195  /* The callback wants to output more */
196  js_ctx->index[js_ctx->depth]++;
197  return 1;
198  }
199  /* Default operation: back up one level! */
200  break;
201  }
202  default:
203  PRINTF("\nError: Illegal json type:'%c'\n", v->type);
204  return 0;
205  }
206  /* Done => back up one level! */
207  if(js_ctx->depth > 0) {
208  js_ctx->depth--;
209  js_ctx->index[js_ctx->depth]++;
210  return 1;
211  }
212  return 0;
213 }
214 /*---------------------------------------------------------------------------*/
215 static struct jsontree_value *
216 find_next(struct jsontree_context *js_ctx)
217 {
218  struct jsontree_value *v;
219  int index;
220 
221  do {
222  v = js_ctx->values[js_ctx->depth];
223 
224  /* Default operation after switch is to back up one level */
225  switch(v->type) {
226  case JSON_TYPE_OBJECT:
227  case JSON_TYPE_ARRAY: {
228  struct jsontree_array *o = (struct jsontree_array *)v;
229  struct jsontree_value *ov;
230 
231  index = js_ctx->index[js_ctx->depth];
232  if(index >= o->count) {
233  /* Default operation: back up one level! */
234  break;
235  }
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 *
264 jsontree_find_next(struct jsontree_context *js_ctx, int type)
265 {
266  struct jsontree_value *v;
267 
268  while((v = find_next(js_ctx)) != NULL && v->type != type &&
269  js_ctx->path < js_ctx->depth) {
270  /* search */
271  }
272  js_ctx->callback_state = 0;
273  return js_ctx->path < js_ctx->depth ? v : NULL;
274 }
275 /*---------------------------------------------------------------------------*/
#define NULL
The null pointer.
JSON output generation