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 int len = strlen ((const char *) str);
102 vec_validate_init_c_string (ns, str, len);
103 vat_json_set_string (json, ns);
106 static_always_inline void
107 vat_json_set_int (vat_json_node_t * json, i64 num)
109 json->type = VAT_JSON_INT;
113 static_always_inline void
114 vat_json_set_uint (vat_json_node_t * json, u64 num)
116 json->type = VAT_JSON_UINT;
120 static_always_inline void
121 vat_json_set_real (vat_json_node_t * json, f64 real)
123 json->type = VAT_JSON_REAL;
127 static_always_inline void
128 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
130 json->type = VAT_JSON_IPV4;
134 static_always_inline void
135 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
137 json->type = VAT_JSON_IPV6;
141 static_always_inline vat_json_node_t *
142 vat_json_object_add (vat_json_node_t * json, const char *name)
144 ASSERT (VAT_JSON_OBJECT == json->type);
145 uword pos = vec_len (json->pairs);
146 vec_validate (json->pairs, pos);
147 json->pairs[pos].name = name;
148 return &json->pairs[pos].value;
151 static_always_inline vat_json_node_t *
152 vat_json_array_add (vat_json_node_t * json)
154 ASSERT (VAT_JSON_ARRAY == json->type);
155 uword pos = vec_len (json->array);
156 vec_validate (json->array, pos);
157 return &json->array[pos];
160 static_always_inline vat_json_node_t *
161 vat_json_object_add_list (vat_json_node_t * json, const char *name)
163 vat_json_node_t *array_node = vat_json_object_add (json, name);
164 vat_json_init_array (array_node);
168 static_always_inline void
169 vat_json_object_add_string_copy (vat_json_node_t * json,
170 const char *name, u8 * str)
172 vat_json_set_string_copy (vat_json_object_add (json, name), str);
175 static_always_inline void
176 vat_json_object_add_uint (vat_json_node_t * json,
177 const char *name, u64 number)
179 vat_json_set_uint (vat_json_object_add (json, name), number);
182 static_always_inline void
183 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
185 vat_json_set_int (vat_json_object_add (json, name), number);
188 static_always_inline void
189 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
191 vat_json_set_real (vat_json_object_add (json, name), real);
194 static_always_inline void
195 vat_json_object_add_ip4 (vat_json_node_t * json,
196 const char *name, struct in_addr ip4)
198 vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
201 static_always_inline void
202 vat_json_object_add_ip6 (vat_json_node_t * json,
203 const char *name, struct in6_addr ip6)
205 vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
208 static_always_inline void
209 vat_json_array_add_int (vat_json_node_t * json, i64 number)
211 vat_json_set_int (vat_json_array_add (json), number);
214 static_always_inline void
215 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
217 vat_json_set_uint (vat_json_array_add (json), number);
220 static_always_inline void
221 vat_json_object_add_bytes (vat_json_node_t * json,
222 const char *name, u8 * array, uword size)
224 ASSERT (VAT_JSON_OBJECT == json->type);
225 vat_json_node_t *json_array = vat_json_object_add (json, name);
226 vat_json_init_array (json_array);
228 for (i = 0; i < size; i++)
230 vat_json_array_add_uint (json_array, array[i]);
234 static_always_inline vat_json_node_t *
235 vat_json_object_get_element (vat_json_node_t * json, const char *name)
239 ASSERT (VAT_JSON_OBJECT == json->type);
240 for (i = 0; i < vec_len (json->pairs); i++)
242 if (0 == strcmp (json->pairs[i].name, name))
244 return &json->pairs[i].value;
250 #endif /* __JSON_FORMAT_H__ */
253 * fd.io coding-style-patch-verification: ON
256 * eval: (c-set-style "gnu")