GRPC Core 0.10.0.0

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
json_writer.h
Go to the documentation of this file.
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, grpc_json_type type);
82 /* Signals the end of a container. */
83 void grpc_json_writer_container_ends(grpc_json_writer* writer, grpc_json_type type);
84 /* Writes down an object key for the next value. */
85 void grpc_json_writer_object_key(grpc_json_writer* writer, const char* string);
86 /* Sets a raw value. Useful for numbers. */
87 void grpc_json_writer_value_raw(grpc_json_writer* writer, const char* string);
88 /* Sets a raw value with its length. Useful for values like true or false. */
89 void grpc_json_writer_value_raw_with_len(grpc_json_writer* writer, const char* string, size_t len);
90 /* Sets a string value. It'll be escaped, and utf-8 validated. */
91 void grpc_json_writer_value_string(grpc_json_writer* writer, const char* string);
92
93 #endif /* GRPC_INTERNAL_CORE_JSON_JSON_WRITER_H */
grpc_json_writer
struct grpc_json_writer grpc_json_writer
grpc_json_writer::depth
int depth
Definition: json_writer.h:67
json_common.h
grpc_json_writer_init
void grpc_json_writer_init(grpc_json_writer *writer, int indent, grpc_json_writer_vtable *vtable, void *userdata)
Definition: json_writer.c:52
grpc_json_writer_vtable
struct grpc_json_writer_vtable grpc_json_writer_vtable
grpc_json_writer::container_empty
int container_empty
Definition: json_writer.h:68
grpc_json_writer_value_string
void grpc_json_writer_value_string(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:248
grpc_json_writer_vtable::output_string_with_len
void(* output_string_with_len)(void *userdata, const char *str, size_t len)
Definition: json_writer.h:59
grpc_json_writer_vtable::output_string
void(* output_string)(void *userdata, const char *str)
Definition: json_writer.h:57
grpc_json_type
grpc_json_type
Definition: json_common.h:38
grpc_json_writer_container_ends
void grpc_json_writer_container_ends(grpc_json_writer *writer, grpc_json_type type)
Definition: json_writer.c:216
grpc_json_writer_value_raw_with_len
void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer, const char *string, size_t len)
Definition: json_writer.c:241
grpc_json_writer::got_key
int got_key
Definition: json_writer.h:69
grpc_json_writer::vtable
grpc_json_writer_vtable * vtable
Definition: json_writer.h:65
grpc_json_writer::indent
int indent
Definition: json_writer.h:66
grpc_json_writer_vtable::output_char
void(* output_char)(void *userdata, char)
Definition: json_writer.h:55
grpc_json_writer_object_key
void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:226
grpc_json_writer::userdata
void * userdata
Definition: json_writer.h:64
grpc_json_writer_vtable
Definition: json_writer.h:53
grpc_json_writer_value_raw
void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:234
grpc_json_writer
Definition: json_writer.h:63
grpc_json_writer_container_begins
void grpc_json_writer_container_begins(grpc_json_writer *writer, grpc_json_type type)
Definition: json_writer.c:207