GRPC Core 0.10.0.0

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
json_reader.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 #ifndef GRPC_INTERNAL_CORE_JSON_JSON_READER_H
35 #define GRPC_INTERNAL_CORE_JSON_JSON_READER_H
36
37 #include <grpc/support/port_platform.h>
38 #include "src/core/json/json_common.h"
39
40 typedef enum {
41 GRPC_JSON_STATE_OBJECT_KEY_BEGIN,
42 GRPC_JSON_STATE_OBJECT_KEY_STRING,
43 GRPC_JSON_STATE_OBJECT_KEY_END,
44 GRPC_JSON_STATE_VALUE_BEGIN,
45 GRPC_JSON_STATE_VALUE_STRING,
46 GRPC_JSON_STATE_STRING_ESCAPE,
47 GRPC_JSON_STATE_STRING_ESCAPE_U1,
48 GRPC_JSON_STATE_STRING_ESCAPE_U2,
49 GRPC_JSON_STATE_STRING_ESCAPE_U3,
50 GRPC_JSON_STATE_STRING_ESCAPE_U4,
51 GRPC_JSON_STATE_VALUE_NUMBER,
52 GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL,
53 GRPC_JSON_STATE_VALUE_NUMBER_ZERO,
54 GRPC_JSON_STATE_VALUE_NUMBER_DOT,
55 GRPC_JSON_STATE_VALUE_NUMBER_E,
56 GRPC_JSON_STATE_VALUE_NUMBER_EPM,
57 GRPC_JSON_STATE_VALUE_TRUE_R,
58 GRPC_JSON_STATE_VALUE_TRUE_U,
59 GRPC_JSON_STATE_VALUE_TRUE_E,
60 GRPC_JSON_STATE_VALUE_FALSE_A,
61 GRPC_JSON_STATE_VALUE_FALSE_L,
62 GRPC_JSON_STATE_VALUE_FALSE_S,
63 GRPC_JSON_STATE_VALUE_FALSE_E,
64 GRPC_JSON_STATE_VALUE_NULL_U,
65 GRPC_JSON_STATE_VALUE_NULL_L1,
66 GRPC_JSON_STATE_VALUE_NULL_L2,
67 GRPC_JSON_STATE_VALUE_END,
68 GRPC_JSON_STATE_END
69 } grpc_json_reader_state;
70
71 enum {
72 /* The first non-unicode value is 0x110000. But let's pick
73 * a value high enough to start our error codes from. These
74 * values are safe to return from the read_char function.
75 */
76 GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0,
77 GRPC_JSON_READ_CHAR_EAGAIN,
78 GRPC_JSON_READ_CHAR_ERROR
79 };
80
81 struct grpc_json_reader;
82
83 typedef struct grpc_json_reader_vtable {
84 /* Clears your internal string scratchpad. */
85 void (*string_clear)(void* userdata);
86 /* Adds a char to the string scratchpad. */
87 void (*string_add_char)(void* userdata, gpr_uint32 c);
88 /* Adds a utf32 char to the string scratchpad. */
89 void (*string_add_utf32)(void* userdata, gpr_uint32 c);
90 /* Reads a character from your input. May be utf-8, 16 or 32. */
91 gpr_uint32 (*read_char)(void* userdata);
92 /* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */
93 void (*container_begins)(void* userdata, grpc_json_type type);
94 /* Ends the current container. Must return the type of its parent. */
95 grpc_json_type (*container_ends)(void* userdata);
96 /* Your internal string scratchpad is an object's key. */
97 void (*set_key)(void* userdata);
98 /* Your internal string scratchpad is a string value. */
99 void (*set_string)(void* userdata);
100 /* Your internal string scratchpad is a numerical value. Return 1 if valid. */
101 int (*set_number)(void* userdata);
102 /* Sets the values true, false or null. */
103 void (*set_true)(void* userdata);
104 void (*set_false)(void* userdata);
105 void (*set_null)(void* userdata);
106 } grpc_json_reader_vtable;
107
108 typedef struct grpc_json_reader {
109 /* That structure is fully private, and initialized by grpc_json_reader_init.
110 * The definition is public so you can put it on your stack.
111 */
112
113 void* userdata;
114 grpc_json_reader_vtable* vtable;
115 int depth;
116 int in_object;
117 int in_array;
118 int escaped_string_was_key;
119 int container_just_begun;
120 gpr_uint16 unicode_char, unicode_high_surrogate;
121 grpc_json_reader_state state;
122 } grpc_json_reader;
123
124 /* The return type of the parser. */
125 typedef enum {
126 GRPC_JSON_DONE, /* The parser finished successfully. */
127 GRPC_JSON_EAGAIN, /* The parser yields to get more data. */
128 GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */
129 GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
130 GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
131 } grpc_json_reader_status;
132
133 /* Call this function to start parsing the input. It will return the following:
134 * . GRPC_JSON_DONE if the input got eof, and the parsing finished
135 * successfully.
136 * . GRPC_JSON_EAGAIN if the read_char function returned again. Call the
137 * parser again as needed. It is okay to call the parser in polling mode,
138 * although a bit dull.
139 * . GRPC_JSON_READ_ERROR if the read_char function returned an error. The
140 * state isn't broken however, and the function can be called again if the
141 * error has been corrected. But please use the EAGAIN feature instead for
142 * consistency.
143 * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid.
144 * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
145 * internal state.
146 */
147 grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader);
148
149 /* Call this function to initialize the reader structure. */
150 void grpc_json_reader_init(grpc_json_reader* reader,
151 grpc_json_reader_vtable* vtable, void* userdata);
152
153 /* You may call this from the read_char callback if you don't know where is the
154 * end of your input stream, and you'd like the json reader to hint you that it
155 * has completed reading its input, so you can return an EOF to it. Note that
156 * there might still be trailing whitespaces after that point.
157 */
158 int grpc_json_reader_is_complete(grpc_json_reader* reader);
159
160 #endif /* GRPC_INTERNAL_CORE_JSON_JSON_READER_H */
grpc_json_reader::in_object
int in_object
Definition: json_reader.h:116
grpc_json_reader_run
grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader)
Definition: json_reader.c:108
grpc_json_reader_vtable::container_ends
grpc_json_type(* container_ends)(void *userdata)
Definition: json_reader.h:95
port_platform.h
grpc_json_reader_vtable::string_add_char
void(* string_add_char)(void *userdata, gpr_uint32 c)
Definition: json_reader.h:87
GRPC_JSON_STATE_VALUE_FALSE_L
Definition: json_reader.h:61
grpc_json_reader_vtable
struct grpc_json_reader_vtable grpc_json_reader_vtable
grpc_json_reader_init
void grpc_json_reader_init(grpc_json_reader *reader, grpc_json_reader_vtable *vtable, void *userdata)
Definition: json_reader.c:94
GRPC_JSON_STATE_VALUE_NULL_U
Definition: json_reader.h:64
grpc_json_reader::container_just_begun
int container_just_begun
Definition: json_reader.h:119
json_common.h
grpc_json_reader_vtable
Definition: json_reader.h:83
grpc_json_reader::userdata
void * userdata
Definition: json_reader.h:113
grpc_json_reader_vtable::string_add_utf32
void(* string_add_utf32)(void *userdata, gpr_uint32 c)
Definition: json_reader.h:89
GRPC_JSON_STATE_VALUE_BEGIN
Definition: json_reader.h:44
grpc_json_reader_vtable::string_clear
void(* string_clear)(void *userdata)
Definition: json_reader.h:85
GRPC_JSON_STATE_VALUE_NUMBER_EPM
Definition: json_reader.h:56
gpr_uint32
uint32_t gpr_uint32
Definition: port_platform.h:309
GRPC_JSON_STATE_STRING_ESCAPE_U1
Definition: json_reader.h:47
GRPC_JSON_STATE_STRING_ESCAPE_U2
Definition: json_reader.h:48
grpc_json_reader_vtable::set_string
void(* set_string)(void *userdata)
Definition: json_reader.h:99
grpc_json_reader_vtable::set_null
void(* set_null)(void *userdata)
Definition: json_reader.h:105
grpc_json_reader::unicode_high_surrogate
gpr_uint16 unicode_high_surrogate
Definition: json_reader.h:120
GRPC_JSON_DONE
Definition: json_reader.h:126
GRPC_JSON_INTERNAL_ERROR
Definition: json_reader.h:130
grpc_json_reader_status
grpc_json_reader_status
Definition: json_reader.h:125
GRPC_JSON_READ_CHAR_EAGAIN
Definition: json_reader.h:77
GRPC_JSON_STATE_VALUE_NUMBER_E
Definition: json_reader.h:55
GRPC_JSON_PARSE_ERROR
Definition: json_reader.h:129
GRPC_JSON_READ_ERROR
Definition: json_reader.h:128
GRPC_JSON_EAGAIN
Definition: json_reader.h:127
grpc_json_type
grpc_json_type
Definition: json_common.h:38
GRPC_JSON_STATE_VALUE_FALSE_E
Definition: json_reader.h:63
GRPC_JSON_STATE_OBJECT_KEY_BEGIN
Definition: json_reader.h:41
grpc_json_reader::unicode_char
gpr_uint16 unicode_char
Definition: json_reader.h:120
GRPC_JSON_STATE_VALUE_NUMBER_ZERO
Definition: json_reader.h:53
GRPC_JSON_STATE_VALUE_FALSE_S
Definition: json_reader.h:62
GRPC_JSON_STATE_STRING_ESCAPE_U4
Definition: json_reader.h:50
grpc_json_reader::escaped_string_was_key
int escaped_string_was_key
Definition: json_reader.h:118
grpc_json_reader::in_array
int in_array
Definition: json_reader.h:117
gpr_uint16
uint16_t gpr_uint16
Definition: port_platform.h:308
GRPC_JSON_STATE_OBJECT_KEY_STRING
Definition: json_reader.h:42
grpc_json_reader_state
grpc_json_reader_state
Definition: json_reader.h:40
grpc_json_reader::vtable
grpc_json_reader_vtable * vtable
Definition: json_reader.h:114
GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL
Definition: json_reader.h:52
GRPC_JSON_STATE_VALUE_END
Definition: json_reader.h:67
GRPC_JSON_STATE_VALUE_NUMBER
Definition: json_reader.h:51
GRPC_JSON_READ_CHAR_EOF
Definition: json_reader.h:76
grpc_json_reader_vtable::set_true
void(* set_true)(void *userdata)
Definition: json_reader.h:103
grpc_json_reader_vtable::set_number
int(* set_number)(void *userdata)
Definition: json_reader.h:101
GRPC_JSON_STATE_OBJECT_KEY_END
Definition: json_reader.h:43
GRPC_JSON_STATE_VALUE_NULL_L2
Definition: json_reader.h:66
grpc_json_reader::depth
int depth
Definition: json_reader.h:115
GRPC_JSON_STATE_END
Definition: json_reader.h:68
grpc_json_reader::state
grpc_json_reader_state state
Definition: json_reader.h:121
grpc_json_reader_vtable::read_char
gpr_uint32(* read_char)(void *userdata)
Definition: json_reader.h:91
grpc_json_reader_is_complete
int grpc_json_reader_is_complete(grpc_json_reader *reader)
Definition: json_reader.c:103
GRPC_JSON_STATE_STRING_ESCAPE_U3
Definition: json_reader.h:49
grpc_json_reader_vtable::container_begins
void(* container_begins)(void *userdata, grpc_json_type type)
Definition: json_reader.h:93
GRPC_JSON_STATE_VALUE_NUMBER_DOT
Definition: json_reader.h:54
GRPC_JSON_STATE_VALUE_NULL_L1
Definition: json_reader.h:65
GRPC_JSON_STATE_VALUE_STRING
Definition: json_reader.h:45
GRPC_JSON_STATE_VALUE_FALSE_A
Definition: json_reader.h:60
grpc_json_reader
struct grpc_json_reader grpc_json_reader
grpc_json_reader
Definition: json_reader.h:108
grpc_json_reader_vtable::set_key
void(* set_key)(void *userdata)
Definition: json_reader.h:97
GRPC_JSON_READ_CHAR_ERROR
Definition: json_reader.h:78
grpc_json_reader_vtable::set_false
void(* set_false)(void *userdata)
Definition: json_reader.h:104
GRPC_JSON_STATE_VALUE_TRUE_E
Definition: json_reader.h:59
GRPC_JSON_STATE_STRING_ESCAPE
Definition: json_reader.h:46
GRPC_JSON_STATE_VALUE_TRUE_R
Definition: json_reader.h:57
GRPC_JSON_STATE_VALUE_TRUE_U
Definition: json_reader.h:58