blob: a49d6fef685e013c98b180dde1b096b321706063 [file] [log] [blame]
Nicolas Noblee04455a2015-01-26 17:01:29 -08001/*
2 *
David Garcia Quintas3598d442016-03-15 14:53:05 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Noblee04455a2015-01-26 17:01:29 -08004 * 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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_JSON_JSON_READER_H
35#define GRPC_CORE_LIB_JSON_JSON_READER_H
Nicolas Noblee04455a2015-01-26 17:01:29 -080036
37#include <grpc/support/port_platform.h>
38#include "src/core/json/json_common.h"
39
Craig Tillera82950e2015-09-22 12:33:20 -070040typedef enum {
Nicolas Noblee04455a2015-01-26 17:01:29 -080041 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
Craig Tillera82950e2015-09-22 12:33:20 -070071enum {
Nicolas Noblee04455a2015-01-26 17:01:29 -080072 /* 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
Nicolas Noble8c2be9b2015-01-27 14:21:18 -080081struct grpc_json_reader;
Nicolas Noblee04455a2015-01-26 17:01:29 -080082
Craig Tillera82950e2015-09-22 12:33:20 -070083typedef struct grpc_json_reader_vtable {
Nicolas Noblee04455a2015-01-26 17:01:29 -080084 /* Clears your internal string scratchpad. */
Craig Tillera82950e2015-09-22 12:33:20 -070085 void (*string_clear)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -080086 /* Adds a char to the string scratchpad. */
Craig Tiller7536af02015-12-22 13:49:30 -080087 void (*string_add_char)(void *userdata, uint32_t c);
Nicolas Noblee04455a2015-01-26 17:01:29 -080088 /* Adds a utf32 char to the string scratchpad. */
Craig Tiller7536af02015-12-22 13:49:30 -080089 void (*string_add_utf32)(void *userdata, uint32_t c);
Nicolas Noblee04455a2015-01-26 17:01:29 -080090 /* Reads a character from your input. May be utf-8, 16 or 32. */
Craig Tiller7536af02015-12-22 13:49:30 -080091 uint32_t (*read_char)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -080092 /* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */
Craig Tillera82950e2015-09-22 12:33:20 -070093 void (*container_begins)(void *userdata, grpc_json_type type);
Nicolas Noblee04455a2015-01-26 17:01:29 -080094 /* Ends the current container. Must return the type of its parent. */
Craig Tillera82950e2015-09-22 12:33:20 -070095 grpc_json_type (*container_ends)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -080096 /* Your internal string scratchpad is an object's key. */
Craig Tillera82950e2015-09-22 12:33:20 -070097 void (*set_key)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -080098 /* Your internal string scratchpad is a string value. */
Craig Tillera82950e2015-09-22 12:33:20 -070099 void (*set_string)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -0800100 /* Your internal string scratchpad is a numerical value. Return 1 if valid. */
Craig Tillera82950e2015-09-22 12:33:20 -0700101 int (*set_number)(void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -0800102 /* Sets the values true, false or null. */
Craig Tillera82950e2015-09-22 12:33:20 -0700103 void (*set_true)(void *userdata);
104 void (*set_false)(void *userdata);
105 void (*set_null)(void *userdata);
Nicolas Noble8c2be9b2015-01-27 14:21:18 -0800106} grpc_json_reader_vtable;
Nicolas Noblee04455a2015-01-26 17:01:29 -0800107
Craig Tillera82950e2015-09-22 12:33:20 -0700108typedef struct grpc_json_reader {
Nicolas Noble8c2be9b2015-01-27 14:21:18 -0800109 /* 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
Craig Tiller45724b32015-09-22 10:42:19 -0700113 void *userdata;
114 grpc_json_reader_vtable *vtable;
Nicolas Noblee04455a2015-01-26 17:01:29 -0800115 int depth;
116 int in_object;
117 int in_array;
118 int escaped_string_was_key;
119 int container_just_begun;
Craig Tiller7536af02015-12-22 13:49:30 -0800120 uint16_t unicode_char, unicode_high_surrogate;
Nicolas Noblee04455a2015-01-26 17:01:29 -0800121 grpc_json_reader_state state;
122} grpc_json_reader;
123
124/* The return type of the parser. */
Craig Tillera82950e2015-09-22 12:33:20 -0700125typedef 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. */
Nicolas Noble8c2be9b2015-01-27 14:21:18 -0800131} grpc_json_reader_status;
Nicolas Noblee04455a2015-01-26 17:01:29 -0800132
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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700147grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader);
Nicolas Noblee04455a2015-01-26 17:01:29 -0800148
149/* Call this function to initialize the reader structure. */
Craig Tillera82950e2015-09-22 12:33:20 -0700150void grpc_json_reader_init(grpc_json_reader *reader,
151 grpc_json_reader_vtable *vtable, void *userdata);
Nicolas Noblee04455a2015-01-26 17:01:29 -0800152
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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700158int grpc_json_reader_is_complete(grpc_json_reader *reader);
Nicolas Noblee04455a2015-01-26 17:01:29 -0800159
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700160#endif /* GRPC_CORE_LIB_JSON_JSON_READER_H */