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 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/json/json_common.h" 25 26 typedef enum { 27 GRPC_JSON_STATE_OBJECT_KEY_BEGIN, 28 GRPC_JSON_STATE_OBJECT_KEY_STRING, 29 GRPC_JSON_STATE_OBJECT_KEY_END, 30 GRPC_JSON_STATE_VALUE_BEGIN, 31 GRPC_JSON_STATE_VALUE_STRING, 32 GRPC_JSON_STATE_STRING_ESCAPE, 33 GRPC_JSON_STATE_STRING_ESCAPE_U1, 34 GRPC_JSON_STATE_STRING_ESCAPE_U2, 35 GRPC_JSON_STATE_STRING_ESCAPE_U3, 36 GRPC_JSON_STATE_STRING_ESCAPE_U4, 37 GRPC_JSON_STATE_VALUE_NUMBER, 38 GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL, 39 GRPC_JSON_STATE_VALUE_NUMBER_ZERO, 40 GRPC_JSON_STATE_VALUE_NUMBER_DOT, 41 GRPC_JSON_STATE_VALUE_NUMBER_E, 42 GRPC_JSON_STATE_VALUE_NUMBER_EPM, 43 GRPC_JSON_STATE_VALUE_TRUE_R, 44 GRPC_JSON_STATE_VALUE_TRUE_U, 45 GRPC_JSON_STATE_VALUE_TRUE_E, 46 GRPC_JSON_STATE_VALUE_FALSE_A, 47 GRPC_JSON_STATE_VALUE_FALSE_L, 48 GRPC_JSON_STATE_VALUE_FALSE_S, 49 GRPC_JSON_STATE_VALUE_FALSE_E, 50 GRPC_JSON_STATE_VALUE_NULL_U, 51 GRPC_JSON_STATE_VALUE_NULL_L1, 52 GRPC_JSON_STATE_VALUE_NULL_L2, 53 GRPC_JSON_STATE_VALUE_END, 54 GRPC_JSON_STATE_END 55 } grpc_json_reader_state; 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 */ 62 GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0, 63 GRPC_JSON_READ_CHAR_EAGAIN, 64 GRPC_JSON_READ_CHAR_ERROR 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); 92 } grpc_json_reader_vtable; 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; 100 grpc_json_reader_vtable* vtable; 101 int depth; 102 int in_object; 103 int in_array; 104 int escaped_string_was_key; 105 int container_just_begun; 106 uint16_t unicode_char, unicode_high_surrogate; 107 grpc_json_reader_state state; 108 } grpc_json_reader; 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. */ 117 } grpc_json_reader_status; 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 */ 133 grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader); 134 135 /* Call this function to initialize the reader structure. */ 136 void grpc_json_reader_init(grpc_json_reader* reader, 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 */ 144 int grpc_json_reader_is_complete(grpc_json_reader* reader); 145 146 #endif /* GRPC_CORE_LIB_JSON_JSON_READER_H */ 147