OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 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 |
| 34 /* The idea of the writer is basically symmetrical of the reader. While the |
| 35 * reader emits various calls to your code, the writer takes basically the |
| 36 * same calls and emit json out of it. It doesn't try to make any check on |
| 37 * the order of the calls you do on it. Meaning you can theorically force |
| 38 * it to generate invalid json. |
| 39 * |
| 40 * Also, unlike the reader, the writer expects UTF-8 encoded input strings. |
| 41 * These strings will be UTF-8 validated, and any invalid character will |
| 42 * cut the conversion short, before any invalid UTF-8 sequence, thus forming |
| 43 * a valid UTF-8 string overall. |
| 44 */ |
| 45 |
| 46 #ifndef GRPC_INTERNAL_CORE_JSON_JSON_WRITER_H |
| 47 #define GRPC_INTERNAL_CORE_JSON_JSON_WRITER_H |
| 48 |
| 49 #include <stdlib.h> |
| 50 |
| 51 #include "src/core/json/json_common.h" |
| 52 |
| 53 typedef struct grpc_json_writer_vtable { |
| 54 /* Adds a character to the output stream. */ |
| 55 void (*output_char)(void *userdata, char); |
| 56 /* Adds a zero-terminated string to the output stream. */ |
| 57 void (*output_string)(void *userdata, const char *str); |
| 58 /* Adds a fixed-length string to the output stream. */ |
| 59 void (*output_string_with_len)(void *userdata, const char *str, size_t len); |
| 60 |
| 61 } grpc_json_writer_vtable; |
| 62 |
| 63 typedef struct grpc_json_writer { |
| 64 void *userdata; |
| 65 grpc_json_writer_vtable *vtable; |
| 66 int indent; |
| 67 int depth; |
| 68 int container_empty; |
| 69 int got_key; |
| 70 } grpc_json_writer; |
| 71 |
| 72 /* Call this to initialize your writer structure. The indent parameter is |
| 73 * specifying the number of spaces to use for indenting the output. If you |
| 74 * use indent=0, then the output will not have any newlines either, thus |
| 75 * emitting a condensed json output. |
| 76 */ |
| 77 void grpc_json_writer_init(grpc_json_writer *writer, int indent, |
| 78 grpc_json_writer_vtable *vtable, void *userdata); |
| 79 |
| 80 /* Signals the beginning of a container. */ |
| 81 void grpc_json_writer_container_begins(grpc_json_writer *writer, |
| 82 grpc_json_type type); |
| 83 /* Signals the end of a container. */ |
| 84 void grpc_json_writer_container_ends(grpc_json_writer *writer, |
| 85 grpc_json_type type); |
| 86 /* Writes down an object key for the next value. */ |
| 87 void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string); |
| 88 /* Sets a raw value. Useful for numbers. */ |
| 89 void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string); |
| 90 /* Sets a raw value with its length. Useful for values like true or false. */ |
| 91 void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer, |
| 92 const char *string, size_t len); |
| 93 /* Sets a string value. It'll be escaped, and utf-8 validated. */ |
| 94 void grpc_json_writer_value_string(grpc_json_writer *writer, |
| 95 const char *string); |
| 96 |
| 97 #endif /* GRPC_INTERNAL_CORE_JSON_JSON_WRITER_H */ |
OLD | NEW |