Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 2e19036 | 2016-03-25 14:33:26 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 34 | #include <stdlib.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 35 | #include <string.h> |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 36 | |
| 37 | #include <grpc/support/alloc.h> |
| 38 | #include <grpc/support/log.h> |
| 39 | |
| 40 | #include "src/core/json/json.h" |
| 41 | #include "src/core/json/json_reader.h" |
| 42 | #include "src/core/json/json_writer.h" |
| 43 | |
| 44 | /* The json reader will construct a bunch of grpc_json objects and |
| 45 | * link them all up together in a tree-like structure that will represent |
| 46 | * the json data in memory. |
| 47 | * |
| 48 | * It also uses its own input as a scratchpad to store all of the decoded, |
| 49 | * unescaped strings. So we need to keep track of all these pointers in |
| 50 | * that opaque structure the reader will carry for us. |
| 51 | * |
| 52 | * Note that this works because the act of parsing json always reduces its |
| 53 | * input size, and never expands it. |
| 54 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | typedef struct { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 56 | grpc_json *top; |
| 57 | grpc_json *current_container; |
| 58 | grpc_json *current_value; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 59 | uint8_t *input; |
| 60 | uint8_t *key; |
| 61 | uint8_t *string; |
| 62 | uint8_t *string_ptr; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 63 | size_t remaining_input; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 64 | } json_reader_userdata; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 65 | |
| 66 | /* This json writer will put everything in a big string. |
| 67 | * The point is that we allocate that string in chunks of 256 bytes. |
| 68 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 69 | typedef struct { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 70 | char *output; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 71 | size_t free_space; |
| 72 | size_t string_len; |
| 73 | size_t allocated; |
| 74 | } json_writer_userdata; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 75 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 76 | /* This function checks if there's enough space left in the output buffer, |
| 77 | * and will enlarge it if necessary. We're only allocating chunks of 256 |
| 78 | * bytes at a time (or multiples thereof). |
| 79 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | static void json_writer_output_check(void *userdata, size_t needed) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 81 | json_writer_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | if (state->free_space >= needed) return; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 83 | needed -= state->free_space; |
| 84 | /* Round up by 256 bytes. */ |
murgatroid99 | 482a44c | 2015-06-19 16:12:22 -0700 | [diff] [blame] | 85 | needed = (needed + 0xff) & ~0xffU; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | state->output = gpr_realloc(state->output, state->allocated + needed); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 87 | state->free_space += needed; |
| 88 | state->allocated += needed; |
| 89 | } |
| 90 | |
| 91 | /* These are needed by the writer's implementation. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | static void json_writer_output_char(void *userdata, char c) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 93 | json_writer_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | json_writer_output_check(userdata, 1); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 95 | state->output[state->string_len++] = c; |
| 96 | state->free_space--; |
| 97 | } |
| 98 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | static void json_writer_output_string_with_len(void *userdata, const char *str, |
| 100 | size_t len) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 101 | json_writer_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | json_writer_output_check(userdata, len); |
| 103 | memcpy(state->output + state->string_len, str, len); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 104 | state->string_len += len; |
| 105 | state->free_space -= len; |
| 106 | } |
| 107 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | static void json_writer_output_string(void *userdata, const char *str) { |
| 109 | size_t len = strlen(str); |
| 110 | json_writer_output_string_with_len(userdata, str, len); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* The reader asks us to clear our scratchpad. In our case, we'll simply mark |
| 114 | * the end of the current string, and advance our output pointer. |
| 115 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | static void json_reader_string_clear(void *userdata) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 117 | json_reader_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | if (state->string) { |
| 119 | GPR_ASSERT(state->string_ptr < state->input); |
| 120 | *state->string_ptr++ = 0; |
| 121 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 122 | state->string = state->string_ptr; |
| 123 | } |
| 124 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 125 | static void json_reader_string_add_char(void *userdata, uint32_t c) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 126 | json_reader_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | GPR_ASSERT(state->string_ptr < state->input); |
| 128 | GPR_ASSERT(c <= 0xff); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 129 | *state->string_ptr++ = (uint8_t)c; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 132 | /* We are converting a UTF-32 character into UTF-8 here, |
| 133 | * as described by RFC3629. |
| 134 | */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 135 | static void json_reader_string_add_utf32(void *userdata, uint32_t c) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | if (c <= 0x7f) { |
| 137 | json_reader_string_add_char(userdata, c); |
| 138 | } else if (c <= 0x7ff) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 139 | uint32_t b1 = 0xc0 | ((c >> 6) & 0x1f); |
| 140 | uint32_t b2 = 0x80 | (c & 0x3f); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | json_reader_string_add_char(userdata, b1); |
| 142 | json_reader_string_add_char(userdata, b2); |
| 143 | } else if (c <= 0xffff) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 144 | uint32_t b1 = 0xe0 | ((c >> 12) & 0x0f); |
| 145 | uint32_t b2 = 0x80 | ((c >> 6) & 0x3f); |
| 146 | uint32_t b3 = 0x80 | (c & 0x3f); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | json_reader_string_add_char(userdata, b1); |
| 148 | json_reader_string_add_char(userdata, b2); |
| 149 | json_reader_string_add_char(userdata, b3); |
| 150 | } else if (c <= 0x1fffff) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 151 | uint32_t b1 = 0xf0 | ((c >> 18) & 0x07); |
| 152 | uint32_t b2 = 0x80 | ((c >> 12) & 0x3f); |
| 153 | uint32_t b3 = 0x80 | ((c >> 6) & 0x3f); |
| 154 | uint32_t b4 = 0x80 | (c & 0x3f); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | json_reader_string_add_char(userdata, b1); |
| 156 | json_reader_string_add_char(userdata, b2); |
| 157 | json_reader_string_add_char(userdata, b3); |
| 158 | json_reader_string_add_char(userdata, b4); |
| 159 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* We consider that the input may be a zero-terminated string. So we |
| 163 | * can end up hitting eof before the end of the alleged string length. |
| 164 | */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 165 | static uint32_t json_reader_read_char(void *userdata) { |
| 166 | uint32_t r; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 167 | json_reader_userdata *state = userdata; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 168 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | if (state->remaining_input == 0) return GRPC_JSON_READ_CHAR_EOF; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 170 | |
| 171 | r = *state->input++; |
| 172 | state->remaining_input--; |
| 173 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | if (r == 0) { |
| 175 | state->remaining_input = 0; |
| 176 | return GRPC_JSON_READ_CHAR_EOF; |
| 177 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 178 | |
| 179 | return r; |
| 180 | } |
| 181 | |
| 182 | /* Helper function to create a new grpc_json object and link it into |
| 183 | * our tree-in-progress inside our opaque structure. |
| 184 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 185 | static grpc_json *json_create_and_link(void *userdata, grpc_json_type type) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 186 | json_reader_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | grpc_json *json = grpc_json_create(type); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 188 | |
| 189 | json->parent = state->current_container; |
| 190 | json->prev = state->current_value; |
| 191 | state->current_value = json; |
| 192 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | if (json->prev) { |
| 194 | json->prev->next = json; |
| 195 | } |
| 196 | if (json->parent) { |
| 197 | if (!json->parent->child) { |
| 198 | json->parent->child = json; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 199 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | if (json->parent->type == GRPC_JSON_OBJECT) { |
| 201 | json->key = (char *)state->key; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 202 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | } |
| 204 | if (!state->top) { |
| 205 | state->top = json; |
| 206 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 207 | |
| 208 | return json; |
| 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | static void json_reader_container_begins(void *userdata, grpc_json_type type) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 212 | json_reader_userdata *state = userdata; |
| 213 | grpc_json *container; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 214 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 215 | GPR_ASSERT(type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 216 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | container = json_create_and_link(userdata, type); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 218 | state->current_container = container; |
| 219 | state->current_value = NULL; |
| 220 | } |
| 221 | |
| 222 | /* It's important to remember that the reader is mostly stateless, so it |
| 223 | * isn't trying to remember what the container was prior the one that just |
| 224 | * ends. Since we're keeping track of these for our own purpose, we are |
| 225 | * able to return that information back, which is useful for it to validate |
| 226 | * the input json stream. |
| 227 | * |
| 228 | * Also note that if we're at the top of the tree, and the last container |
| 229 | * ends, we have to return GRPC_JSON_TOP_LEVEL. |
| 230 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | static grpc_json_type json_reader_container_ends(void *userdata) { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 232 | grpc_json_type container_type = GRPC_JSON_TOP_LEVEL; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 233 | json_reader_userdata *state = userdata; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 234 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | GPR_ASSERT(state->current_container); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 236 | |
| 237 | state->current_value = state->current_container; |
| 238 | state->current_container = state->current_container->parent; |
| 239 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | if (state->current_container) { |
| 241 | container_type = state->current_container->type; |
| 242 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 243 | |
| 244 | return container_type; |
| 245 | } |
| 246 | |
| 247 | /* The next 3 functions basically are the reader asking us to use our string |
| 248 | * scratchpad for one of these 3 purposes. |
| 249 | * |
| 250 | * Note that in the set_number case, we're not going to try interpreting it. |
| 251 | * We'll keep it as a string, and leave it to the caller to evaluate it. |
| 252 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | static void json_reader_set_key(void *userdata) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 254 | json_reader_userdata *state = userdata; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 255 | state->key = state->string; |
| 256 | } |
| 257 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 258 | static void json_reader_set_string(void *userdata) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 259 | json_reader_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | grpc_json *json = json_create_and_link(userdata, GRPC_JSON_STRING); |
| 261 | json->value = (char *)state->string; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | static int json_reader_set_number(void *userdata) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 265 | json_reader_userdata *state = userdata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | grpc_json *json = json_create_and_link(userdata, GRPC_JSON_NUMBER); |
| 267 | json->value = (char *)state->string; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | /* The object types true, false and null are self-sufficient, and don't need |
| 272 | * any more information beside their type. |
| 273 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | static void json_reader_set_true(void *userdata) { |
| 275 | json_create_and_link(userdata, GRPC_JSON_TRUE); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | static void json_reader_set_false(void *userdata) { |
| 279 | json_create_and_link(userdata, GRPC_JSON_FALSE); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | static void json_reader_set_null(void *userdata) { |
| 283 | json_create_and_link(userdata, GRPC_JSON_NULL); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 286 | static grpc_json_reader_vtable reader_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | json_reader_string_clear, json_reader_string_add_char, |
| 288 | json_reader_string_add_utf32, json_reader_read_char, |
| 289 | json_reader_container_begins, json_reader_container_ends, |
| 290 | json_reader_set_key, json_reader_set_string, |
| 291 | json_reader_set_number, json_reader_set_true, |
| 292 | json_reader_set_false, json_reader_set_null}; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 293 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 294 | /* And finally, let's define our public API. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 295 | grpc_json *grpc_json_parse_string_with_len(char *input, size_t size) { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 296 | grpc_json_reader reader; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 297 | json_reader_userdata state; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 298 | grpc_json *json = NULL; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 299 | grpc_json_reader_status status; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 300 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | if (!input) return NULL; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 302 | |
| 303 | state.top = state.current_container = state.current_value = NULL; |
| 304 | state.string = state.key = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 305 | state.string_ptr = state.input = (uint8_t *)input; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 306 | state.remaining_input = size; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | grpc_json_reader_init(&reader, &reader_vtable, &state); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 308 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 309 | status = grpc_json_reader_run(&reader); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 310 | json = state.top; |
| 311 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 312 | if ((status != GRPC_JSON_DONE) && json) { |
| 313 | grpc_json_destroy(json); |
| 314 | json = NULL; |
| 315 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 316 | |
| 317 | return json; |
| 318 | } |
| 319 | |
| 320 | #define UNBOUND_JSON_STRING_LENGTH 0x7fffffff |
| 321 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 322 | grpc_json *grpc_json_parse_string(char *input) { |
| 323 | return grpc_json_parse_string_with_len(input, UNBOUND_JSON_STRING_LENGTH); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 326 | static void json_dump_recursive(grpc_json_writer *writer, grpc_json *json, |
| 327 | int in_object) { |
| 328 | while (json) { |
| 329 | if (in_object) grpc_json_writer_object_key(writer, json->key); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 330 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | switch (json->type) { |
| 332 | case GRPC_JSON_OBJECT: |
| 333 | case GRPC_JSON_ARRAY: |
| 334 | grpc_json_writer_container_begins(writer, json->type); |
| 335 | if (json->child) |
| 336 | json_dump_recursive(writer, json->child, |
| 337 | json->type == GRPC_JSON_OBJECT); |
| 338 | grpc_json_writer_container_ends(writer, json->type); |
| 339 | break; |
| 340 | case GRPC_JSON_STRING: |
| 341 | grpc_json_writer_value_string(writer, json->value); |
| 342 | break; |
| 343 | case GRPC_JSON_NUMBER: |
| 344 | grpc_json_writer_value_raw(writer, json->value); |
| 345 | break; |
| 346 | case GRPC_JSON_TRUE: |
| 347 | grpc_json_writer_value_raw_with_len(writer, "true", 4); |
| 348 | break; |
| 349 | case GRPC_JSON_FALSE: |
| 350 | grpc_json_writer_value_raw_with_len(writer, "false", 5); |
| 351 | break; |
| 352 | case GRPC_JSON_NULL: |
| 353 | grpc_json_writer_value_raw_with_len(writer, "null", 4); |
| 354 | break; |
| 355 | default: |
Nicolas "Pixel" Noble | 11c320d | 2015-12-12 01:47:36 +0100 | [diff] [blame] | 356 | GPR_UNREACHABLE_CODE(abort()); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 357 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 358 | json = json->next; |
| 359 | } |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 362 | static grpc_json_writer_vtable writer_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 363 | json_writer_output_char, json_writer_output_string, |
| 364 | json_writer_output_string_with_len}; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 365 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 366 | char *grpc_json_dump_to_string(grpc_json *json, int indent) { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 367 | grpc_json_writer writer; |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 368 | json_writer_userdata state; |
| 369 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 370 | state.output = NULL; |
| 371 | state.free_space = state.string_len = state.allocated = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 372 | grpc_json_writer_init(&writer, indent, &writer_vtable, &state); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 373 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 374 | json_dump_recursive(&writer, json, 0); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 375 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 376 | json_writer_output_char(&state, 0); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 377 | |
| 378 | return state.output; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 379 | } |