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 */
grpc_json_type
Definition: json_common.h:23
grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader)
Definition: json_reader.cc:94
struct grpc_json_reader grpc_json_reader
struct grpc_json_reader_vtable grpc_json_reader_vtable
grpc_json_reader_state
Definition: json_reader.h:26
@ GRPC_JSON_STATE_VALUE_NUMBER
Definition: json_reader.h:37
@ GRPC_JSON_STATE_VALUE_NUMBER_EPM
Definition: json_reader.h:42
@ GRPC_JSON_STATE_VALUE_NULL_L2
Definition: json_reader.h:52
@ GRPC_JSON_STATE_STRING_ESCAPE_U4
Definition: json_reader.h:36
@ GRPC_JSON_STATE_OBJECT_KEY_END
Definition: json_reader.h:29
@ GRPC_JSON_STATE_VALUE_STRING
Definition: json_reader.h:31
@ GRPC_JSON_STATE_VALUE_NUMBER_ZERO
Definition: json_reader.h:39
@ GRPC_JSON_STATE_VALUE_FALSE_L
Definition: json_reader.h:47
@ GRPC_JSON_STATE_VALUE_TRUE_E
Definition: json_reader.h:45
@ GRPC_JSON_STATE_VALUE_BEGIN
Definition: json_reader.h:30
@ GRPC_JSON_STATE_STRING_ESCAPE_U1
Definition: json_reader.h:33
@ GRPC_JSON_STATE_VALUE_TRUE_U
Definition: json_reader.h:44
@ GRPC_JSON_STATE_VALUE_TRUE_R
Definition: json_reader.h:43
@ GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL
Definition: json_reader.h:38
@ GRPC_JSON_STATE_VALUE_NULL_L1
Definition: json_reader.h:51
@ GRPC_JSON_STATE_VALUE_NULL_U
Definition: json_reader.h:50
@ GRPC_JSON_STATE_OBJECT_KEY_STRING
Definition: json_reader.h:28
@ GRPC_JSON_STATE_VALUE_END
Definition: json_reader.h:53
@ GRPC_JSON_STATE_VALUE_NUMBER_E
Definition: json_reader.h:41
@ GRPC_JSON_STATE_END
Definition: json_reader.h:54
@ GRPC_JSON_STATE_VALUE_FALSE_S
Definition: json_reader.h:48
@ GRPC_JSON_STATE_VALUE_FALSE_E
Definition: json_reader.h:49
@ GRPC_JSON_STATE_STRING_ESCAPE
Definition: json_reader.h:32
@ GRPC_JSON_STATE_VALUE_NUMBER_DOT
Definition: json_reader.h:40
@ GRPC_JSON_STATE_STRING_ESCAPE_U3
Definition: json_reader.h:35
@ GRPC_JSON_STATE_OBJECT_KEY_BEGIN
Definition: json_reader.h:27
@ GRPC_JSON_STATE_VALUE_FALSE_A
Definition: json_reader.h:46
@ GRPC_JSON_STATE_STRING_ESCAPE_U2
Definition: json_reader.h:34
grpc_json_reader_status
Definition: json_reader.h:111
@ GRPC_JSON_EAGAIN
Definition: json_reader.h:113
@ GRPC_JSON_DONE
Definition: json_reader.h:112
@ GRPC_JSON_PARSE_ERROR
Definition: json_reader.h:115
@ GRPC_JSON_INTERNAL_ERROR
Definition: json_reader.h:116
@ GRPC_JSON_READ_ERROR
Definition: json_reader.h:114
int grpc_json_reader_is_complete(grpc_json_reader *reader)
Definition: json_reader.cc:88
void grpc_json_reader_init(grpc_json_reader *reader, grpc_json_reader_vtable *vtable, void *userdata)
Definition: json_reader.cc:79
@ GRPC_JSON_READ_CHAR_ERROR
Definition: json_reader.h:64
@ GRPC_JSON_READ_CHAR_EAGAIN
Definition: json_reader.h:63
@ GRPC_JSON_READ_CHAR_EOF
Definition: json_reader.h:62
Definition: json_reader.h:69
void(* set_string)(void *userdata)
Definition: json_reader.h:85
void(* string_add_char)(void *userdata, uint32_t c)
Definition: json_reader.h:73
int(* set_number)(void *userdata)
Definition: json_reader.h:87
void(* set_true)(void *userdata)
Definition: json_reader.h:89
uint32_t(* read_char)(void *userdata)
Definition: json_reader.h:77
void(* set_key)(void *userdata)
Definition: json_reader.h:83
grpc_json_type(* container_ends)(void *userdata)
Definition: json_reader.h:81
void(* string_add_utf32)(void *userdata, uint32_t c)
Definition: json_reader.h:75
void(* string_clear)(void *userdata)
Definition: json_reader.h:71
void(* set_null)(void *userdata)
Definition: json_reader.h:91
void(* container_begins)(void *userdata, grpc_json_type type)
Definition: json_reader.h:79
void(* set_false)(void *userdata)
Definition: json_reader.h:90
Definition: json_reader.h:94
int in_array
Definition: json_reader.h:103
int escaped_string_was_key
Definition: json_reader.h:104
grpc_json_reader_state state
Definition: json_reader.h:107
uint16_t unicode_char
Definition: json_reader.h:106
uint16_t unicode_high_surrogate
Definition: json_reader.h:106
int container_just_begun
Definition: json_reader.h:105
void * userdata
Definition: json_reader.h:99
int in_object
Definition: json_reader.h:102
grpc_json_reader_vtable * vtable
Definition: json_reader.h:100
int depth
Definition: json_reader.h:101