FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
json_format.h
Go to the documentation of this file.
1 /*
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 */
28 typedef enum
29 {
41 
44 
45 /* JSON object structure */
47 {
49  union
50  {
54  struct in_addr ip4;
55  struct in6_addr ip6;
59  };
60 };
61 
63 {
64  const char *name;
66 };
67 
68 void vat_json_print (FILE * ofp, vat_json_node_t * node);
70 
73 {
74  json->type = VAT_JSON_OBJECT;
75  json->pairs = NULL;
76 }
77 
80 {
81  json->type = VAT_JSON_ARRAY;
82  json->array = NULL;
83 }
84 
87 {
88  json->type = VAT_JSON_STRING;
89  json->string = str;
90 }
91 
94 {
95  u8 *ns = NULL;
96  vec_validate (ns, strlen ((const char *) str));
97  strncpy ((char *) ns, (const char *) str, vec_len (ns));
98  vec_add1 (ns, '\0');
99  vat_json_set_string (json, ns);
100 }
101 
104 {
105  json->type = VAT_JSON_INT;
106  json->sint = num;
107 }
108 
111 {
112  json->type = VAT_JSON_UINT;
113  json->uint = num;
114 }
115 
118 {
119  json->type = VAT_JSON_REAL;
120  json->real = real;
121 }
122 
124 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
125 {
126  json->type = VAT_JSON_IPV4;
127  json->ip4 = ip4;
128 }
129 
131 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
132 {
133  json->type = VAT_JSON_IPV6;
134  json->ip6 = ip6;
135 }
136 
139 {
140  ASSERT (VAT_JSON_OBJECT == json->type);
141  uword pos = vec_len (json->pairs);
142  vec_validate (json->pairs, pos);
143  json->pairs[pos].name = name;
144  return &json->pairs[pos].value;
145 }
146 
149 {
150  ASSERT (VAT_JSON_ARRAY == json->type);
151  uword pos = vec_len (json->array);
152  vec_validate (json->array, pos);
153  return &json->array[pos];
154 }
155 
158 {
159  vat_json_node_t *array_node = vat_json_object_add (json, name);
160  vat_json_init_array (array_node);
161  return array_node;
162 }
163 
166  const char *name, u8 * str)
167 {
169 }
170 
173  const char *name, u64 number)
174 {
176 }
177 
180 {
182 }
183 
186 {
188 }
189 
192  const char *name, struct in_addr ip4)
193 {
195 }
196 
199  const char *name, struct in6_addr ip6)
200 {
202 }
203 
206 {
208 }
209 
212 {
214 }
215 
218  const char *name, u8 * array, uword size)
219 {
220  ASSERT (VAT_JSON_OBJECT == json->type);
221  vat_json_node_t *json_array = vat_json_object_add (json, name);
222  vat_json_init_array (json_array);
223  int i;
224  for (i = 0; i < size; i++)
225  {
226  vat_json_array_add_uint (json_array, array[i]);
227  }
228 }
229 
232 {
233  int i = 0;
234 
235  ASSERT (VAT_JSON_OBJECT == json->type);
236  for (i = 0; i < vec_len (json->pairs); i++)
237  {
238  if (0 == strcmp (json->pairs[i].name, name))
239  {
240  return &json->pairs[i].value;
241  }
242  }
243  return NULL;
244 }
245 
246 #endif /* __JSON_FORMAT_H__ */
247 
248 /*
249  * fd.io coding-style-patch-verification: ON
250  *
251  * Local Variables:
252  * eval: (c-set-style "gnu")
253  * End:
254  */
vat_json_val_type_t
vat_json_val_type_t
Definition: json_format.h:28
vat_json_node_s::sint
i64 sint
Definition: json_format.h:57
vat_json_object_add
static_always_inline vat_json_node_t * vat_json_object_add(vat_json_node_t *json, const char *name)
Definition: json_format.h:138
vat_json_free
void vat_json_free(vat_json_node_t *node)
Definition: json_format.c:257
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
name
string name[64]
Definition: fib.api:25
clib.h
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vat_json_object_add_list
static_always_inline vat_json_node_t * vat_json_object_add_list(vat_json_node_t *json, const char *name)
Definition: json_format.h:157
vat_json_pair_s
Definition: json_format.h:62
vat_json_node_s::ip4
struct in_addr ip4
Definition: json_format.h:54
VAT_JSON_REAL
@ VAT_JSON_REAL
Definition: json_format.h:34
VAT_JSON_OBJECT
@ VAT_JSON_OBJECT
Definition: json_format.h:31
vat_json_node_s::array
vat_json_node_t * array
Definition: json_format.h:52
number
const char *const const double number
Definition: cJSON.h:268
vat_json_set_string_copy
static_always_inline void vat_json_set_string_copy(vat_json_node_t *json, const u8 *str)
Definition: json_format.h:93
vat_json_array_add_int
static_always_inline void vat_json_array_add_int(vat_json_node_t *json, i64 number)
Definition: json_format.h:205
vat_json_node_s::ip6
struct in6_addr ip6
Definition: json_format.h:55
i64
signed long i64
Definition: types.h:78
vat_json_init_object
static_always_inline void vat_json_init_object(vat_json_node_t *json)
Definition: json_format.h:72
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
VAT_JSON_INT
@ VAT_JSON_INT
Definition: json_format.h:36
vat_json_set_ip6
static_always_inline void vat_json_set_ip6(vat_json_node_t *json, struct in6_addr ip6)
Definition: json_format.h:131
vat_json_object_add_ip6
static_always_inline void vat_json_object_add_ip6(vat_json_node_t *json, const char *name, struct in6_addr ip6)
Definition: json_format.h:198
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vat_json_node_s::string
u8 * string
Definition: json_format.h:53
static_always_inline
#define static_always_inline
Definition: clib.h:112
vat_json_print
void vat_json_print(FILE *ofp, vat_json_node_t *node)
Definition: json_format.c:245
uword
u64 uword
Definition: types.h:112
vat_json_set_real
static_always_inline void vat_json_set_real(vat_json_node_t *json, f64 real)
Definition: json_format.h:117
VAT_JSON_ARRAY
@ VAT_JSON_ARRAY
Definition: json_format.h:32
vat_json_object_get_element
static_always_inline vat_json_node_t * vat_json_object_get_element(vat_json_node_t *json, const char *name)
Definition: json_format.h:231
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
f64
double f64
Definition: types.h:142
vat_json_array_add
static_always_inline vat_json_node_t * vat_json_array_add(vat_json_node_t *json)
Definition: json_format.h:148
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
format.h
VAT_JSON_MAX
@ VAT_JSON_MAX
Definition: json_format.h:39
vat_json_object_add_bytes
static_always_inline void vat_json_object_add_bytes(vat_json_node_t *json, const char *name, u8 *array, uword size)
Definition: json_format.h:217
VAT_JSON_UINT
@ VAT_JSON_UINT
Definition: json_format.h:35
vat_json_object_add_uint
static_always_inline void vat_json_object_add_uint(vat_json_node_t *json, const char *name, u64 number)
Definition: json_format.h:172
VAT_JSON_NONE
@ VAT_JSON_NONE
Definition: json_format.h:30
VAT_JSON_IPV4
@ VAT_JSON_IPV4
Definition: json_format.h:37
vat_json_array_add_uint
static_always_inline void vat_json_array_add_uint(vat_json_node_t *json, u64 number)
Definition: json_format.h:211
size
u32 size
Definition: vhost_user.h:125
vat_json_object_add_int
static_always_inline void vat_json_object_add_int(vat_json_node_t *json, const char *name, i64 number)
Definition: json_format.h:179
vat_json_object_add_string_copy
static_always_inline void vat_json_object_add_string_copy(vat_json_node_t *json, const char *name, u8 *str)
Definition: json_format.h:165
vat_json_object_add_real
static_always_inline void vat_json_object_add_real(vat_json_node_t *json, const char *name, f64 real)
Definition: json_format.h:185
u64
unsigned long u64
Definition: types.h:89
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vat_json_node_s::pairs
vat_json_pair_t * pairs
Definition: json_format.h:51
VAT_JSON_IPV6
@ VAT_JSON_IPV6
Definition: json_format.h:38
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
vat_json_object_add_ip4
static_always_inline void vat_json_object_add_ip4(vat_json_node_t *json, const char *name, struct in_addr ip4)
Definition: json_format.h:191
vat_json_set_string
static_always_inline void vat_json_set_string(vat_json_node_t *json, u8 *str)
Definition: json_format.h:86
vat_json_set_int
static_always_inline void vat_json_set_int(vat_json_node_t *json, i64 num)
Definition: json_format.h:103
vat_json_pair_s::value
vat_json_node_t value
Definition: json_format.h:65
vat_json_set_uint
static_always_inline void vat_json_set_uint(vat_json_node_t *json, u64 num)
Definition: json_format.h:110
u8
unsigned char u8
Definition: types.h:56
vat_json_node_s::real
f64 real
Definition: json_format.h:58
vat_json_set_ip4
static_always_inline void vat_json_set_ip4(vat_json_node_t *json, struct in_addr ip4)
Definition: json_format.h:124
VAT_JSON_STRING
@ VAT_JSON_STRING
Definition: json_format.h:33
vat_json_node_s::type
vat_json_val_type_t type
Definition: json_format.h:48
vat_json_pair_s::name
const char * name
Definition: json_format.h:64
vat_json_init_array
static_always_inline void vat_json_init_array(vat_json_node_t *json)
Definition: json_format.h:79
vat_json_node_s::uint
u64 uint
Definition: json_format.h:56
vat_json_node_s
Definition: json_format.h:46