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> |
| 25 | #include <netinet/ip.h> |
| 26 | |
| 27 | /* JSON value type */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 28 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | } vat_json_val_type_t; |
| 41 | |
| 42 | typedef struct vat_json_node_s vat_json_node_t; |
| 43 | typedef struct vat_json_pair_s vat_json_pair_t; |
| 44 | |
| 45 | /* JSON object structure */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 46 | struct 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 62 | struct vat_json_pair_s |
| 63 | { |
| 64 | const char *name; |
| 65 | vat_json_node_t value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 68 | void vat_json_print (FILE * ofp, vat_json_node_t * node); |
| 69 | void vat_json_free (vat_json_node_t * node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 71 | static_always_inline void |
| 72 | vat_json_init_object (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 74 | json->type = VAT_JSON_OBJECT; |
| 75 | json->pairs = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 78 | static_always_inline void |
| 79 | vat_json_init_array (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 81 | json->type = VAT_JSON_ARRAY; |
| 82 | json->array = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 85 | static_always_inline void |
| 86 | vat_json_set_string (vat_json_node_t * json, u8 * str) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 88 | json->type = VAT_JSON_STRING; |
| 89 | json->string = str; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 92 | static_always_inline void |
| 93 | 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] | 94 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 95 | u8 *ns = NULL; |
Dave Barach | 032a5e3 | 2020-04-06 10:19:18 -0400 | [diff] [blame] | 96 | int len = strlen ((const char *) str); |
Andreas Schultz | 6355135 | 2020-04-07 13:40:36 +0200 | [diff] [blame] | 97 | vec_validate_init_c_string (ns, str, len); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 98 | vat_json_set_string (json, ns); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 101 | static_always_inline void |
| 102 | vat_json_set_int (vat_json_node_t * json, i64 num) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 104 | json->type = VAT_JSON_INT; |
| 105 | json->sint = num; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 108 | static_always_inline void |
| 109 | vat_json_set_uint (vat_json_node_t * json, u64 num) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 111 | json->type = VAT_JSON_UINT; |
| 112 | json->uint = num; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 115 | static_always_inline void |
| 116 | vat_json_set_real (vat_json_node_t * json, f64 real) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 118 | json->type = VAT_JSON_REAL; |
| 119 | json->real = real; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 122 | static_always_inline void |
| 123 | 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] | 124 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 125 | json->type = VAT_JSON_IPV4; |
| 126 | json->ip4 = ip4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 129 | static_always_inline void |
| 130 | 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] | 131 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 132 | json->type = VAT_JSON_IPV6; |
| 133 | json->ip6 = ip6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 136 | static_always_inline vat_json_node_t * |
| 137 | vat_json_object_add (vat_json_node_t * json, const char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 139 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 146 | static_always_inline vat_json_node_t * |
| 147 | vat_json_array_add (vat_json_node_t * json) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 149 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 155 | static_always_inline vat_json_node_t * |
| 156 | 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] | 157 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 158 | vat_json_node_t *array_node = vat_json_object_add (json, name); |
| 159 | vat_json_init_array (array_node); |
| 160 | return array_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 163 | static_always_inline void |
| 164 | vat_json_object_add_string_copy (vat_json_node_t * json, |
| 165 | const char *name, u8 * str) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 167 | vat_json_set_string_copy (vat_json_object_add (json, name), str); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 170 | static_always_inline void |
| 171 | vat_json_object_add_uint (vat_json_node_t * json, |
| 172 | const char *name, u64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 174 | vat_json_set_uint (vat_json_object_add (json, name), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 177 | static_always_inline void |
| 178 | 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] | 179 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 180 | vat_json_set_int (vat_json_object_add (json, name), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 183 | static_always_inline void |
| 184 | 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] | 185 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 186 | vat_json_set_real (vat_json_object_add (json, name), real); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 189 | static_always_inline void |
| 190 | vat_json_object_add_ip4 (vat_json_node_t * json, |
| 191 | const char *name, struct in_addr ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 193 | vat_json_set_ip4 (vat_json_object_add (json, name), ip4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 196 | static_always_inline void |
| 197 | vat_json_object_add_ip6 (vat_json_node_t * json, |
| 198 | const char *name, struct in6_addr ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 200 | vat_json_set_ip6 (vat_json_object_add (json, name), ip6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 203 | static_always_inline void |
| 204 | vat_json_array_add_int (vat_json_node_t * json, i64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 206 | vat_json_set_int (vat_json_array_add (json), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 209 | static_always_inline void |
| 210 | vat_json_array_add_uint (vat_json_node_t * json, u64 number) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 212 | vat_json_set_uint (vat_json_array_add (json), number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 215 | static_always_inline void |
| 216 | vat_json_object_add_bytes (vat_json_node_t * json, |
| 217 | const char *name, u8 * array, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 219 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 229 | static_always_inline vat_json_node_t * |
| 230 | 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] | 231 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 232 | int i = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 234 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 242 | return NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | #endif /* __JSON_FORMAT_H__ */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * fd.io coding-style-patch-verification: ON |
| 249 | * |
| 250 | * Local Variables: |
| 251 | * eval: (c-set-style "gnu") |
| 252 | * End: |
| 253 | */ |