blob: 77128621d2154e544f19961e4127b5d36b9b9cc3 [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>
Tom Jones393bfa72024-01-30 09:47:05 +000025#ifdef __FreeBSD__
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#endif /* __FreeBSD__ */
Ed Warnickecb9cada2015-12-08 15:45:58 -070030#include <netinet/ip.h>
31
32/* JSON value type */
Dave Barach72d72232016-08-04 10:15:08 -040033typedef enum
34{
35 VAT_JSON_NONE,
36 VAT_JSON_OBJECT,
37 VAT_JSON_ARRAY,
38 VAT_JSON_STRING,
39 VAT_JSON_REAL,
40 VAT_JSON_UINT,
41 VAT_JSON_INT,
42 VAT_JSON_IPV4,
43 VAT_JSON_IPV6,
44 VAT_JSON_MAX
Ed Warnickecb9cada2015-12-08 15:45:58 -070045} vat_json_val_type_t;
46
47typedef struct vat_json_node_s vat_json_node_t;
48typedef struct vat_json_pair_s vat_json_pair_t;
49
50/* JSON object structure */
Dave Barach72d72232016-08-04 10:15:08 -040051struct vat_json_node_s
52{
53 vat_json_val_type_t type;
54 union
55 {
56 vat_json_pair_t *pairs;
57 vat_json_node_t *array;
58 u8 *string;
59 struct in_addr ip4;
60 struct in6_addr ip6;
61 u64 uint;
62 i64 sint;
63 f64 real;
64 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070065};
66
Dave Barach72d72232016-08-04 10:15:08 -040067struct vat_json_pair_s
68{
69 const char *name;
70 vat_json_node_t value;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071};
72
Dave Barach72d72232016-08-04 10:15:08 -040073void vat_json_print (FILE * ofp, vat_json_node_t * node);
74void vat_json_free (vat_json_node_t * node);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Dave Barach72d72232016-08-04 10:15:08 -040076static_always_inline void
77vat_json_init_object (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
Dave Barach72d72232016-08-04 10:15:08 -040079 json->type = VAT_JSON_OBJECT;
80 json->pairs = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081}
82
Dave Barach72d72232016-08-04 10:15:08 -040083static_always_inline void
84vat_json_init_array (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
Dave Barach72d72232016-08-04 10:15:08 -040086 json->type = VAT_JSON_ARRAY;
87 json->array = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088}
89
Dave Barach72d72232016-08-04 10:15:08 -040090static_always_inline void
91vat_json_set_string (vat_json_node_t * json, u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092{
Dave Barach72d72232016-08-04 10:15:08 -040093 json->type = VAT_JSON_STRING;
94 json->string = str;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095}
96
Dave Barach72d72232016-08-04 10:15:08 -040097static_always_inline void
98vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Dave Barach72d72232016-08-04 10:15:08 -0400100 u8 *ns = NULL;
Dave Barach032a5e32020-04-06 10:19:18 -0400101 int len = strlen ((const char *) str);
Andreas Schultz63551352020-04-07 13:40:36 +0200102 vec_validate_init_c_string (ns, str, len);
Dave Barach72d72232016-08-04 10:15:08 -0400103 vat_json_set_string (json, ns);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104}
105
Dave Barach72d72232016-08-04 10:15:08 -0400106static_always_inline void
107vat_json_set_int (vat_json_node_t * json, i64 num)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108{
Dave Barach72d72232016-08-04 10:15:08 -0400109 json->type = VAT_JSON_INT;
110 json->sint = num;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111}
112
Dave Barach72d72232016-08-04 10:15:08 -0400113static_always_inline void
114vat_json_set_uint (vat_json_node_t * json, u64 num)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barach72d72232016-08-04 10:15:08 -0400116 json->type = VAT_JSON_UINT;
117 json->uint = num;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118}
119
Dave Barach72d72232016-08-04 10:15:08 -0400120static_always_inline void
121vat_json_set_real (vat_json_node_t * json, f64 real)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Dave Barach72d72232016-08-04 10:15:08 -0400123 json->type = VAT_JSON_REAL;
124 json->real = real;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125}
126
Dave Barach72d72232016-08-04 10:15:08 -0400127static_always_inline void
128vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129{
Dave Barach72d72232016-08-04 10:15:08 -0400130 json->type = VAT_JSON_IPV4;
131 json->ip4 = ip4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132}
133
Dave Barach72d72232016-08-04 10:15:08 -0400134static_always_inline void
135vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136{
Dave Barach72d72232016-08-04 10:15:08 -0400137 json->type = VAT_JSON_IPV6;
138 json->ip6 = ip6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139}
140
Dave Barach72d72232016-08-04 10:15:08 -0400141static_always_inline vat_json_node_t *
142vat_json_object_add (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach72d72232016-08-04 10:15:08 -0400144 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149}
150
Dave Barach72d72232016-08-04 10:15:08 -0400151static_always_inline vat_json_node_t *
152vat_json_array_add (vat_json_node_t * json)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153{
Dave Barach72d72232016-08-04 10:15:08 -0400154 ASSERT (VAT_JSON_ARRAY == json->type);
155 uword pos = vec_len (json->array);
156 vec_validate (json->array, pos);
157 return &json->array[pos];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158}
159
Dave Barach72d72232016-08-04 10:15:08 -0400160static_always_inline vat_json_node_t *
161vat_json_object_add_list (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Dave Barach72d72232016-08-04 10:15:08 -0400163 vat_json_node_t *array_node = vat_json_object_add (json, name);
164 vat_json_init_array (array_node);
165 return array_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
Dave Barach72d72232016-08-04 10:15:08 -0400168static_always_inline void
169vat_json_object_add_string_copy (vat_json_node_t * json,
170 const char *name, u8 * str)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171{
Dave Barach72d72232016-08-04 10:15:08 -0400172 vat_json_set_string_copy (vat_json_object_add (json, name), str);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173}
174
Dave Barach72d72232016-08-04 10:15:08 -0400175static_always_inline void
176vat_json_object_add_uint (vat_json_node_t * json,
177 const char *name, u64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178{
Dave Barach72d72232016-08-04 10:15:08 -0400179 vat_json_set_uint (vat_json_object_add (json, name), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180}
181
Dave Barach72d72232016-08-04 10:15:08 -0400182static_always_inline void
183vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184{
Dave Barach72d72232016-08-04 10:15:08 -0400185 vat_json_set_int (vat_json_object_add (json, name), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186}
187
Dave Barach72d72232016-08-04 10:15:08 -0400188static_always_inline void
189vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Dave Barach72d72232016-08-04 10:15:08 -0400191 vat_json_set_real (vat_json_object_add (json, name), real);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192}
193
Dave Barach72d72232016-08-04 10:15:08 -0400194static_always_inline void
195vat_json_object_add_ip4 (vat_json_node_t * json,
196 const char *name, struct in_addr ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
Dave Barach72d72232016-08-04 10:15:08 -0400198 vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199}
200
Dave Barach72d72232016-08-04 10:15:08 -0400201static_always_inline void
202vat_json_object_add_ip6 (vat_json_node_t * json,
203 const char *name, struct in6_addr ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
Dave Barach72d72232016-08-04 10:15:08 -0400205 vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206}
207
Dave Barach72d72232016-08-04 10:15:08 -0400208static_always_inline void
209vat_json_array_add_int (vat_json_node_t * json, i64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210{
Dave Barach72d72232016-08-04 10:15:08 -0400211 vat_json_set_int (vat_json_array_add (json), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212}
213
Dave Barach72d72232016-08-04 10:15:08 -0400214static_always_inline void
215vat_json_array_add_uint (vat_json_node_t * json, u64 number)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216{
Dave Barach72d72232016-08-04 10:15:08 -0400217 vat_json_set_uint (vat_json_array_add (json), number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218}
219
Dave Barach72d72232016-08-04 10:15:08 -0400220static_always_inline void
221vat_json_object_add_bytes (vat_json_node_t * json,
222 const char *name, u8 * array, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
Dave Barach72d72232016-08-04 10:15:08 -0400224 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);
227 int i;
228 for (i = 0; i < size; i++)
229 {
230 vat_json_array_add_uint (json_array, array[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 }
232}
233
Dave Barach72d72232016-08-04 10:15:08 -0400234static_always_inline vat_json_node_t *
235vat_json_object_get_element (vat_json_node_t * json, const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236{
Dave Barach72d72232016-08-04 10:15:08 -0400237 int i = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barach72d72232016-08-04 10:15:08 -0400239 ASSERT (VAT_JSON_OBJECT == json->type);
240 for (i = 0; i < vec_len (json->pairs); i++)
241 {
242 if (0 == strcmp (json->pairs[i].name, name))
243 {
244 return &json->pairs[i].value;
245 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 }
Dave Barach72d72232016-08-04 10:15:08 -0400247 return NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248}
249
250#endif /* __JSON_FORMAT_H__ */
Dave Barach72d72232016-08-04 10:15:08 -0400251
252/*
253 * fd.io coding-style-patch-verification: ON
254 *
255 * Local Variables:
256 * eval: (c-set-style "gnu")
257 * End:
258 */