2 *------------------------------------------------------------------
5 * Copyright (c) 2015 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #ifndef __JSON_FORMAT_H__
21 #define __JSON_FORMAT_H__
23 #include <vppinfra/clib.h>
24 #include <vppinfra/format.h>
25 #include <netinet/ip.h>
40 } vat_json_val_type_t;
42 typedef struct vat_json_node_s vat_json_node_t;
43 typedef struct vat_json_pair_s vat_json_pair_t;
45 /* JSON object structure */
46 struct vat_json_node_s
48 vat_json_val_type_t type;
51 vat_json_pair_t *pairs;
52 vat_json_node_t *array;
62 struct vat_json_pair_s
65 vat_json_node_t value;
68 void vat_json_print (FILE * ofp, vat_json_node_t * node);
69 void vat_json_free (vat_json_node_t * node);
71 static_always_inline void
72 vat_json_init_object (vat_json_node_t * json)
74 json->type = VAT_JSON_OBJECT;
78 static_always_inline void
79 vat_json_init_array (vat_json_node_t * json)
81 json->type = VAT_JSON_ARRAY;
85 static_always_inline void
86 vat_json_set_string (vat_json_node_t * json, u8 * str)
88 json->type = VAT_JSON_STRING;
92 static_always_inline void
93 vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
96 vec_validate (ns, strlen ((const char *) str));
97 strcpy ((char *) ns, (const char *) str);
99 vat_json_set_string (json, ns);
102 static_always_inline void
103 vat_json_set_int (vat_json_node_t * json, i64 num)
105 json->type = VAT_JSON_INT;
109 static_always_inline void
110 vat_json_set_uint (vat_json_node_t * json, u64 num)
112 json->type = VAT_JSON_UINT;
116 static_always_inline void
117 vat_json_set_real (vat_json_node_t * json, f64 real)
119 json->type = VAT_JSON_REAL;
123 static_always_inline void
124 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
126 json->type = VAT_JSON_IPV4;
130 static_always_inline void
131 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
133 json->type = VAT_JSON_IPV6;
137 static_always_inline vat_json_node_t *
138 vat_json_object_add (vat_json_node_t * json, const char *name)
140 ASSERT (VAT_JSON_OBJECT == json->type);
141 uword pos = vec_len (json->pairs);
142 vec_validate (json->pairs, pos);
143 json->pairs[pos].name = name;
144 return &json->pairs[pos].value;
147 static_always_inline vat_json_node_t *
148 vat_json_array_add (vat_json_node_t * json)
150 ASSERT (VAT_JSON_ARRAY == json->type);
151 uword pos = vec_len (json->array);
152 vec_validate (json->array, pos);
153 return &json->array[pos];
156 static_always_inline vat_json_node_t *
157 vat_json_object_add_list (vat_json_node_t * json, const char *name)
159 vat_json_node_t *array_node = vat_json_object_add (json, name);
160 vat_json_init_array (array_node);
164 static_always_inline void
165 vat_json_object_add_string_copy (vat_json_node_t * json,
166 const char *name, u8 * str)
168 vat_json_set_string_copy (vat_json_object_add (json, name), str);
171 static_always_inline void
172 vat_json_object_add_uint (vat_json_node_t * json,
173 const char *name, u64 number)
175 vat_json_set_uint (vat_json_object_add (json, name), number);
178 static_always_inline void
179 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
181 vat_json_set_int (vat_json_object_add (json, name), number);
184 static_always_inline void
185 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
187 vat_json_set_real (vat_json_object_add (json, name), real);
190 static_always_inline void
191 vat_json_object_add_ip4 (vat_json_node_t * json,
192 const char *name, struct in_addr ip4)
194 vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
197 static_always_inline void
198 vat_json_object_add_ip6 (vat_json_node_t * json,
199 const char *name, struct in6_addr ip6)
201 vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
204 static_always_inline void
205 vat_json_array_add_int (vat_json_node_t * json, i64 number)
207 vat_json_set_int (vat_json_array_add (json), number);
210 static_always_inline void
211 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
213 vat_json_set_uint (vat_json_array_add (json), number);
216 static_always_inline void
217 vat_json_object_add_bytes (vat_json_node_t * json,
218 const char *name, u8 * array, uword size)
220 ASSERT (VAT_JSON_OBJECT == json->type);
221 vat_json_node_t *json_array = vat_json_object_add (json, name);
222 vat_json_init_array (json_array);
224 for (i = 0; i < size; i++)
226 vat_json_array_add_uint (json_array, array[i]);
230 static_always_inline vat_json_node_t *
231 vat_json_object_get_element (vat_json_node_t * json, const char *name)
235 ASSERT (VAT_JSON_OBJECT == json->type);
236 for (i = 0; i < vec_len (json->pairs); i++)
238 if (0 == strcmp (json->pairs[i].name, name))
240 return &json->pairs[i].value;
246 #endif /* __JSON_FORMAT_H__ */
249 * fd.io coding-style-patch-verification: ON
252 * eval: (c-set-style "gnu")