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