hs-test: temp skip some tests
[vpp.git] / src / vpp / api / json_format.h
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 #ifdef __FreeBSD__
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #endif /* __FreeBSD__ */
30 #include <netinet/ip.h>
31
32 /* JSON value type */
33 typedef 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
45 } vat_json_val_type_t;
46
47 typedef struct vat_json_node_s vat_json_node_t;
48 typedef struct vat_json_pair_s vat_json_pair_t;
49
50 /* JSON object structure */
51 struct 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   };
65 };
66
67 struct vat_json_pair_s
68 {
69   const char *name;
70   vat_json_node_t value;
71 };
72
73 void vat_json_print (FILE * ofp, vat_json_node_t * node);
74 void vat_json_free (vat_json_node_t * node);
75
76 static_always_inline void
77 vat_json_init_object (vat_json_node_t * json)
78 {
79   json->type = VAT_JSON_OBJECT;
80   json->pairs = NULL;
81 }
82
83 static_always_inline void
84 vat_json_init_array (vat_json_node_t * json)
85 {
86   json->type = VAT_JSON_ARRAY;
87   json->array = NULL;
88 }
89
90 static_always_inline void
91 vat_json_set_string (vat_json_node_t * json, u8 * str)
92 {
93   json->type = VAT_JSON_STRING;
94   json->string = str;
95 }
96
97 static_always_inline void
98 vat_json_set_string_copy (vat_json_node_t * json, const u8 * str)
99 {
100   u8 *ns = NULL;
101   vec_validate (ns, strlen ((const char *) str));
102   strncpy ((char *) ns, (const char *) str, vec_len (ns));
103   vec_add1 (ns, '\0');
104   vat_json_set_string (json, ns);
105 }
106
107 static_always_inline void
108 vat_json_set_int (vat_json_node_t * json, i64 num)
109 {
110   json->type = VAT_JSON_INT;
111   json->sint = num;
112 }
113
114 static_always_inline void
115 vat_json_set_uint (vat_json_node_t * json, u64 num)
116 {
117   json->type = VAT_JSON_UINT;
118   json->uint = num;
119 }
120
121 static_always_inline void
122 vat_json_set_real (vat_json_node_t * json, f64 real)
123 {
124   json->type = VAT_JSON_REAL;
125   json->real = real;
126 }
127
128 static_always_inline void
129 vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4)
130 {
131   json->type = VAT_JSON_IPV4;
132   json->ip4 = ip4;
133 }
134
135 static_always_inline void
136 vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6)
137 {
138   json->type = VAT_JSON_IPV6;
139   json->ip6 = ip6;
140 }
141
142 static_always_inline vat_json_node_t *
143 vat_json_object_add (vat_json_node_t * json, const char *name)
144 {
145   ASSERT (VAT_JSON_OBJECT == json->type);
146   uword pos = vec_len (json->pairs);
147   vec_validate (json->pairs, pos);
148   json->pairs[pos].name = name;
149   return &json->pairs[pos].value;
150 }
151
152 static_always_inline vat_json_node_t *
153 vat_json_array_add (vat_json_node_t * json)
154 {
155   ASSERT (VAT_JSON_ARRAY == json->type);
156   uword pos = vec_len (json->array);
157   vec_validate (json->array, pos);
158   return &json->array[pos];
159 }
160
161 static_always_inline vat_json_node_t *
162 vat_json_object_add_list (vat_json_node_t * json, const char *name)
163 {
164   vat_json_node_t *array_node = vat_json_object_add (json, name);
165   vat_json_init_array (array_node);
166   return array_node;
167 }
168
169 static_always_inline void
170 vat_json_object_add_string_copy (vat_json_node_t * json,
171                                  const char *name, u8 * str)
172 {
173   vat_json_set_string_copy (vat_json_object_add (json, name), str);
174 }
175
176 static_always_inline void
177 vat_json_object_add_uint (vat_json_node_t * json,
178                           const char *name, u64 number)
179 {
180   vat_json_set_uint (vat_json_object_add (json, name), number);
181 }
182
183 static_always_inline void
184 vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number)
185 {
186   vat_json_set_int (vat_json_object_add (json, name), number);
187 }
188
189 static_always_inline void
190 vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real)
191 {
192   vat_json_set_real (vat_json_object_add (json, name), real);
193 }
194
195 static_always_inline void
196 vat_json_object_add_ip4 (vat_json_node_t * json,
197                          const char *name, struct in_addr ip4)
198 {
199   vat_json_set_ip4 (vat_json_object_add (json, name), ip4);
200 }
201
202 static_always_inline void
203 vat_json_object_add_ip6 (vat_json_node_t * json,
204                          const char *name, struct in6_addr ip6)
205 {
206   vat_json_set_ip6 (vat_json_object_add (json, name), ip6);
207 }
208
209 static_always_inline void
210 vat_json_array_add_int (vat_json_node_t * json, i64 number)
211 {
212   vat_json_set_int (vat_json_array_add (json), number);
213 }
214
215 static_always_inline void
216 vat_json_array_add_uint (vat_json_node_t * json, u64 number)
217 {
218   vat_json_set_uint (vat_json_array_add (json), number);
219 }
220
221 static_always_inline void
222 vat_json_object_add_bytes (vat_json_node_t * json,
223                            const char *name, u8 * array, uword size)
224 {
225   ASSERT (VAT_JSON_OBJECT == json->type);
226   vat_json_node_t *json_array = vat_json_object_add (json, name);
227   vat_json_init_array (json_array);
228   int i;
229   for (i = 0; i < size; i++)
230     {
231       vat_json_array_add_uint (json_array, array[i]);
232     }
233 }
234
235 static_always_inline vat_json_node_t *
236 vat_json_object_get_element (vat_json_node_t * json, const char *name)
237 {
238   int i = 0;
239
240   ASSERT (VAT_JSON_OBJECT == json->type);
241   for (i = 0; i < vec_len (json->pairs); i++)
242     {
243       if (0 == strcmp (json->pairs[i].name, name))
244         {
245           return &json->pairs[i].value;
246         }
247     }
248   return NULL;
249 }
250
251 #endif /* __JSON_FORMAT_H__ */
252
253 /*
254  * fd.io coding-style-patch-verification: ON
255  *
256  * Local Variables:
257  * eval: (c-set-style "gnu")
258  * End:
259  */