gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
json_reader.h
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);
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;
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 */
Definition: json_reader.h:83
Definition: json_reader.h:108