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>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #endif /* __FreeBSD__ */
30 #include <netinet/ip.h>
45 } vat_json_val_type_t;
47 typedef struct vat_json_node_s vat_json_node_t;
48 typedef struct vat_json_pair_s vat_json_pair_t;
50 /* JSON object structure */
51 struct vat_json_node_s
53 vat_json_val_type_t type;
56 vat_json_pair_t *pairs;
57 vat_json_node_t *array;
67 struct vat_json_pair_s
70 vat_json_node_t value;
73 void vat_json_print (FILE * ofp, vat_json_node_t * node);
74 void vat_json_free (vat_json_node_t * node);
76 static_always_inline void
77 vat_json_init_object (vat_json_node_t * json)
79 json->type = VAT_JSON_OBJECT;
83 static_always_inline void
84 vat_json_init_array (vat_json_node_t * json)
86 json->type = VAT_JSON_ARRAY;
90 static_always_inline void
91 vat_json_set_string (vat_json_node_t * json, u8 * str)
93 json->type = VAT_JSON_STRING;
97 static_always_inline void
98 vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
101 vec_validate (ns, strlen ((const char *) str));
102 strncpy ((char *) ns, (const char *) str, vec_len (ns));
104 vat_json_set_string (json, ns);
107 static_always_inline void
108 vat_json_set_int (vat_json_node_t * json, i64 num)
110 json->type = VAT_JSON_INT;
114 static_always_inline void
115 vat_json_set_uint (vat_json_node_t * json, u64 num)
117 json->type = VAT_JSON_UINT;
121 static_always_inline void
122 vat_json_set_real (vat_json_node_t * json, f64 real)
124 json->type = VAT_JSON_REAL;
128 static_always_inline void
129 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
131 json->type = VAT_JSON_IPV4;
135 static_always_inline void
136 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
138 json->type = VAT_JSON_IPV6;
142 static_always_inline vat_json_node_t *
143 vat_json_object_add (vat_json_node_t * json, const char *name)
145 ASSERT (VAT_JSON_OBJECT == json->type);
146 uword pos = vec_len (json->pairs);
147 vec_validate (json->pairs, pos);
148 json->pairs[pos].name = name;
149 return &json->pairs[pos].value;
152 static_always_inline vat_json_node_t *
153 vat_json_array_add (vat_json_node_t * json)
155 ASSERT (VAT_JSON_ARRAY == json->type);
156 uword pos = vec_len (json->array);
157 vec_validate (json->array, pos);
158 return &json->array[pos];
161 static_always_inline vat_json_node_t *
162 vat_json_object_add_list (vat_json_node_t * json, const char *name)
164 vat_json_node_t *array_node = vat_json_object_add (json, name);
165 vat_json_init_array (array_node);
169 static_always_inline void
170 vat_json_object_add_string_copy (vat_json_node_t * json,
171 const char *name, u8 * str)
173 vat_json_set_string_copy (vat_json_object_add (json, name), str);
176 static_always_inline void
177 vat_json_object_add_uint (vat_json_node_t * json,
178 const char *name, u64 number)
180 vat_json_set_uint (vat_json_object_add (json, name), number);
183 static_always_inline void
184 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
186 vat_json_set_int (vat_json_object_add (json, name), number);
189 static_always_inline void
190 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
192 vat_json_set_real (vat_json_object_add (json, name), real);
195 static_always_inline void
196 vat_json_object_add_ip4 (vat_json_node_t * json,
197 const char *name, struct in_addr ip4)
199 vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
202 static_always_inline void
203 vat_json_object_add_ip6 (vat_json_node_t * json,
204 const char *name, struct in6_addr ip6)
206 vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
209 static_always_inline void
210 vat_json_array_add_int (vat_json_node_t * json, i64 number)
212 vat_json_set_int (vat_json_array_add (json), number);
215 static_always_inline void
216 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
218 vat_json_set_uint (vat_json_array_add (json), number);
221 static_always_inline void
222 vat_json_object_add_bytes (vat_json_node_t * json,
223 const char *name, u8 * array, uword size)
225 ASSERT (VAT_JSON_OBJECT == json->type);
226 vat_json_node_t *json_array = vat_json_object_add (json, name);
227 vat_json_init_array (json_array);
229 for (i = 0; i < size; i++)
231 vat_json_array_add_uint (json_array, array[i]);
235 static_always_inline vat_json_node_t *
236 vat_json_object_get_element (vat_json_node_t * json, const char *name)
240 ASSERT (VAT_JSON_OBJECT == json->type);
241 for (i = 0; i < vec_len (json->pairs); i++)
243 if (0 == strcmp (json->pairs[i].name, name))
245 return &json->pairs[i].value;
251 #endif /* __JSON_FORMAT_H__ */
254 * fd.io coding-style-patch-verification: ON
257 * eval: (c-set-style "gnu")