GRPC Core  0.10.0.0
 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 
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 
62 
63 typedef struct grpc_json_writer {
64  void* userdata;
66  int indent;
67  int depth;
69  int got_key;
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. */
82 /* Signals the end of a container. */
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 */
struct grpc_json_writer grpc_json_writer
int depth
Definition: json_writer.h:67
void grpc_json_writer_init(grpc_json_writer *writer, int indent, grpc_json_writer_vtable *vtable, void *userdata)
Definition: json_writer.c:52
struct grpc_json_writer_vtable grpc_json_writer_vtable
int container_empty
Definition: json_writer.h:68
void grpc_json_writer_value_string(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:248
void(* output_string_with_len)(void *userdata, const char *str, size_t len)
Definition: json_writer.h:59
void(* output_string)(void *userdata, const char *str)
Definition: json_writer.h:57
grpc_json_type
Definition: json_common.h:38
void grpc_json_writer_container_ends(grpc_json_writer *writer, grpc_json_type type)
Definition: json_writer.c:216
void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer, const char *string, size_t len)
Definition: json_writer.c:241
int got_key
Definition: json_writer.h:69
grpc_json_writer_vtable * vtable
Definition: json_writer.h:65
int indent
Definition: json_writer.h:66
void(* output_char)(void *userdata, char)
Definition: json_writer.h:55
void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:226
void * userdata
Definition: json_writer.h:64
Definition: json_writer.h:53
void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string)
Definition: json_writer.c:234
Definition: json_writer.h:63
void grpc_json_writer_container_begins(grpc_json_writer *writer, grpc_json_type type)
Definition: json_writer.c:207