Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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> |
Tom Jones | 393bfa7 | 2024-01-30 09:47:05 +0000 | [diff] [blame] | 25 | #ifdef __FreeBSD__ |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
| 29 | #endif /* __FreeBSD__ */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | #include <netinet/ip.h> |
| 31 | |
| 32 | /* JSON value type */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 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 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 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 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 67 | struct vat_json_pair_s |
| 68 | { |
| 69 | const char *name; |
| 70 | vat_json_node_t value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 73 | void vat_json_print (FILE * ofp, vat_json_node_t * node); |
| 74 | void vat_json_free (vat_json_node_t * node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 76 | static_always_inline void |
| 77 | vat_json_init_object (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 79 | json->type = VAT_JSON_OBJECT; |
| 80 | json->pairs = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 83 | static_always_inline void |
| 84 | vat_json_init_array (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 86 | json->type = VAT_JSON_ARRAY; |
| 87 | json->array = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 90 | static_always_inline void |
| 91 | vat_json_set_string (vat_json_node_t * json, u8 * str) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 93 | json->type = VAT_JSON_STRING; |
| 94 | json->string = str; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 97 | static_always_inline void |
| 98 | vat_json_set_string_copy (vat_json_node_t * json, const u8 * str) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 100 | u8 *ns = NULL; |
Dave Barach | 032a5e3 | 2020-04-06 10:19:18 -0400 | [diff] [blame] | 101 | int len = strlen ((const char *) str); |
Andreas Schultz | 6355135 | 2020-04-07 13:40:36 +0200 | [diff] [blame] | 102 | vec_validate_init_c_string (ns, str, len); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 103 | vat_json_set_string (json, ns); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 106 | static_always_inline void |
| 107 | vat_json_set_int (vat_json_node_t * json, i64 num) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 109 | json->type = VAT_JSON_INT; |
| 110 | json->sint = num; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 113 | static_always_inline void |
| 114 | vat_json_set_uint (vat_json_node_t * json, u64 num) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 116 | json->type = VAT_JSON_UINT; |
| 117 | json->uint = num; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 120 | static_always_inline void |
| 121 | vat_json_set_real (vat_json_node_t * json, f64 real) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 123 | json->type = VAT_JSON_REAL; |
| 124 | json->real = real; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 127 | static_always_inline void |
| 128 | vat_json_set_ip4 (vat_json_node_t * json, struct in_addr ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 130 | json->type = VAT_JSON_IPV4; |
| 131 | json->ip4 = ip4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 134 | static_always_inline void |
| 135 | vat_json_set_ip6 (vat_json_node_t * json, struct in6_addr ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 137 | json->type = VAT_JSON_IPV6; |
| 138 | json->ip6 = ip6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 141 | static_always_inline vat_json_node_t * |
| 142 | vat_json_object_add (vat_json_node_t * json, const char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 144 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 151 | static_always_inline vat_json_node_t * |
| 152 | vat_json_array_add (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 154 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 160 | static_always_inline vat_json_node_t * |
| 161 | vat_json_object_add_list (vat_json_node_t * json, const char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 163 | vat_json_node_t *array_node = vat_json_object_add (json, name); |
| 164 | vat_json_init_array (array_node); |
| 165 | return array_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 168 | static_always_inline void |
| 169 | vat_json_object_add_string_copy (vat_json_node_t * json, |
| 170 | const char *name, u8 * str) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 172 | vat_json_set_string_copy (vat_json_object_add (json, name), str); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 175 | static_always_inline void |
| 176 | vat_json_object_add_uint (vat_json_node_t * json, |
| 177 | const char *name, u64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 179 | vat_json_set_uint (vat_json_object_add (json, name), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 182 | static_always_inline void |
| 183 | vat_json_object_add_int (vat_json_node_t * json, const char *name, i64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 185 | vat_json_set_int (vat_json_object_add (json, name), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 188 | static_always_inline void |
| 189 | vat_json_object_add_real (vat_json_node_t * json, const char *name, f64 real) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 191 | vat_json_set_real (vat_json_object_add (json, name), real); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 194 | static_always_inline void |
| 195 | vat_json_object_add_ip4 (vat_json_node_t * json, |
| 196 | const char *name, struct in_addr ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 198 | vat_json_set_ip4 (vat_json_object_add (json, name), ip4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 201 | static_always_inline void |
| 202 | vat_json_object_add_ip6 (vat_json_node_t * json, |
| 203 | const char *name, struct in6_addr ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 205 | vat_json_set_ip6 (vat_json_object_add (json, name), ip6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 208 | static_always_inline void |
| 209 | vat_json_array_add_int (vat_json_node_t * json, i64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 211 | vat_json_set_int (vat_json_array_add (json), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 214 | static_always_inline void |
| 215 | vat_json_array_add_uint (vat_json_node_t * json, u64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 217 | vat_json_set_uint (vat_json_array_add (json), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 220 | static_always_inline void |
| 221 | vat_json_object_add_bytes (vat_json_node_t * json, |
| 222 | const char *name, u8 * array, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 224 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 234 | static_always_inline vat_json_node_t * |
| 235 | vat_json_object_get_element (vat_json_node_t * json, const char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 237 | int i = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 239 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 247 | return NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | #endif /* __JSON_FORMAT_H__ */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * fd.io coding-style-patch-verification: ON |
| 254 | * |
| 255 | * Local Variables: |
| 256 | * eval: (c-set-style "gnu") |
| 257 | * End: |
| 258 | */ |