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>
39 } vat_json_val_type_t;
41 typedef struct vat_json_node_s vat_json_node_t;
42 typedef struct vat_json_pair_s vat_json_pair_t;
44 /* JSON object structure */
45 struct vat_json_node_s {
46 vat_json_val_type_t type;
48 vat_json_pair_t * pairs;
49 vat_json_node_t * array;
59 struct vat_json_pair_s {
61 vat_json_node_t value;
64 void vat_json_print(FILE *ofp, vat_json_node_t *node);
65 void vat_json_free(vat_json_node_t *node);
67 static_always_inline void vat_json_init_object(vat_json_node_t *json)
69 json->type = VAT_JSON_OBJECT;
73 static_always_inline void vat_json_init_array(vat_json_node_t *json)
75 json->type = VAT_JSON_ARRAY;
79 static_always_inline void vat_json_set_string(vat_json_node_t *json, u8 *str)
81 json->type = VAT_JSON_STRING;
85 static_always_inline void vat_json_set_string_copy(vat_json_node_t *json,
89 vec_validate(ns, strlen((const char *)str));
90 strcpy((char*)ns, (const char*)str);
92 vat_json_set_string(json, ns);
95 static_always_inline void vat_json_set_int(vat_json_node_t *json, i64 num)
97 json->type = VAT_JSON_INT;
101 static_always_inline void vat_json_set_uint(vat_json_node_t *json, u64 num)
103 json->type = VAT_JSON_UINT;
107 static_always_inline void vat_json_set_real(vat_json_node_t *json, f64 real)
109 json->type = VAT_JSON_REAL;
113 static_always_inline void vat_json_set_ip4(vat_json_node_t *json,
116 json->type = VAT_JSON_IPV4;
120 static_always_inline void vat_json_set_ip6(vat_json_node_t *json,
123 json->type = VAT_JSON_IPV6;
127 static_always_inline vat_json_node_t* vat_json_object_add(vat_json_node_t *json,
130 ASSERT(VAT_JSON_OBJECT == json->type);
131 uword pos = vec_len(json->pairs);
132 vec_validate(json->pairs, pos);
133 json->pairs[pos].name = name;
134 return &json->pairs[pos].value;
137 static_always_inline vat_json_node_t* vat_json_array_add(vat_json_node_t *json)
139 ASSERT(VAT_JSON_ARRAY == json->type);
140 uword pos = vec_len(json->array);
141 vec_validate(json->array, pos);
142 return &json->array[pos];
145 static_always_inline vat_json_node_t* vat_json_object_add_list(vat_json_node_t *json,
148 vat_json_node_t *array_node = vat_json_object_add(json, name);
149 vat_json_init_array(array_node);
153 static_always_inline void vat_json_object_add_string_copy(vat_json_node_t *json,
157 vat_json_set_string_copy(vat_json_object_add(json, name), str);
160 static_always_inline void vat_json_object_add_uint(vat_json_node_t *json,
164 vat_json_set_uint(vat_json_object_add(json, name), number);
167 static_always_inline void vat_json_object_add_int(vat_json_node_t *json,
171 vat_json_set_int(vat_json_object_add(json, name), number);
174 static_always_inline void vat_json_object_add_real(vat_json_node_t *json,
175 const char *name, f64 real)
177 vat_json_set_real(vat_json_object_add(json, name), real);
180 static_always_inline void vat_json_object_add_ip4(vat_json_node_t *json,
184 vat_json_set_ip4(vat_json_object_add(json, name), ip4);
187 static_always_inline void vat_json_object_add_ip6(vat_json_node_t *json,
191 vat_json_set_ip6(vat_json_object_add(json, name), ip6);
194 static_always_inline void vat_json_array_add_int(vat_json_node_t *json,
197 vat_json_set_int(vat_json_array_add(json), number);
200 static_always_inline void vat_json_array_add_uint(vat_json_node_t *json,
203 vat_json_set_uint(vat_json_array_add(json), number);
206 static_always_inline void vat_json_object_add_bytes(vat_json_node_t *json,
208 u8 *array, uword size)
210 ASSERT(VAT_JSON_OBJECT == json->type);
211 vat_json_node_t *json_array = vat_json_object_add(json, name);
212 vat_json_init_array(json_array);
214 for (i = 0; i < size; i++) {
215 vat_json_array_add_uint(json_array, array[i]);
219 static_always_inline vat_json_node_t*
220 vat_json_object_get_element(vat_json_node_t *json, const char *name)
224 ASSERT(VAT_JSON_OBJECT == json->type);
225 for (i = 0; i < vec_len(json->pairs); i++) {
226 if (0 == strcmp(json->pairs[i].name, name)) {
227 return &json->pairs[i].value;
233 #endif /* __JSON_FORMAT_H__ */