MeterLogger
sdk
include
json
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 "
c_types.h
"
44
#include "
json/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_PAIR_ARRAY(value) (struct jsontree_value *)(value)
113
#define JSONTREE_ARRAY(name, ...) \
114
static struct jsontree_value* jsontree_value_##name[] = {__VA_ARGS__}; \
115
static struct jsontree_array name = { \
116
JSON_TYPE_ARRAY, \
117
sizeof(jsontree_value_##name)/sizeof(struct jsontree_value*), \
118
jsontree_value_##name }
119
120
#define JSONTREE_OBJECT_EXT(name, ...) \
121
static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
122
struct jsontree_object name = { \
123
JSON_TYPE_OBJECT, \
124
sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
125
jsontree_pair_##name }
126
127
void
jsontree_setup
(
struct
jsontree_context
*js_ctx,
128
struct
jsontree_value
*root,
int
(*
putchar
)(
int
));
129
void
jsontree_reset
(
struct
jsontree_context
*js_ctx);
130
131
const
char
*
jsontree_path_name
(
const
struct
jsontree_context
*js_ctx,
132
int
depth
);
133
134
void
jsontree_write_int
(
const
struct
jsontree_context
*js_ctx,
int
value);
135
void
jsontree_write_int_array
(
const
struct
jsontree_context
*js_ctx,
const
int
*text,
uint32
length);
136
137
void
jsontree_write_atom
(
const
struct
jsontree_context
*js_ctx,
138
const
char
*text);
139
void
jsontree_write_string
(
const
struct
jsontree_context
*js_ctx,
140
const
char
*text);
141
int
jsontree_print_next
(
struct
jsontree_context
*js_ctx);
142
struct
jsontree_value
*
jsontree_find_next
(
struct
jsontree_context
*js_ctx,
143
int
type
);
144
145
#endif
/* __JSONTREE_H__ */
jsontree_string::value
const char * value
Definition:
jsontree.h:68
c_types.h
jsontree_context::values
struct jsontree_value * values[JSONTREE_MAX_DEPTH]
Definition:
jsontree.h:53
jsontree_pair::value
struct jsontree_value * value
Definition:
jsontree.h:86
jsontree_context::index
uint16_t index[JSONTREE_MAX_DEPTH]
Definition:
jsontree.h:54
jsontree_write_int_array
void jsontree_write_int_array(const struct jsontree_context *js_ctx, const int *text, uint32 length)
jsontree_context::callback_state
int callback_state
Definition:
jsontree.h:58
jsontree_array::count
uint8_t count
Definition:
jsontree.h:97
jsontree_string
Definition:
jsontree.h:66
jsonparse_state
Definition:
jsonparse.h:44
jsontree_pair::name
const char * name
Definition:
jsontree.h:85
jsontree_path_name
const char * jsontree_path_name(const struct jsontree_context *js_ctx, int depth)
jsontree_int::type
uint8_t type
Definition:
jsontree.h:72
jsontree_int::value
int value
Definition:
jsontree.h:73
jsontree_object::type
uint8_t type
Definition:
jsontree.h:90
jsontree_callback::type
uint8_t type
Definition:
jsontree.h:79
jsontree_write_string
void jsontree_write_string(const struct jsontree_context *js_ctx, const char *text)
jsontree_array
Definition:
jsontree.h:95
jsontree_string::type
uint8_t type
Definition:
jsontree.h:67
json.h
jsontree_value::type
uint8_t type
Definition:
jsontree.h:62
jsontree_pair
Definition:
jsontree.h:84
jsontree_write_int
void jsontree_write_int(const struct jsontree_context *js_ctx, int value)
jsontree_setup
void jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root, int(*putchar)(int))
jsontree_value
Definition:
jsontree.h:61
jsontree_reset
void jsontree_reset(struct jsontree_context *js_ctx)
jsontree_object
Definition:
jsontree.h:89
jsontree_context::depth
uint8_t depth
Definition:
jsontree.h:56
jsontree_find_next
struct jsontree_value * jsontree_find_next(struct jsontree_context *js_ctx, int type)
uint32
unsigned int uint32
Definition:
c_types.h:54
jsontree_context::path
uint8_t path
Definition:
jsontree.h:57
jsontree_object::pairs
struct jsontree_pair * pairs
Definition:
jsontree.h:92
jsontree_object::count
uint8_t count
Definition:
jsontree.h:91
jsontree_int
Definition:
jsontree.h:71
jsontree_print_next
int jsontree_print_next(struct jsontree_context *js_ctx)
jsontree_write_atom
void jsontree_write_atom(const struct jsontree_context *js_ctx, const char *text)
jsontree_callback
Definition:
jsontree.h:78
JSONTREE_MAX_DEPTH
#define JSONTREE_MAX_DEPTH
Definition:
jsontree.h:49
jsontree_context
Definition:
jsontree.h:52
jsontree_array::values
struct jsontree_value ** values
Definition:
jsontree.h:98
jsontree_array::type
uint8_t type
Definition:
jsontree.h:96
jsontree_context::putchar
int(* putchar)(int)
Definition:
jsontree.h:55
Generated by
1.8.13