GRPC C++  1.26.0
json_writer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* The idea of the writer is basically symmetrical of the reader. While the
20  * reader emits various calls to your code, the writer takes basically the
21  * same calls and emit json out of it. It doesn't try to make any check on
22  * the order of the calls you do on it. Meaning you can theorically force
23  * it to generate invalid json.
24  *
25  * Also, unlike the reader, the writer expects UTF-8 encoded input strings.
26  * These strings will be UTF-8 validated, and any invalid character will
27  * cut the conversion short, before any invalid UTF-8 sequence, thus forming
28  * a valid UTF-8 string overall.
29  */
30 
31 #ifndef GRPC_CORE_LIB_JSON_JSON_WRITER_H
32 #define GRPC_CORE_LIB_JSON_JSON_WRITER_H
33 
35 
36 #include <stdlib.h>
37 
39 
40 typedef struct grpc_json_writer_vtable {
41  /* Adds a character to the output stream. */
42  void (*output_char)(void* userdata, char);
43  /* Adds a zero-terminated string to the output stream. */
44  void (*output_string)(void* userdata, const char* str);
45  /* Adds a fixed-length string to the output stream. */
46  void (*output_string_with_len)(void* userdata, const char* str, size_t len);
47 
49 
50 typedef struct grpc_json_writer {
51  void* userdata;
53  int indent;
54  int depth;
56  int got_key;
58 
59 /* Call this to initialize your writer structure. The indent parameter is
60  * specifying the number of spaces to use for indenting the output. If you
61  * use indent=0, then the output will not have any newlines either, thus
62  * emitting a condensed json output.
63  */
64 void grpc_json_writer_init(grpc_json_writer* writer, int indent,
65  grpc_json_writer_vtable* vtable, void* userdata);
66 
67 /* Signals the beginning of a container. */
69  grpc_json_type type);
70 /* Signals the end of a container. */
72  grpc_json_type type);
73 /* Writes down an object key for the next value. */
74 void grpc_json_writer_object_key(grpc_json_writer* writer, const char* string);
75 /* Sets a raw value. Useful for numbers. */
76 void grpc_json_writer_value_raw(grpc_json_writer* writer, const char* string);
77 /* Sets a raw value with its length. Useful for values like true or false. */
79  const char* string, size_t len);
80 /* Sets a string value. It'll be escaped, and utf-8 validated. */
82  const char* string);
83 
84 #endif /* GRPC_CORE_LIB_JSON_JSON_WRITER_H */
struct grpc_json_writer grpc_json_writer
int depth
Definition: json_writer.h:54
void grpc_json_writer_init(grpc_json_writer *writer, int indent, grpc_json_writer_vtable *vtable, void *userdata)
struct grpc_json_writer_vtable grpc_json_writer_vtable
int container_empty
Definition: json_writer.h:55
void grpc_json_writer_value_string(grpc_json_writer *writer, const char *string)
grpc_json_type
Definition: json_common.h:23
void grpc_json_writer_container_ends(grpc_json_writer *writer, grpc_json_type type)
void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer, const char *string, size_t len)
void(* output_string)(void *userdata, const char *str)
Definition: json_writer.h:44
int got_key
Definition: json_writer.h:56
grpc_json_writer_vtable * vtable
Definition: json_writer.h:52
int indent
Definition: json_writer.h:53
void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string)
void * userdata
Definition: json_writer.h:51
void(* output_char)(void *userdata, char)
Definition: json_writer.h:42
Definition: json_writer.h:40
void(* output_string_with_len)(void *userdata, const char *str, size_t len)
Definition: json_writer.h:46
void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string)
Definition: json_writer.h:50
void grpc_json_writer_container_begins(grpc_json_writer *writer, grpc_json_type type)