Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 1 | #include "aos/json_to_flatbuffer.h" |
| 2 | |
| 3 | #include <cstddef> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 4 | #include <string_view> |
| 5 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 6 | #include "aos/flatbuffer_utils.h" |
Austin Schuh | d7e252d | 2019-10-06 13:51:02 -0700 | [diff] [blame] | 7 | #include "aos/json_tokenizer.h" |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 8 | #include "flatbuffers/flatbuffers.h" |
| 9 | #include "flatbuffers/minireflect.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 10 | #include "glog/logging.h" |
Brian Silverman | 5c71041 | 2021-02-09 19:09:32 -0800 | [diff] [blame] | 11 | #include "stdio.h" |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 12 | |
| 13 | // TODO(austin): Can we just do an Offset<void> ? It doesn't matter, so maybe |
| 14 | // just say that. |
| 15 | // |
| 16 | // TODO(austin): I've yet to see how to create an ET_UTYPE, so I don't know what |
| 17 | // one is and how to test it. So everything rejects it. |
| 18 | |
| 19 | namespace aos { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // Class to hold one of the 3 json types for an array. |
| 23 | struct Element { |
| 24 | // The type. |
| 25 | enum class ElementType { INT, DOUBLE, OFFSET }; |
| 26 | |
| 27 | // Constructs an Element holding an integer. |
| 28 | Element(int64_t new_int_element) |
| 29 | : int_element(new_int_element), type(ElementType::INT) {} |
| 30 | // Constructs an Element holding an double. |
| 31 | Element(double new_double_element) |
| 32 | : double_element(new_double_element), type(ElementType::DOUBLE) {} |
| 33 | // Constructs an Element holding an Offset. |
| 34 | Element(flatbuffers::Offset<flatbuffers::String> new_offset_element) |
| 35 | : offset_element(new_offset_element), type(ElementType::OFFSET) {} |
| 36 | |
| 37 | // Union for the various datatypes. |
| 38 | union { |
| 39 | int64_t int_element; |
| 40 | double double_element; |
| 41 | flatbuffers::Offset<flatbuffers::String> offset_element; |
| 42 | }; |
| 43 | |
| 44 | // And an enum signaling which one is in use. |
| 45 | ElementType type; |
| 46 | }; |
| 47 | |
| 48 | // Structure to represent a field element. |
| 49 | struct FieldElement { |
| 50 | FieldElement(int new_field_index, int64_t int_element) |
| 51 | : element(int_element), field_index(new_field_index) {} |
| 52 | FieldElement(int new_field_index, double double_element) |
| 53 | : element(double_element), field_index(new_field_index) {} |
| 54 | FieldElement(int new_field_index, |
| 55 | flatbuffers::Offset<flatbuffers::String> offset_element) |
| 56 | : element(offset_element), field_index(new_field_index) {} |
| 57 | |
| 58 | // Data to write. |
| 59 | Element element; |
| 60 | // Field index. The type table which this index is for is stored outside this |
| 61 | // object. |
| 62 | int field_index; |
| 63 | }; |
| 64 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 65 | // Adds a single element. This assumes that vectors have been dealt with |
| 66 | // already. Returns true on success. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 67 | bool AddSingleElement(FlatbufferType type, const FieldElement &field_element, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 68 | ::std::vector<bool> *fields_in_use, |
| 69 | flatbuffers::FlatBufferBuilder *fbb); |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 70 | bool AddSingleElement(FlatbufferType type, int field_index, int64_t int_value, |
| 71 | flatbuffers::FlatBufferBuilder *fbb); |
| 72 | bool AddSingleElement(FlatbufferType type, int field_index, double double_value, |
| 73 | flatbuffers::FlatBufferBuilder *fbb); |
| 74 | bool AddSingleElement(FlatbufferType type, int field_index, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 75 | flatbuffers::Offset<flatbuffers::String> offset_element, |
| 76 | flatbuffers::FlatBufferBuilder *fbb); |
| 77 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 78 | // Writes an array of FieldElement (with the definition in "type") to the |
| 79 | // builder. Returns the offset of the resulting table. |
| 80 | flatbuffers::uoffset_t WriteTable(FlatbufferType type, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 81 | const ::std::vector<FieldElement> &elements, |
| 82 | flatbuffers::FlatBufferBuilder *fbb) { |
| 83 | // End of a nested struct! Add it. |
| 84 | const flatbuffers::uoffset_t start = fbb->StartTable(); |
| 85 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 86 | ::std::vector<bool> fields_in_use(type.NumberFields(), false); |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 87 | |
| 88 | for (const FieldElement &field_element : elements) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 89 | AddSingleElement(type, field_element, &fields_in_use, fbb); |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return fbb->EndTable(start); |
| 93 | } |
| 94 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 95 | // Class to parse JSON into a flatbuffer. |
| 96 | // |
| 97 | // The basic strategy is that we need to do everything backwards. So we need to |
| 98 | // build up what we need to do fully in memory, then do it. |
| 99 | // |
| 100 | // The driver for this is that strings need to be fully created before the |
| 101 | // tables that use them. Same for sub messages. But, we only know we have them |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 102 | // all when the structure ends. So, store each sub message in a |
| 103 | // FieldElement and put them in the table at the end when we finish up |
| 104 | // each message. Same goes for vectors. |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 105 | class JsonParser { |
| 106 | public: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 107 | JsonParser(flatbuffers::FlatBufferBuilder *fbb) : fbb_(fbb) {} |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 108 | ~JsonParser() {} |
| 109 | |
| 110 | // Parses the json into a flatbuffer. Returns either an empty vector on |
| 111 | // error, or a vector with the flatbuffer data in it. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 112 | flatbuffers::Offset<flatbuffers::Table> Parse(const std::string_view data, |
| 113 | FlatbufferType type) { |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 114 | flatbuffers::uoffset_t end = 0; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 115 | bool result = DoParse(type, data, &end); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 116 | |
| 117 | if (result) { |
| 118 | // On success, finish the table and build the vector. |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 119 | return flatbuffers::Offset<flatbuffers::Table>(end); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 120 | } else { |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 121 | return flatbuffers::Offset<flatbuffers::Table>(0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | // Setters and getters for in_vector (at the current level of the stack) |
| 127 | bool in_vector() const { return stack_.back().in_vector; } |
| 128 | void set_in_vector(bool in_vector) { stack_.back().in_vector = in_vector; } |
| 129 | |
| 130 | // Parses the flatbuffer. This is a second method so we can do easier |
| 131 | // cleanup at the top level. Returns true on success. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 132 | bool DoParse(FlatbufferType type, const std::string_view data, |
| 133 | flatbuffers::uoffset_t *table_end); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 134 | |
| 135 | // Adds *_value for the provided field. If we are in a vector, queues the |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | // data up in vector_elements. Returns true on success. |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 137 | bool AddElement(int field_index, int64_t int_value); |
| 138 | bool AddElement(int field_index, double double_value); |
| 139 | bool AddElement(int field_index, const ::std::string &data); |
| 140 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 141 | // Finishes a vector for the provided field index. Returns true on success. |
| 142 | bool FinishVector(int field_index); |
| 143 | |
| 144 | // Pushes an element as part of a vector. Returns true on success. |
| 145 | bool PushElement(flatbuffers::ElementaryType elementary_type, |
| 146 | int64_t int_value); |
| 147 | bool PushElement(flatbuffers::ElementaryType elementary_type, |
| 148 | double double_value); |
| 149 | bool PushElement(flatbuffers::ElementaryType elementary_type, |
| 150 | flatbuffers::Offset<flatbuffers::String> offset_value); |
| 151 | |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 152 | flatbuffers::FlatBufferBuilder *fbb_; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 153 | |
| 154 | // This holds the state information that is needed as you recurse into |
| 155 | // nested structures. |
| 156 | struct FlatBufferContext { |
| 157 | // Type of the current type. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 158 | FlatbufferType type; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 159 | // If true, we are parsing a vector. |
| 160 | bool in_vector; |
| 161 | // The field index of the current field. |
| 162 | int field_index; |
| 163 | // Name of the current field. |
| 164 | ::std::string field_name; |
| 165 | |
| 166 | // Field elements that need to be inserted. |
| 167 | ::std::vector<FieldElement> elements; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | |
| 169 | // For scalar types (not strings, and not nested tables), the vector ends |
| 170 | // up being implemented as a start and end, and a block of data. So we |
| 171 | // can't just push offsets in as we go. We either need to reproduce the |
| 172 | // logic inside flatbuffers, or build up vectors of the data. Vectors will |
| 173 | // be a bit of extra stack space, but whatever. |
| 174 | // |
| 175 | // Strings and nested structures are vectors of offsets. |
| 176 | // into the vector. Once you get to the end, you build up a vector and |
| 177 | // push that into the field. |
| 178 | ::std::vector<Element> vector_elements; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 179 | }; |
| 180 | ::std::vector<FlatBufferContext> stack_; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 183 | bool JsonParser::DoParse(FlatbufferType type, const std::string_view data, |
Austin Schuh | d339a9b | 2019-10-05 21:33:32 -0700 | [diff] [blame] | 184 | flatbuffers::uoffset_t *table_end) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 185 | ::std::vector<FlatbufferType> stack; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 186 | |
| 187 | Tokenizer t(data); |
| 188 | |
| 189 | // Main loop. Run until we get an end. |
| 190 | while (true) { |
| 191 | Tokenizer::TokenType token = t.Next(); |
| 192 | |
| 193 | switch (token) { |
| 194 | case Tokenizer::TokenType::kEnd: |
| 195 | if (stack_.size() != 0) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 196 | fprintf(stderr, "Failed to unwind stack all the way\n"); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } else { |
| 199 | return true; |
| 200 | } |
| 201 | break; |
| 202 | case Tokenizer::TokenType::kError: |
| 203 | return false; |
| 204 | break; |
| 205 | |
| 206 | case Tokenizer::TokenType::kStartObject: // { |
| 207 | if (stack_.size() == 0) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 208 | stack_.push_back({type, false, -1, "", {}, {}}); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 209 | } else { |
| 210 | int field_index = stack_.back().field_index; |
| 211 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 212 | if (!stack_.back().type.FieldIsSequence(field_index)) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 213 | fprintf(stderr, "Field '%s' is not a sequence\n", |
| 214 | stack_.back().field_name.c_str()); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 218 | stack_.push_back({stack_.back().type.FieldType(field_index), |
| 219 | false, |
| 220 | -1, |
| 221 | "", |
| 222 | {}, |
| 223 | {}}); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | case Tokenizer::TokenType::kEndObject: // } |
| 227 | if (stack_.size() == 0) { |
| 228 | // Somehow we popped more than we pushed. Error. |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 229 | fprintf(stderr, "Empty stack\n"); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } else { |
| 232 | // End of a nested struct! Add it. |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 233 | const flatbuffers::uoffset_t end = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 234 | WriteTable(stack_.back().type, stack_.back().elements, fbb_); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 235 | |
| 236 | // We now want to talk about the parent structure. Pop the child. |
| 237 | stack_.pop_back(); |
| 238 | |
| 239 | if (stack_.size() == 0) { |
| 240 | // Instead of queueing it up in the stack, return it through the |
| 241 | // passed in variable. |
| 242 | *table_end = end; |
| 243 | } else { |
| 244 | // And now we can add it. |
| 245 | const int field_index = stack_.back().field_index; |
| 246 | |
| 247 | // Do the right thing if we are in a vector. |
| 248 | if (in_vector()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | stack_.back().vector_elements.emplace_back( |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 250 | flatbuffers::Offset<flatbuffers::String>(end)); |
| 251 | } else { |
| 252 | stack_.back().elements.emplace_back( |
| 253 | field_index, flatbuffers::Offset<flatbuffers::String>(end)); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | break; |
| 258 | |
| 259 | case Tokenizer::TokenType::kStartArray: // [ |
| 260 | if (stack_.size() == 0) { |
| 261 | // We don't support an array of structs at the root level. |
| 262 | return false; |
| 263 | } |
| 264 | // Sanity check that we aren't trying to make a vector of vectors. |
| 265 | if (in_vector()) { |
| 266 | return false; |
| 267 | } |
| 268 | set_in_vector(true); |
| 269 | |
| 270 | break; |
| 271 | case Tokenizer::TokenType::kEndArray: { // ] |
| 272 | if (!in_vector()) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | const int field_index = stack_.back().field_index; |
| 277 | |
| 278 | if (!FinishVector(field_index)) return false; |
| 279 | |
| 280 | set_in_vector(false); |
| 281 | } break; |
| 282 | |
| 283 | case Tokenizer::TokenType::kTrueValue: // true |
| 284 | case Tokenizer::TokenType::kFalseValue: // false |
| 285 | case Tokenizer::TokenType::kNumberValue: { |
| 286 | bool is_int = true; |
| 287 | double double_value; |
| 288 | long long int_value; |
| 289 | if (token == Tokenizer::TokenType::kTrueValue) { |
| 290 | int_value = 1; |
| 291 | } else if (token == Tokenizer::TokenType::kFalseValue) { |
| 292 | int_value = 0; |
| 293 | } else if (!t.FieldAsInt(&int_value)) { |
| 294 | if (t.FieldAsDouble(&double_value)) { |
| 295 | is_int = false; |
| 296 | } else { |
| 297 | fprintf(stderr, "Got a invalid number '%s'\n", |
| 298 | t.field_value().c_str()); |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | const int field_index = stack_.back().field_index; |
| 304 | |
| 305 | if (is_int) { |
| 306 | // No need to get too stressed about bool vs int. Convert them all. |
| 307 | int64_t val = int_value; |
| 308 | if (!AddElement(field_index, val)) return false; |
| 309 | } else { |
| 310 | if (!AddElement(field_index, double_value)) return false; |
| 311 | } |
| 312 | } break; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 313 | case Tokenizer::TokenType::kStringValue: // string value |
| 314 | { |
| 315 | const int field_index = stack_.back().field_index; |
| 316 | |
| 317 | if (!AddElement(field_index, t.field_value())) return false; |
| 318 | } break; |
| 319 | case Tokenizer::TokenType::kField: // field name |
| 320 | { |
| 321 | stack_.back().field_name = t.field_name(); |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 322 | stack_.back().field_index = |
| 323 | stack_.back().type.FieldIndex(stack_.back().field_name.c_str()); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 324 | |
| 325 | if (stack_.back().field_index == -1) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 326 | fprintf(stderr, "Invalid field name '%s'\n", |
| 327 | stack_.back().field_name.c_str()); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | } break; |
| 331 | } |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool JsonParser::AddElement(int field_index, int64_t int_value) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 337 | if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 338 | fprintf(stderr, "Type and json disagree on if we are in a vector or not\n"); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (in_vector()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 343 | stack_.back().vector_elements.emplace_back(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 344 | } else { |
| 345 | stack_.back().elements.emplace_back(field_index, int_value); |
| 346 | } |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool JsonParser::AddElement(int field_index, double double_value) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 351 | if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 352 | fprintf(stderr, "Type and json disagree on if we are in a vector or not\n"); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
| 356 | if (in_vector()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 357 | stack_.back().vector_elements.emplace_back(double_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 358 | } else { |
| 359 | stack_.back().elements.emplace_back(field_index, double_value); |
| 360 | } |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | bool JsonParser::AddElement(int field_index, const ::std::string &data) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 365 | if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 366 | fprintf(stderr, "Type and json disagree on if we are in a vector or not\n"); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 370 | const flatbuffers::ElementaryType elementary_type = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 371 | stack_.back().type.FieldElementaryType(field_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 372 | switch (elementary_type) { |
| 373 | case flatbuffers::ET_CHAR: |
| 374 | case flatbuffers::ET_UCHAR: |
| 375 | case flatbuffers::ET_SHORT: |
| 376 | case flatbuffers::ET_USHORT: |
| 377 | case flatbuffers::ET_INT: |
| 378 | case flatbuffers::ET_UINT: |
| 379 | case flatbuffers::ET_LONG: |
| 380 | case flatbuffers::ET_ULONG: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 381 | if (stack_.back().type.FieldIsEnum(field_index)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 382 | // We have an enum. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 383 | const FlatbufferType type = stack_.back().type; |
| 384 | const FlatbufferType enum_type = type.FieldType(field_index); |
| 385 | CHECK(enum_type.IsEnum()); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 386 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 387 | const std::optional<int64_t> int_value = enum_type.EnumValue(data); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 388 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 389 | if (!int_value) { |
| 390 | const std::string_view name = type.FieldName(field_index); |
| 391 | fprintf(stderr, "Enum value '%s' not found for field '%.*s'\n", |
| 392 | data.c_str(), static_cast<int>(name.size()), name.data()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
| 396 | if (in_vector()) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 397 | stack_.back().vector_elements.emplace_back(*int_value); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 398 | } else { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 399 | stack_.back().elements.emplace_back(field_index, *int_value); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | case flatbuffers::ET_UTYPE: |
| 404 | case flatbuffers::ET_BOOL: |
| 405 | case flatbuffers::ET_FLOAT: |
| 406 | case flatbuffers::ET_DOUBLE: |
| 407 | case flatbuffers::ET_STRING: |
| 408 | case flatbuffers::ET_SEQUENCE: |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | if (in_vector()) { |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 413 | stack_.back().vector_elements.emplace_back(fbb_->CreateString(data)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 414 | } else { |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 415 | stack_.back().elements.emplace_back(field_index, fbb_->CreateString(data)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 416 | } |
| 417 | return true; |
| 418 | } |
| 419 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 420 | bool AddSingleElement(FlatbufferType type, const FieldElement &field_element, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 421 | ::std::vector<bool> *fields_in_use, |
| 422 | flatbuffers::FlatBufferBuilder *fbb) { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 423 | if ((*fields_in_use)[field_element.field_index]) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 424 | const std::string_view name = type.FieldName(field_element.field_index); |
| 425 | fprintf(stderr, "Duplicate field: '%.*s'\n", static_cast<int>(name.size()), |
| 426 | name.data()); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
| 430 | (*fields_in_use)[field_element.field_index] = true; |
| 431 | |
| 432 | switch (field_element.element.type) { |
| 433 | case Element::ElementType::INT: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 434 | return AddSingleElement(type, field_element.field_index, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 435 | field_element.element.int_element, fbb); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 436 | case Element::ElementType::DOUBLE: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 437 | return AddSingleElement(type, field_element.field_index, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 438 | field_element.element.double_element, fbb); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 439 | case Element::ElementType::OFFSET: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 440 | return AddSingleElement(type, field_element.field_index, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 441 | field_element.element.offset_element, fbb); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 442 | } |
| 443 | return false; |
| 444 | } |
| 445 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 446 | bool AddSingleElement(FlatbufferType type, int field_index, int64_t int_value, |
| 447 | flatbuffers::FlatBufferBuilder *fbb |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 448 | |
| 449 | ) { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 450 | flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset( |
| 451 | static_cast<flatbuffers::voffset_t>(field_index)); |
| 452 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 453 | const flatbuffers::ElementaryType elementary_type = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 454 | type.FieldElementaryType(field_index); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 455 | switch (elementary_type) { |
| 456 | case flatbuffers::ET_BOOL: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 457 | fbb->AddElement<bool>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 458 | return true; |
| 459 | case flatbuffers::ET_CHAR: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 460 | fbb->AddElement<int8_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 461 | return true; |
| 462 | case flatbuffers::ET_UCHAR: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 463 | fbb->AddElement<uint8_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 464 | return true; |
| 465 | case flatbuffers::ET_SHORT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 466 | fbb->AddElement<int16_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 467 | return true; |
| 468 | case flatbuffers::ET_USHORT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 469 | fbb->AddElement<uint16_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 470 | return true; |
| 471 | case flatbuffers::ET_INT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 472 | fbb->AddElement<int32_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 473 | return true; |
| 474 | case flatbuffers::ET_UINT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 475 | fbb->AddElement<uint32_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 476 | return true; |
| 477 | case flatbuffers::ET_LONG: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 478 | fbb->AddElement<int64_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 479 | return true; |
| 480 | case flatbuffers::ET_ULONG: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 481 | fbb->AddElement<uint64_t>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 482 | return true; |
| 483 | case flatbuffers::ET_FLOAT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 484 | fbb->AddElement<float>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 485 | return true; |
| 486 | case flatbuffers::ET_DOUBLE: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 487 | fbb->AddElement<double>(field_offset, int_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 488 | return true; |
| 489 | case flatbuffers::ET_STRING: |
| 490 | case flatbuffers::ET_UTYPE: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 491 | case flatbuffers::ET_SEQUENCE: { |
| 492 | const std::string_view name = type.FieldName(field_index); |
| 493 | fprintf(stderr, |
| 494 | "Mismatched type for field '%.*s'. Got: integer, expected %s\n", |
| 495 | static_cast<int>(name.size()), name.data(), |
| 496 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 497 | return false; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 498 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 499 | }; |
| 500 | return false; |
| 501 | } |
| 502 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 503 | bool AddSingleElement(FlatbufferType type, int field_index, double double_value, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 504 | flatbuffers::FlatBufferBuilder *fbb) { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 505 | flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset( |
| 506 | static_cast<flatbuffers::voffset_t>(field_index)); |
| 507 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 508 | const flatbuffers::ElementaryType elementary_type = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 509 | type.FieldElementaryType(field_index); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 510 | switch (elementary_type) { |
| 511 | case flatbuffers::ET_UTYPE: |
| 512 | case flatbuffers::ET_BOOL: |
| 513 | case flatbuffers::ET_CHAR: |
| 514 | case flatbuffers::ET_UCHAR: |
| 515 | case flatbuffers::ET_SHORT: |
| 516 | case flatbuffers::ET_USHORT: |
| 517 | case flatbuffers::ET_INT: |
| 518 | case flatbuffers::ET_UINT: |
| 519 | case flatbuffers::ET_LONG: |
| 520 | case flatbuffers::ET_ULONG: |
| 521 | case flatbuffers::ET_STRING: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 522 | case flatbuffers::ET_SEQUENCE: { |
| 523 | const std::string_view name = type.FieldName(field_index); |
| 524 | fprintf(stderr, |
| 525 | "Mismatched type for field '%.*s'. Got: double, expected %s\n", |
| 526 | static_cast<int>(name.size()), name.data(), |
| 527 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 528 | return false; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 529 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 530 | case flatbuffers::ET_FLOAT: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 531 | fbb->AddElement<float>(field_offset, double_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 532 | return true; |
| 533 | case flatbuffers::ET_DOUBLE: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 534 | fbb->AddElement<double>(field_offset, double_value, 0); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | return false; |
| 538 | } |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 539 | bool AddSingleElement(FlatbufferType type, int field_index, |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 540 | flatbuffers::Offset<flatbuffers::String> offset_element, |
| 541 | flatbuffers::FlatBufferBuilder *fbb) { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 542 | flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset( |
| 543 | static_cast<flatbuffers::voffset_t>(field_index)); |
| 544 | |
| 545 | // Vectors will always be Offset<>'s. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 546 | if (type.FieldIsRepeating(field_index)) { |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 547 | fbb->AddOffset(field_offset, offset_element); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | |
| 551 | const flatbuffers::ElementaryType elementary_type = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 552 | type.FieldElementaryType(field_index); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 553 | switch (elementary_type) { |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 554 | case flatbuffers::ET_CHAR: |
| 555 | case flatbuffers::ET_UCHAR: |
| 556 | case flatbuffers::ET_SHORT: |
| 557 | case flatbuffers::ET_USHORT: |
| 558 | case flatbuffers::ET_INT: |
| 559 | case flatbuffers::ET_UINT: |
| 560 | case flatbuffers::ET_LONG: |
| 561 | case flatbuffers::ET_ULONG: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 562 | case flatbuffers::ET_UTYPE: |
| 563 | case flatbuffers::ET_BOOL: |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 564 | case flatbuffers::ET_FLOAT: |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 565 | case flatbuffers::ET_DOUBLE: { |
| 566 | const std::string_view name = type.FieldName(field_index); |
| 567 | fprintf(stderr, |
| 568 | "Mismatched type for field '%.*s'. Got: string, expected %s\n", |
| 569 | static_cast<int>(name.size()), name.data(), |
| 570 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 571 | return false; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 572 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 573 | case flatbuffers::ET_STRING: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 574 | case flatbuffers::ET_SEQUENCE: |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 575 | fbb->AddOffset(field_offset, offset_element); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 576 | return true; |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | bool JsonParser::FinishVector(int field_index) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 582 | // Vectors have a start (unfortunately which needs to know the size) |
| 583 | fbb_->StartVector(stack_.back().vector_elements.size(), |
| 584 | stack_.back().type.FieldInlineSize(field_index)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 585 | |
| 586 | const flatbuffers::ElementaryType elementary_type = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 587 | stack_.back().type.FieldElementaryType(field_index); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 588 | |
| 589 | // Then the data (in reverse order for some reason...) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 590 | for (size_t i = stack_.back().vector_elements.size(); i > 0;) { |
| 591 | const Element &element = stack_.back().vector_elements[--i]; |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 592 | switch (element.type) { |
| 593 | case Element::ElementType::INT: |
| 594 | if (!PushElement(elementary_type, element.int_element)) return false; |
| 595 | break; |
| 596 | case Element::ElementType::DOUBLE: |
| 597 | if (!PushElement(elementary_type, element.double_element)) return false; |
| 598 | break; |
| 599 | case Element::ElementType::OFFSET: |
| 600 | if (!PushElement(elementary_type, element.offset_element)) return false; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Then an End which is placed into the buffer the same as any other offset. |
| 606 | stack_.back().elements.emplace_back( |
| 607 | field_index, flatbuffers::Offset<flatbuffers::String>( |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 608 | fbb_->EndVector(stack_.back().vector_elements.size()))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 609 | stack_.back().vector_elements.clear(); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 610 | return true; |
| 611 | } |
| 612 | |
| 613 | bool JsonParser::PushElement(flatbuffers::ElementaryType elementary_type, |
| 614 | int64_t int_value) { |
| 615 | switch (elementary_type) { |
| 616 | case flatbuffers::ET_BOOL: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 617 | fbb_->PushElement<bool>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 618 | return true; |
| 619 | case flatbuffers::ET_CHAR: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 620 | fbb_->PushElement<int8_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 621 | return true; |
| 622 | case flatbuffers::ET_UCHAR: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 623 | fbb_->PushElement<uint8_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 624 | return true; |
| 625 | case flatbuffers::ET_SHORT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 626 | fbb_->PushElement<int16_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 627 | return true; |
| 628 | case flatbuffers::ET_USHORT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 629 | fbb_->PushElement<uint16_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 630 | return true; |
| 631 | case flatbuffers::ET_INT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 632 | fbb_->PushElement<int32_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 633 | return true; |
| 634 | case flatbuffers::ET_UINT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 635 | fbb_->PushElement<uint32_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 636 | return true; |
| 637 | case flatbuffers::ET_LONG: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 638 | fbb_->PushElement<int64_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 639 | return true; |
| 640 | case flatbuffers::ET_ULONG: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 641 | fbb_->PushElement<uint64_t>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 642 | return true; |
| 643 | case flatbuffers::ET_FLOAT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 644 | fbb_->PushElement<float>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 645 | return true; |
| 646 | case flatbuffers::ET_DOUBLE: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 647 | fbb_->PushElement<double>(int_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 648 | return true; |
| 649 | case flatbuffers::ET_STRING: |
| 650 | case flatbuffers::ET_UTYPE: |
| 651 | case flatbuffers::ET_SEQUENCE: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 652 | fprintf(stderr, |
| 653 | "Mismatched type for field '%s'. Got: integer, expected %s\n", |
| 654 | stack_.back().field_name.c_str(), |
| 655 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 656 | return false; |
| 657 | }; |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | bool JsonParser::PushElement(flatbuffers::ElementaryType elementary_type, |
| 662 | double double_value) { |
| 663 | switch (elementary_type) { |
| 664 | case flatbuffers::ET_UTYPE: |
| 665 | case flatbuffers::ET_BOOL: |
| 666 | case flatbuffers::ET_CHAR: |
| 667 | case flatbuffers::ET_UCHAR: |
| 668 | case flatbuffers::ET_SHORT: |
| 669 | case flatbuffers::ET_USHORT: |
| 670 | case flatbuffers::ET_INT: |
| 671 | case flatbuffers::ET_UINT: |
| 672 | case flatbuffers::ET_LONG: |
| 673 | case flatbuffers::ET_ULONG: |
| 674 | case flatbuffers::ET_STRING: |
| 675 | case flatbuffers::ET_SEQUENCE: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 676 | fprintf(stderr, |
| 677 | "Mismatched type for field '%s'. Got: double, expected %s\n", |
| 678 | stack_.back().field_name.c_str(), |
| 679 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 680 | return false; |
| 681 | case flatbuffers::ET_FLOAT: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 682 | fbb_->PushElement<float>(double_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 683 | return true; |
| 684 | case flatbuffers::ET_DOUBLE: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 685 | fbb_->PushElement<double>(double_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 686 | return true; |
| 687 | } |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | bool JsonParser::PushElement( |
| 692 | flatbuffers::ElementaryType elementary_type, |
| 693 | flatbuffers::Offset<flatbuffers::String> offset_value) { |
| 694 | switch (elementary_type) { |
| 695 | case flatbuffers::ET_UTYPE: |
| 696 | case flatbuffers::ET_BOOL: |
| 697 | case flatbuffers::ET_CHAR: |
| 698 | case flatbuffers::ET_UCHAR: |
| 699 | case flatbuffers::ET_SHORT: |
| 700 | case flatbuffers::ET_USHORT: |
| 701 | case flatbuffers::ET_INT: |
| 702 | case flatbuffers::ET_UINT: |
| 703 | case flatbuffers::ET_LONG: |
| 704 | case flatbuffers::ET_ULONG: |
| 705 | case flatbuffers::ET_FLOAT: |
| 706 | case flatbuffers::ET_DOUBLE: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 707 | fprintf(stderr, |
| 708 | "Mismatched type for field '%s'. Got: sequence, expected %s\n", |
| 709 | stack_.back().field_name.c_str(), |
| 710 | ElementaryTypeName(elementary_type)); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 711 | return false; |
| 712 | case flatbuffers::ET_STRING: |
| 713 | case flatbuffers::ET_SEQUENCE: |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 714 | fbb_->PushElement(offset_value); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | } // namespace |
| 721 | |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 722 | flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer( |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 723 | const std::string_view data, FlatbufferType type, |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 724 | flatbuffers::FlatBufferBuilder *fbb) { |
| 725 | JsonParser p(fbb); |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 726 | return p.Parse(data, type); |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 729 | flatbuffers::DetachedBuffer JsonToFlatbuffer(const std::string_view data, |
| 730 | FlatbufferType type) { |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 731 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 732 | fbb.ForceDefaults(true); |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 733 | |
| 734 | const flatbuffers::Offset<flatbuffers::Table> result = |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 735 | JsonToFlatbuffer(data, type, &fbb); |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 736 | if (result.o != 0) { |
| 737 | fbb.Finish(result); |
| 738 | |
| 739 | return fbb.Release(); |
| 740 | } else { |
| 741 | // Otherwise return an empty vector. |
| 742 | return flatbuffers::DetachedBuffer(); |
| 743 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 746 | namespace { |
| 747 | |
| 748 | // A visitor which manages skipping the contents of vectors that are longer than |
| 749 | // a specified threshold. |
| 750 | class TruncatingStringVisitor : public flatbuffers::IterationVisitor { |
| 751 | public: |
| 752 | TruncatingStringVisitor(size_t max_vector_size, std::string delimiter, |
| 753 | bool quotes, std::string indent, bool vdelimited) |
| 754 | : max_vector_size_(max_vector_size), |
| 755 | to_string_(delimiter, quotes, indent, vdelimited) {} |
| 756 | ~TruncatingStringVisitor() override {} |
| 757 | |
| 758 | void StartSequence() override { |
| 759 | if (should_skip()) return; |
| 760 | to_string_.StartSequence(); |
| 761 | } |
| 762 | void EndSequence() override { |
| 763 | if (should_skip()) return; |
| 764 | to_string_.EndSequence(); |
| 765 | } |
| 766 | void Field(size_t field_idx, size_t set_idx, flatbuffers::ElementaryType type, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 767 | bool is_repeating, const flatbuffers::TypeTable *type_table, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 768 | const char *name, const uint8_t *val) override { |
| 769 | if (should_skip()) return; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 770 | to_string_.Field(field_idx, set_idx, type, is_repeating, type_table, name, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 771 | val); |
| 772 | } |
| 773 | void UType(uint8_t value, const char *name) override { |
| 774 | if (should_skip()) return; |
| 775 | to_string_.UType(value, name); |
| 776 | } |
| 777 | void Bool(bool value) override { |
| 778 | if (should_skip()) return; |
| 779 | to_string_.Bool(value); |
| 780 | } |
| 781 | void Char(int8_t value, const char *name) override { |
| 782 | if (should_skip()) return; |
| 783 | to_string_.Char(value, name); |
| 784 | } |
| 785 | void UChar(uint8_t value, const char *name) override { |
| 786 | if (should_skip()) return; |
| 787 | to_string_.UChar(value, name); |
| 788 | } |
| 789 | void Short(int16_t value, const char *name) override { |
| 790 | if (should_skip()) return; |
| 791 | to_string_.Short(value, name); |
| 792 | } |
| 793 | void UShort(uint16_t value, const char *name) override { |
| 794 | if (should_skip()) return; |
| 795 | to_string_.UShort(value, name); |
| 796 | } |
| 797 | void Int(int32_t value, const char *name) override { |
| 798 | if (should_skip()) return; |
| 799 | to_string_.Int(value, name); |
| 800 | } |
| 801 | void UInt(uint32_t value, const char *name) override { |
| 802 | if (should_skip()) return; |
| 803 | to_string_.UInt(value, name); |
| 804 | } |
| 805 | void Long(int64_t value) override { |
| 806 | if (should_skip()) return; |
| 807 | to_string_.Long(value); |
| 808 | } |
| 809 | void ULong(uint64_t value) override { |
| 810 | if (should_skip()) return; |
| 811 | to_string_.ULong(value); |
| 812 | } |
| 813 | void Float(float value) override { |
| 814 | if (should_skip()) return; |
| 815 | to_string_.Float(value); |
| 816 | } |
| 817 | void Double(double value) override { |
| 818 | if (should_skip()) return; |
| 819 | to_string_.Double(value); |
| 820 | } |
| 821 | void String(const flatbuffers::String *value) override { |
| 822 | if (should_skip()) return; |
| 823 | to_string_.String(value); |
| 824 | } |
| 825 | void Unknown(const uint8_t *value) override { |
| 826 | if (should_skip()) return; |
| 827 | to_string_.Unknown(value); |
| 828 | } |
| 829 | void Element(size_t i, flatbuffers::ElementaryType type, |
| 830 | const flatbuffers::TypeTable *type_table, |
| 831 | const uint8_t *val) override { |
| 832 | if (should_skip()) return; |
| 833 | to_string_.Element(i, type, type_table, val); |
| 834 | } |
| 835 | |
| 836 | virtual void StartVector(size_t size) override { |
| 837 | if (should_skip()) { |
| 838 | ++skip_levels_; |
| 839 | return; |
| 840 | } |
| 841 | if (size > max_vector_size_) { |
| 842 | ++skip_levels_; |
| 843 | to_string_.s += "[ ... " + std::to_string(size) + " elements ... ]"; |
| 844 | return; |
| 845 | } |
| 846 | to_string_.StartVector(size); |
| 847 | } |
| 848 | virtual void EndVector() override { |
| 849 | if (should_skip()) { |
| 850 | --skip_levels_; |
| 851 | return; |
| 852 | } |
| 853 | to_string_.EndVector(); |
| 854 | } |
| 855 | |
| 856 | std::string &string() { return to_string_.s; } |
| 857 | |
| 858 | private: |
| 859 | bool should_skip() const { return skip_levels_ > 0; } |
| 860 | |
| 861 | const size_t max_vector_size_; |
| 862 | flatbuffers::ToStringVisitor to_string_; |
| 863 | int skip_levels_ = 0; |
| 864 | }; |
| 865 | |
| 866 | } // namespace |
| 867 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 868 | ::std::string TableFlatbufferToJson(const flatbuffers::Table *t, |
| 869 | const ::flatbuffers::TypeTable *typetable, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 870 | JsonOptions json_options) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 871 | // It is pretty common to get passed in a nullptr when a test fails. Rather |
| 872 | // than CHECK, return a more user friendly result. |
| 873 | if (t == nullptr) { |
| 874 | return "null"; |
| 875 | } |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 876 | TruncatingStringVisitor tostring_visitor( |
| 877 | json_options.max_vector_size, json_options.multi_line ? "\n" : " ", true, |
| 878 | json_options.multi_line ? " " : "", json_options.multi_line); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 879 | flatbuffers::IterateObject(reinterpret_cast<const uint8_t *>(t), typetable, |
| 880 | &tostring_visitor); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 881 | return tostring_visitor.string(); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 884 | } // namespace aos |