blob: 71db79eacf56e4211cce8b9667864fe5b781d905 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 *------------------------------------------------------------------
3 * json_format.h
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#ifndef __JSON_FORMAT_H__
21#define __JSON_FORMAT_H__
22
23#include <vppinfra/clib.h>
24#include <vppinfra/format.h>
25#include <netinet/ip.h>
26
27/* JSON value type */
Dave Barach72d72232016-08-04 10:15:08 -040028typedef enum
29{
30 VAT_JSON_NONE,
31 VAT_JSON_OBJECT,
32 VAT_JSON_ARRAY,
33 VAT_JSON_STRING,
34 VAT_JSON_REAL,
35 VAT_JSON_UINT,
36 VAT_JSON_INT,
37 VAT_JSON_IPV4,
38 VAT_JSON_IPV6,
39 VAT_JSON_MAX
Ed Warnickecb9cada2015-12-08 15:45:58 -070040} vat_json_val_type_t;
41
42typedef struct vat_json_node_s vat_json_node_t;
43typedef struct vat_json_pair_s vat_json_pair_t;
44
45/* JSON object structure */
Dave Barach72d72232016-08-04 10:15:08 -040046struct vat_json_node_s
47{
48 vat_json_val_type_t type;
49 union
50 {
51 vat_json_pair_t *pairs;
52 vat_json_node_t *array;
53 u8 *string;
54 struct in_addr ip4;
55 struct in6_addr ip6;
56 u64 uint;
57 i64 sint;
58 f64 real;
59 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070060};
61
Dave Barach72d72232016-08-04 10:15:08 -040062struct vat_json_pair_s
63{
64 const char *name;
65 vat_json_node_t value;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066};
67
Dave Barach72d72232016-08-04 10:15:08 -040068void vat_json_print (FILE * ofp, vat_json_node_t * node);
69void vat_json_free (vat_json_node_t * node);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Dave Barach72d72232016-08-04 10:15:08 -040071static_always_inline void
72vat_json_init_object (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Dave Barach72d72232016-08-04 10:15:08 -040074 json->type = VAT_JSON_OBJECT;
75 json->pairs = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076}
77
Dave Barach72d72232016-08-04 10:15:08 -040078static_always_inline void
79vat_json_init_array (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barach72d72232016-08-04 10:15:08 -040081 json->type = VAT_JSON_ARRAY;
82 json->array = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
Dave Barach72d72232016-08-04 10:15:08 -040085static_always_inline void
86vat_json_set_string (vat_json_node_t * json, u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Dave Barach72d72232016-08-04 10:15:08 -040088 json->type = VAT_JSON_STRING;
89 json->string = str;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090}
91
Dave Barach72d72232016-08-04 10:15:08 -040092static_always_inline void
93vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Dave Barach72d72232016-08-04 10:15:08 -040095 u8 *ns = NULL;
Dave Barach032a5e32020-04-06 10:19:18 -040096 int len = strlen ((const char *) str);
Andreas Schultz63551352020-04-07 13:40:36 +020097 vec_validate_init_c_string (ns, str, len);
Dave Barach72d72232016-08-04 10:15:08 -040098 vat_json_set_string (json, ns);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099}
100
Dave Barach72d72232016-08-04 10:15:08 -0400101static_always_inline void
102vat_json_set_int (vat_json_node_t * json, i64 num)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
Dave Barach72d72232016-08-04 10:15:08 -0400104 json->type = VAT_JSON_INT;
105 json->sint = num;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106}
107
Dave Barach72d72232016-08-04 10:15:08 -0400108static_always_inline void
109vat_json_set_uint (vat_json_node_t * json, u64 num)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110{
Dave Barach72d72232016-08-04 10:15:08 -0400111 json->type = VAT_JSON_UINT;
112 json->uint = num;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113}
114
Dave Barach72d72232016-08-04 10:15:08 -0400115static_always_inline void
116vat_json_set_real (vat_json_node_t * json, f64 real)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
Dave Barach72d72232016-08-04 10:15:08 -0400118 json->type = VAT_JSON_REAL;
119 json->real = real;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120}
121
Dave Barach72d72232016-08-04 10:15:08 -0400122static_always_inline void
123vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barach72d72232016-08-04 10:15:08 -0400125 json->type = VAT_JSON_IPV4;
126 json->ip4 = ip4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127}
128
Dave Barach72d72232016-08-04 10:15:08 -0400129static_always_inline void
130vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131{
Dave Barach72d72232016-08-04 10:15:08 -0400132 json->type = VAT_JSON_IPV6;
133 json->ip6 = ip6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134}
135
Dave Barach72d72232016-08-04 10:15:08 -0400136static_always_inline vat_json_node_t *
137vat_json_object_add (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138{
Dave Barach72d72232016-08-04 10:15:08 -0400139 ASSERT (VAT_JSON_OBJECT == json->type);
140 uword pos = vec_len (json->pairs);
141 vec_validate (json->pairs, pos);
142 json->pairs[pos].name = name;
143 return &json->pairs[pos].value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144}
145
Dave Barach72d72232016-08-04 10:15:08 -0400146static_always_inline vat_json_node_t *
147vat_json_array_add (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148{
Dave Barach72d72232016-08-04 10:15:08 -0400149 ASSERT (VAT_JSON_ARRAY == json->type);
150 uword pos = vec_len (json->array);
151 vec_validate (json->array, pos);
152 return &json->array[pos];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153}
154
Dave Barach72d72232016-08-04 10:15:08 -0400155static_always_inline vat_json_node_t *
156vat_json_object_add_list (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157{
Dave Barach72d72232016-08-04 10:15:08 -0400158 vat_json_node_t *array_node = vat_json_object_add (json, name);
159 vat_json_init_array (array_node);
160 return array_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161}
162
Dave Barach72d72232016-08-04 10:15:08 -0400163static_always_inline void
164vat_json_object_add_string_copy (vat_json_node_t * json,
165 const char *name, u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166{
Dave Barach72d72232016-08-04 10:15:08 -0400167 vat_json_set_string_copy (vat_json_object_add (json, name), str);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168}
169
Dave Barach72d72232016-08-04 10:15:08 -0400170static_always_inline void
171vat_json_object_add_uint (vat_json_node_t * json,
172 const char *name, u64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
Dave Barach72d72232016-08-04 10:15:08 -0400174 vat_json_set_uint (vat_json_object_add (json, name), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175}
176
Dave Barach72d72232016-08-04 10:15:08 -0400177static_always_inline void
178vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
Dave Barach72d72232016-08-04 10:15:08 -0400180 vat_json_set_int (vat_json_object_add (json, name), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181}
182
Dave Barach72d72232016-08-04 10:15:08 -0400183static_always_inline void
184vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185{
Dave Barach72d72232016-08-04 10:15:08 -0400186 vat_json_set_real (vat_json_object_add (json, name), real);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187}
188
Dave Barach72d72232016-08-04 10:15:08 -0400189static_always_inline void
190vat_json_object_add_ip4 (vat_json_node_t * json,
191 const char *name, struct in_addr ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192{
Dave Barach72d72232016-08-04 10:15:08 -0400193 vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194}
195
Dave Barach72d72232016-08-04 10:15:08 -0400196static_always_inline void
197vat_json_object_add_ip6 (vat_json_node_t * json,
198 const char *name, struct in6_addr ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199{
Dave Barach72d72232016-08-04 10:15:08 -0400200 vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201}
202
Dave Barach72d72232016-08-04 10:15:08 -0400203static_always_inline void
204vat_json_array_add_int (vat_json_node_t * json, i64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205{
Dave Barach72d72232016-08-04 10:15:08 -0400206 vat_json_set_int (vat_json_array_add (json), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207}
208
Dave Barach72d72232016-08-04 10:15:08 -0400209static_always_inline void
210vat_json_array_add_uint (vat_json_node_t * json, u64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211{
Dave Barach72d72232016-08-04 10:15:08 -0400212 vat_json_set_uint (vat_json_array_add (json), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213}
214
Dave Barach72d72232016-08-04 10:15:08 -0400215static_always_inline void
216vat_json_object_add_bytes (vat_json_node_t * json,
217 const char *name, u8 * array, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218{
Dave Barach72d72232016-08-04 10:15:08 -0400219 ASSERT (VAT_JSON_OBJECT == json->type);
220 vat_json_node_t *json_array = vat_json_object_add (json, name);
221 vat_json_init_array (json_array);
222 int i;
223 for (i = 0; i < size; i++)
224 {
225 vat_json_array_add_uint (json_array, array[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 }
227}
228
Dave Barach72d72232016-08-04 10:15:08 -0400229static_always_inline vat_json_node_t *
230vat_json_object_get_element (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
Dave Barach72d72232016-08-04 10:15:08 -0400232 int i = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Dave Barach72d72232016-08-04 10:15:08 -0400234 ASSERT (VAT_JSON_OBJECT == json->type);
235 for (i = 0; i < vec_len (json->pairs); i++)
236 {
237 if (0 == strcmp (json->pairs[i].name, name))
238 {
239 return &json->pairs[i].value;
240 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 }
Dave Barach72d72232016-08-04 10:15:08 -0400242 return NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243}
244
245#endif /* __JSON_FORMAT_H__ */
Dave Barach72d72232016-08-04 10:15:08 -0400246
247/*
248 * fd.io coding-style-patch-verification: ON
249 *
250 * Local Variables:
251 * eval: (c-set-style "gnu")
252 * End:
253 */