GRPC Core  0.11.0.0
 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 
39 
40 typedef enum {
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  */
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);
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;
115  int depth;
117  int in_array;
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. */
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  */
148 
149 /* Call this function to initialize the reader structure. */
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  */
159 
160 #endif /* GRPC_INTERNAL_CORE_JSON_JSON_READER_H */
int in_object
Definition: json_reader.h:116
grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader)
Definition: json_reader.c:108
grpc_json_type(* container_ends)(void *userdata)
Definition: json_reader.h:95
void(* string_add_char)(void *userdata, gpr_uint32 c)
Definition: json_reader.h:87
Definition: json_reader.h:61
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.c:93
Definition: json_reader.h:64
int container_just_begun
Definition: json_reader.h:119
Definition: json_reader.h:83
void * userdata
Definition: json_reader.h:113
void(* string_add_utf32)(void *userdata, gpr_uint32 c)
Definition: json_reader.h:89
Definition: json_reader.h:44
void(* string_clear)(void *userdata)
Definition: json_reader.h:85
Definition: json_reader.h:56
uint32_t gpr_uint32
Definition: port_platform.h:312
Definition: json_reader.h:47
Definition: json_reader.h:77
Definition: json_reader.h:48
void(* set_string)(void *userdata)
Definition: json_reader.h:99
void(* set_null)(void *userdata)
Definition: json_reader.h:105
gpr_uint16 unicode_high_surrogate
Definition: json_reader.h:120
Definition: json_reader.h:126
Definition: json_reader.h:130
grpc_json_reader_status
Definition: json_reader.h:125
Definition: json_reader.h:55
Definition: json_reader.h:76
Definition: json_reader.h:129
Definition: json_reader.h:128
Definition: json_reader.h:127
grpc_json_type
Definition: json_common.h:38
Definition: json_reader.h:63
Definition: json_reader.h:41
gpr_uint16 unicode_char
Definition: json_reader.h:120
Definition: json_reader.h:53
Definition: json_reader.h:62
Definition: json_reader.h:50
int escaped_string_was_key
Definition: json_reader.h:118
int in_array
Definition: json_reader.h:117
uint16_t gpr_uint16
Definition: port_platform.h:311
Definition: json_reader.h:42
grpc_json_reader_state
Definition: json_reader.h:40
grpc_json_reader_vtable * vtable
Definition: json_reader.h:114
Definition: json_reader.h:52
Definition: json_reader.h:67
Definition: json_reader.h:51
void(* set_true)(void *userdata)
Definition: json_reader.h:103
int(* set_number)(void *userdata)
Definition: json_reader.h:101
Definition: json_reader.h:43
Definition: json_reader.h:66
int depth
Definition: json_reader.h:115
Definition: json_reader.h:68
grpc_json_reader_state state
Definition: json_reader.h:121
gpr_uint32(* read_char)(void *userdata)
Definition: json_reader.h:91
int grpc_json_reader_is_complete(grpc_json_reader *reader)
Definition: json_reader.c:102
Definition: json_reader.h:49
void(* container_begins)(void *userdata, grpc_json_type type)
Definition: json_reader.h:93
Definition: json_reader.h:54
Definition: json_reader.h:65
Definition: json_reader.h:45
Definition: json_reader.h:60
struct grpc_json_reader grpc_json_reader
Definition: json_reader.h:108
void(* set_key)(void *userdata)
Definition: json_reader.h:97
Definition: json_reader.h:78
void(* set_false)(void *userdata)
Definition: json_reader.h:104
Definition: json_reader.h:59
Definition: json_reader.h:46
Definition: json_reader.h:57
Definition: json_reader.h:58