• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 #include <grpc/support/port_platform.h>
35 
36 #include <stdlib.h>
37 
38 #include "src/core/lib/json/json_common.h"
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 
48 } grpc_json_writer_vtable;
49 
50 typedef struct grpc_json_writer {
51   void* userdata;
52   grpc_json_writer_vtable* vtable;
53   int indent;
54   int depth;
55   int container_empty;
56   int got_key;
57 } grpc_json_writer;
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. */
68 void grpc_json_writer_container_begins(grpc_json_writer* writer,
69                                        grpc_json_type type);
70 /* Signals the end of a container. */
71 void grpc_json_writer_container_ends(grpc_json_writer* writer,
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. */
78 void grpc_json_writer_value_raw_with_len(grpc_json_writer* writer,
79                                          const char* string, size_t len);
80 /* Sets a string value. It'll be escaped, and utf-8 validated. */
81 void grpc_json_writer_value_string(grpc_json_writer* writer,
82                                    const char* string);
83 
84 #endif /* GRPC_CORE_LIB_JSON_JSON_WRITER_H */
85