Contiki 3.x
jsontree.h
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 #ifndef JSONTREE_H_
41 #define JSONTREE_H_
42 
43 #include "contiki-conf.h"
44 #include "json.h"
45 
46 #ifdef JSONTREE_CONF_MAX_DEPTH
47 #define JSONTREE_MAX_DEPTH JSONTREE_CONF_MAX_DEPTH
48 #else
49 #define JSONTREE_MAX_DEPTH 10
50 #endif /* JSONTREE_CONF_MAX_DEPTH */
51 
52 struct jsontree_context {
53  struct jsontree_value *values[JSONTREE_MAX_DEPTH];
54  uint16_t index[JSONTREE_MAX_DEPTH];
55  int (* putchar)(int);
56  uint8_t depth;
57  uint8_t path;
58  int callback_state;
59 };
60 
61 struct jsontree_value {
62  uint8_t type;
63  /* followed by a value */
64 };
65 
66 struct jsontree_string {
67  uint8_t type;
68  const char *value;
69 };
70 
71 struct jsontree_int {
72  uint8_t type;
73  int value;
74 };
75 
76 /* NOTE: the jsontree_callback set will receive a jsonparse state */
77 struct jsonparse_state;
78 struct jsontree_callback {
79  uint8_t type;
80  int (* output)(struct jsontree_context *js_ctx);
81  int (* set)(struct jsontree_context *js_ctx, struct jsonparse_state *parser);
82 };
83 
84 struct jsontree_pair {
85  const char *name;
86  struct jsontree_value *value;
87 };
88 
89 struct jsontree_object {
90  uint8_t type;
91  uint8_t count;
92  struct jsontree_pair *pairs;
93 };
94 
95 struct jsontree_array {
96  uint8_t type;
97  uint8_t count;
98  struct jsontree_value **values;
99 };
100 
101 #define JSONTREE_STRING(text) {JSON_TYPE_STRING, (text)}
102 #define JSONTREE_PAIR(name, value) {(name), (struct jsontree_value *)(value)}
103 #define JSONTREE_CALLBACK(output, set) {JSON_TYPE_CALLBACK, (output), (set)}
104 
105 #define JSONTREE_OBJECT(name, ...) \
106  static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
107  static struct jsontree_object name = { \
108  JSON_TYPE_OBJECT, \
109  sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
110  jsontree_pair_##name }
111 
112 #define JSONTREE_OBJECT_EXT(name, ...) \
113  static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
114  struct jsontree_object name = { \
115  JSON_TYPE_OBJECT, \
116  sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
117  jsontree_pair_##name }
118 
119 #define JSONTREE_ARRAY(name, count) \
120  static struct jsontree_value *jsontree_value##name[count]; \
121  static struct jsontree_array name = { \
122  JSON_TYPE_ARRAY, \
123  count, \
124  jsontree_value##name }
125 
126 void jsontree_setup(struct jsontree_context *js_ctx,
127  struct jsontree_value *root, int (* putchar)(int));
128 void jsontree_reset(struct jsontree_context *js_ctx);
129 
130 const char *jsontree_path_name(const struct jsontree_context *js_ctx,
131  int depth);
132 
133 void jsontree_write_int(const struct jsontree_context *js_ctx, int value);
134 void jsontree_write_atom(const struct jsontree_context *js_ctx,
135  const char *text);
136 void jsontree_write_string(const struct jsontree_context *js_ctx,
137  const char *text);
138 int jsontree_print_next(struct jsontree_context *js_ctx);
139 struct jsontree_value *jsontree_find_next(struct jsontree_context *js_ctx,
140  int type);
141 
142 #endif /* JSONTREE_H_ */
A few JSON defines used for parsing and generating JSON.