GRPC Core  9.0.0
json_reader.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 #ifndef GRPC_CORE_LIB_JSON_JSON_READER_H
20 #define GRPC_CORE_LIB_JSON_JSON_READER_H
21 
23 
25 
26 typedef enum {
56 
57 enum {
58  /* The first non-unicode value is 0x110000. But let's pick
59  * a value high enough to start our error codes from. These
60  * values are safe to return from the read_char function.
61  */
65 };
66 
67 struct grpc_json_reader;
68 
69 typedef struct grpc_json_reader_vtable {
70  /* Clears your internal string scratchpad. */
71  void (*string_clear)(void* userdata);
72  /* Adds a char to the string scratchpad. */
73  void (*string_add_char)(void* userdata, uint32_t c);
74  /* Adds a utf32 char to the string scratchpad. */
75  void (*string_add_utf32)(void* userdata, uint32_t c);
76  /* Reads a character from your input. May be utf-8, 16 or 32. */
77  uint32_t (*read_char)(void* userdata);
78  /* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */
79  void (*container_begins)(void* userdata, grpc_json_type type);
80  /* Ends the current container. Must return the type of its parent. */
81  grpc_json_type (*container_ends)(void* userdata);
82  /* Your internal string scratchpad is an object's key. */
83  void (*set_key)(void* userdata);
84  /* Your internal string scratchpad is a string value. */
85  void (*set_string)(void* userdata);
86  /* Your internal string scratchpad is a numerical value. Return 1 if valid. */
87  int (*set_number)(void* userdata);
88  /* Sets the values true, false or null. */
89  void (*set_true)(void* userdata);
90  void (*set_false)(void* userdata);
91  void (*set_null)(void* userdata);
93 
94 typedef struct grpc_json_reader {
95  /* That structure is fully private, and initialized by grpc_json_reader_init.
96  * The definition is public so you can put it on your stack.
97  */
98 
99  void* userdata;
101  int depth;
103  int in_array;
109 
110 /* The return type of the parser. */
111 typedef enum {
112  GRPC_JSON_DONE, /* The parser finished successfully. */
113  GRPC_JSON_EAGAIN, /* The parser yields to get more data. */
114  GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */
115  GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
116  GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
118 
119 /* Call this function to start parsing the input. It will return the following:
120  * . GRPC_JSON_DONE if the input got eof, and the parsing finished
121  * successfully.
122  * . GRPC_JSON_EAGAIN if the read_char function returned again. Call the
123  * parser again as needed. It is okay to call the parser in polling mode,
124  * although a bit dull.
125  * . GRPC_JSON_READ_ERROR if the read_char function returned an error. The
126  * state isn't broken however, and the function can be called again if the
127  * error has been corrected. But please use the EAGAIN feature instead for
128  * consistency.
129  * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid.
130  * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
131  * internal state.
132  */
134 
135 /* Call this function to initialize the reader structure. */
137  grpc_json_reader_vtable* vtable, void* userdata);
138 
139 /* You may call this from the read_char callback if you don't know where is the
140  * end of your input stream, and you'd like the json reader to hint you that it
141  * has completed reading its input, so you can return an EOF to it. Note that
142  * there might still be trailing whitespaces after that point.
143  */
145 
146 #endif /* GRPC_CORE_LIB_JSON_JSON_READER_H */
int in_object
Definition: json_reader.h:102
grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader)
Definition: json_reader.cc:94
void(* string_add_char)(void *userdata, uint32_t c)
Definition: json_reader.h:73
grpc_json_type(* container_ends)(void *userdata)
Definition: json_reader.h:81
Definition: json_reader.h:47
struct grpc_json_reader_vtable grpc_json_reader_vtable
void grpc_json_reader_init(grpc_json_reader *reader, grpc_json_reader_vtable *vtable, void *userdata)
Definition: json_reader.cc:79
Definition: json_reader.h:50
int container_just_begun
Definition: json_reader.h:105
uint16_t unicode_high_surrogate
Definition: json_reader.h:106
int(* set_number)(void *userdata)
Definition: json_reader.h:87
Definition: json_reader.h:69
void(* set_string)(void *userdata)
Definition: json_reader.h:85
void * userdata
Definition: json_reader.h:99
void(* set_key)(void *userdata)
Definition: json_reader.h:83
void(* string_clear)(void *userdata)
Definition: json_reader.h:71
Definition: json_reader.h:30
Definition: json_reader.h:42
Definition: json_reader.h:33
Definition: json_reader.h:34
void(* string_add_utf32)(void *userdata, uint32_t c)
Definition: json_reader.h:75
Definition: json_reader.h:112
Definition: json_reader.h:116
grpc_json_reader_status
Definition: json_reader.h:111
Definition: json_reader.h:62
Definition: json_reader.h:41
Definition: json_reader.h:115
Definition: json_reader.h:114
Definition: json_reader.h:113
grpc_json_type
Definition: json_common.h:23
Definition: json_reader.h:49
Definition: json_reader.h:27
Definition: json_reader.h:63
Definition: json_reader.h:39
uint32_t(* read_char)(void *userdata)
Definition: json_reader.h:77
Definition: json_reader.h:48
Definition: json_reader.h:36
int escaped_string_was_key
Definition: json_reader.h:104
int in_array
Definition: json_reader.h:103
void(* container_begins)(void *userdata, grpc_json_type type)
Definition: json_reader.h:79
void(* set_true)(void *userdata)
Definition: json_reader.h:89
void(* set_null)(void *userdata)
Definition: json_reader.h:91
Definition: json_reader.h:28
grpc_json_reader_state
Definition: json_reader.h:26
grpc_json_reader_vtable * vtable
Definition: json_reader.h:100
Definition: json_reader.h:38
Definition: json_reader.h:53
Definition: json_reader.h:37
Definition: json_reader.h:64
Definition: json_reader.h:29
Definition: json_reader.h:52
int depth
Definition: json_reader.h:101
Definition: json_reader.h:54
grpc_json_reader_state state
Definition: json_reader.h:107
int grpc_json_reader_is_complete(grpc_json_reader *reader)
Definition: json_reader.cc:88
Definition: json_reader.h:35
uint16_t unicode_char
Definition: json_reader.h:106
void(* set_false)(void *userdata)
Definition: json_reader.h:90
Definition: json_reader.h:40
Definition: json_reader.h:51
Definition: json_reader.h:31
Definition: json_reader.h:46
struct grpc_json_reader grpc_json_reader
Definition: json_reader.h:94
Definition: json_reader.h:45
Definition: json_reader.h:32
Definition: json_reader.h:43
Definition: json_reader.h:44