Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_JSON_JSON_H |
| 35 | #define GRPC_CORE_LIB_JSON_JSON_H |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 36 | |
| 37 | #include <stdlib.h> |
| 38 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 39 | #include "src/core/json/json_common.h" |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 40 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 41 | /* A tree-like structure to hold json values. The key and value pointers |
| 42 | * are not owned by it. |
| 43 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | typedef struct grpc_json { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 45 | struct grpc_json *next; |
| 46 | struct grpc_json *prev; |
| 47 | struct grpc_json *child; |
| 48 | struct grpc_json *parent; |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 49 | |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 50 | grpc_json_type type; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 51 | const char *key; |
| 52 | const char *value; |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 53 | } grpc_json; |
| 54 | |
| 55 | /* The next two functions are going to parse the input string, and |
Julien Boeuf | 61a0ef5 | 2015-07-08 00:49:14 +0200 | [diff] [blame] | 56 | * modify it in the process, in order to use its space to store |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 57 | * all of the keys and values for the returned object tree. |
| 58 | * |
| 59 | * They assume UTF-8 input stream, and will output UTF-8 encoded |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 60 | * strings in the tree. The input stream's UTF-8 isn't validated, |
| 61 | * as in, what you input is what you get as an output. |
| 62 | * |
Julien Boeuf | 3e29de1 | 2015-06-18 23:29:13 +0200 | [diff] [blame] | 63 | * All the keys and values in the grpc_json objects will be strings |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 64 | * pointing at your input buffer. |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 65 | * |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 66 | * Delete the allocated tree afterward using grpc_json_destroy(). |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 67 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | grpc_json *grpc_json_parse_string_with_len(char *input, size_t size); |
| 69 | grpc_json *grpc_json_parse_string(char *input); |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 70 | |
| 71 | /* This function will create a new string using gpr_realloc, and will |
| 72 | * deserialize the grpc_json tree into it. It'll be zero-terminated, |
| 73 | * but will be allocated in chunks of 256 bytes. |
| 74 | * |
| 75 | * The indent parameter controls the way the output is formatted. |
| 76 | * If indent is 0, then newlines will be suppressed as well, and the |
| 77 | * output will be condensed at its maximum. |
| 78 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | char *grpc_json_dump_to_string(grpc_json *json, int indent); |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 80 | |
| 81 | /* Use these to create or delete a grpc_json object. |
| 82 | * Deletion is recursive. We will not attempt to free any of the strings |
| 83 | * in any of the objects of that tree. |
| 84 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | grpc_json *grpc_json_create(grpc_json_type type); |
| 86 | void grpc_json_destroy(grpc_json *json); |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 87 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 88 | #endif /* GRPC_CORE_LIB_JSON_JSON_H */ |