blob: c16094c09a4f2d8a8ca540d622ffd7788e11df6c [file] [log] [blame]
Austin Schuh3e95e5d2019-09-20 00:08:54 -07001#include "aos/json_to_flatbuffer.h"
2
3#include <cstddef>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdio>
James Kuszmaul3ae42262019-11-08 12:33:41 -08005#include <string_view>
6
Austin Schuh3e95e5d2019-09-20 00:08:54 -07007#include "flatbuffers/flatbuffers.h"
8#include "flatbuffers/minireflect.h"
Austin Schuhe93d8642019-10-13 15:27:07 -07009#include "glog/logging.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070010
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "aos/flatbuffer_utils.h"
12#include "aos/json_tokenizer.h"
13
Austin Schuh3e95e5d2019-09-20 00:08:54 -070014// 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
20namespace aos {
Austin Schuh3e95e5d2019-09-20 00:08:54 -070021namespace {
22
23// Class to hold one of the 3 json types for an array.
24struct Element {
25 // The type.
James Kuszmaul768c4682023-10-12 21:07:16 -070026 enum class ElementType { INT, DOUBLE, OFFSET, STRUCT };
Austin Schuh3e95e5d2019-09-20 00:08:54 -070027
28 // Constructs an Element holding an integer.
James Kuszmaul768c4682023-10-12 21:07:16 -070029 Element(absl::int128 new_int_element)
Austin Schuh3e95e5d2019-09-20 00:08:54 -070030 : 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 Kuszmaul768c4682023-10-12 21:07:16 -070037 // 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 Schuh3e95e5d2019-09-20 00:08:54 -070043
44 // Union for the various datatypes.
45 union {
James Kuszmaul768c4682023-10-12 21:07:16 -070046 absl::int128 int_element;
Austin Schuh3e95e5d2019-09-20 00:08:54 -070047 double double_element;
48 flatbuffers::Offset<flatbuffers::String> offset_element;
49 };
James Kuszmaul768c4682023-10-12 21:07:16 -070050 // 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 Schuh3e95e5d2019-09-20 00:08:54 -070055
56 // And an enum signaling which one is in use.
57 ElementType type;
58};
59
60// Structure to represent a field element.
61struct FieldElement {
James Kuszmaul768c4682023-10-12 21:07:16 -070062 FieldElement(int new_field_index, absl::int128 int_element)
Austin Schuh3e95e5d2019-09-20 00:08:54 -070063 : 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 Kuszmaul768c4682023-10-12 21:07:16 -070069 FieldElement(int new_field_index, const Element &element)
70 : element(element), field_index(new_field_index) {}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070071
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 Schuh43c6a352019-09-30 22:22:10 -070079// Adds a single element. This assumes that vectors have been dealt with
80// already. Returns true on success.
Brian Silvermancf4fb662021-02-10 17:54:53 -080081bool AddSingleElement(FlatbufferType type, const FieldElement &field_element,
Austin Schuh43c6a352019-09-30 22:22:10 -070082 ::std::vector<bool> *fields_in_use,
83 flatbuffers::FlatBufferBuilder *fbb);
James Kuszmaul768c4682023-10-12 21:07:16 -070084bool AddSingleElement(FlatbufferType type, int field_index,
85 absl::int128 int_value,
Brian Silvermancf4fb662021-02-10 17:54:53 -080086 flatbuffers::FlatBufferBuilder *fbb);
87bool AddSingleElement(FlatbufferType type, int field_index, double double_value,
88 flatbuffers::FlatBufferBuilder *fbb);
89bool AddSingleElement(FlatbufferType type, int field_index,
Austin Schuh43c6a352019-09-30 22:22:10 -070090 flatbuffers::Offset<flatbuffers::String> offset_element,
91 flatbuffers::FlatBufferBuilder *fbb);
James Kuszmaul768c4682023-10-12 21:07:16 -070092bool AddSingleElement(FlatbufferType type, int field_index,
93 const std::vector<uint8_t> &struct_data,
94 flatbuffers::FlatBufferBuilder *fbb);
95
96template <typename T, typename U>
97void 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
104bool 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
156bool 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 Schuh43c6a352019-09-30 22:22:10 -0700189
Brian Silvermancf4fb662021-02-10 17:54:53 -0800190// Writes an array of FieldElement (with the definition in "type") to the
191// builder. Returns the offset of the resulting table.
James Kuszmaul768c4682023-10-12 21:07:16 -0700192std::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 Schuh43c6a352019-09-30 22:22:10 -0700198
James Kuszmaul768c4682023-10-12 21:07:16 -0700199 ::std::vector<bool> fields_in_use(type.NumberFields(), false);
Austin Schuh43c6a352019-09-30 22:22:10 -0700200
James Kuszmaul768c4682023-10-12 21:07:16 -0700201 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 Schuh43c6a352019-09-30 22:22:10 -0700255 }
James Kuszmaul768c4682023-10-12 21:07:16 -0700256 LOG(FATAL) << "Unimplemented.";
Austin Schuh43c6a352019-09-30 22:22:10 -0700257}
258
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700259// 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 Schuh43c6a352019-09-30 22:22:10 -0700266// 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 Schuh3e95e5d2019-09-20 00:08:54 -0700269class JsonParser {
270 public:
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800271 JsonParser(flatbuffers::FlatBufferBuilder *fbb) : fbb_(fbb) {}
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700272 ~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 Silvermancf4fb662021-02-10 17:54:53 -0800276 flatbuffers::Offset<flatbuffers::Table> Parse(const std::string_view data,
277 FlatbufferType type) {
Austin Schuh43c6a352019-09-30 22:22:10 -0700278 flatbuffers::uoffset_t end = 0;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800279 bool result = DoParse(type, data, &end);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700280
281 if (result) {
282 // On success, finish the table and build the vector.
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800283 return flatbuffers::Offset<flatbuffers::Table>(end);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700284 } else {
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800285 return flatbuffers::Offset<flatbuffers::Table>(0);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700286 }
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 Silvermancf4fb662021-02-10 17:54:53 -0800296 bool DoParse(FlatbufferType type, const std::string_view data,
297 flatbuffers::uoffset_t *table_end);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700298
299 // Adds *_value for the provided field. If we are in a vector, queues the
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300 // data up in vector_elements. Returns true on success.
James Kuszmaul768c4682023-10-12 21:07:16 -0700301 bool AddElement(int field_index, absl::int128 int_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700302 bool AddElement(int field_index, double double_value);
303 bool AddElement(int field_index, const ::std::string &data);
304
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700305 // 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 Kuszmaul768c4682023-10-12 21:07:16 -0700310 absl::int128 int_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700311 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 Kuszmaul768c4682023-10-12 21:07:16 -0700315 bool PushElement(const FlatbufferType &type,
316 const std::vector<uint8_t> &struct_data);
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800317 flatbuffers::FlatBufferBuilder *fbb_;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700318
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 Silvermancf4fb662021-02-10 17:54:53 -0800323 FlatbufferType type;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700324 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700333
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 Kuszmaul768c4682023-10-12 21:07:16 -0700337 // logic inside flatbuffers, or build up vectors of the data. Vectors
338 // will be a bit of extra stack space, but whatever.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 //
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 Schuh3e95e5d2019-09-20 00:08:54 -0700344 };
345 ::std::vector<FlatBufferContext> stack_;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700346};
347
Brian Silvermancf4fb662021-02-10 17:54:53 -0800348bool JsonParser::DoParse(FlatbufferType type, const std::string_view data,
Austin Schuhd339a9b2019-10-05 21:33:32 -0700349 flatbuffers::uoffset_t *table_end) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800350 ::std::vector<FlatbufferType> stack;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700351
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 Schuh217a9782019-12-21 23:02:50 -0800361 fprintf(stderr, "Failed to unwind stack all the way\n");
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700362 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 Silvermancf4fb662021-02-10 17:54:53 -0800373 stack_.push_back({type, false, -1, "", {}, {}});
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700374 } else {
375 int field_index = stack_.back().field_index;
376
Brian Silvermancf4fb662021-02-10 17:54:53 -0800377 if (!stack_.back().type.FieldIsSequence(field_index)) {
Austin Schuh217a9782019-12-21 23:02:50 -0800378 fprintf(stderr, "Field '%s' is not a sequence\n",
379 stack_.back().field_name.c_str());
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700380 return false;
381 }
382
James Kuszmaulbb60dfd2023-01-05 17:13:11 -0800383 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 Silvermancf4fb662021-02-10 17:54:53 -0800391 stack_.push_back({stack_.back().type.FieldType(field_index),
392 false,
393 -1,
394 "",
395 {},
396 {}});
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700397 }
398 break;
399 case Tokenizer::TokenType::kEndObject: // }
400 if (stack_.size() == 0) {
401 // Somehow we popped more than we pushed. Error.
Austin Schuh217a9782019-12-21 23:02:50 -0800402 fprintf(stderr, "Empty stack\n");
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700403 return false;
404 } else {
James Kuszmaul768c4682023-10-12 21:07:16 -0700405 // 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 Schuh3e95e5d2019-09-20 00:08:54 -0700411
412 // We now want to talk about the parent structure. Pop the child.
413 stack_.pop_back();
414
415 if (stack_.size() == 0) {
James Kuszmaul768c4682023-10-12 21:07:16 -0700416 CHECK_EQ(static_cast<int>(object->type),
417 static_cast<int>(Element::ElementType::OFFSET))
418 << ": JSON parsing only supports parsing flatbuffer tables.";
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700419 // Instead of queueing it up in the stack, return it through the
420 // passed in variable.
James Kuszmaul768c4682023-10-12 21:07:16 -0700421 *table_end = object->offset_element.o;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700422 } 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 Perrycb7da4b2019-08-28 19:35:56 -0700428 stack_.back().vector_elements.emplace_back(
James Kuszmaul768c4682023-10-12 21:07:16 -0700429 std::move(object.value()));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700430 } else {
James Kuszmaul768c4682023-10-12 21:07:16 -0700431 stack_.back().elements.emplace_back(field_index,
432 std::move(object.value()));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700433 }
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 Kuszmaul768c4682023-10-12 21:07:16 -0700467 absl::int128 int_value;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700468 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 Kuszmaul768c4682023-10-12 21:07:16 -0700486 absl::int128 val = int_value;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700487 if (!AddElement(field_index, val)) return false;
488 } else {
489 if (!AddElement(field_index, double_value)) return false;
490 }
491 } break;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700492 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 Silvermancf4fb662021-02-10 17:54:53 -0800501 stack_.back().field_index =
502 stack_.back().type.FieldIndex(stack_.back().field_name.c_str());
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700503
504 if (stack_.back().field_index == -1) {
Austin Schuh217a9782019-12-21 23:02:50 -0800505 fprintf(stderr, "Invalid field name '%s'\n",
506 stack_.back().field_name.c_str());
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700507 return false;
508 }
509 } break;
510 }
511 }
512 return false;
513}
514
James Kuszmaul768c4682023-10-12 21:07:16 -0700515bool JsonParser::AddElement(int field_index, absl::int128 int_value) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800516 if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800517 fprintf(stderr, "Type and json disagree on if we are in a vector or not\n");
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700518 return false;
519 }
520
521 if (in_vector()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700522 stack_.back().vector_elements.emplace_back(int_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700523 } else {
524 stack_.back().elements.emplace_back(field_index, int_value);
525 }
526 return true;
527}
528
529bool JsonParser::AddElement(int field_index, double double_value) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800530 if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800531 fprintf(stderr, "Type and json disagree on if we are in a vector or not\n");
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700532 return false;
533 }
534
535 if (in_vector()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700536 stack_.back().vector_elements.emplace_back(double_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700537 } else {
538 stack_.back().elements.emplace_back(field_index, double_value);
539 }
540 return true;
541}
542
543bool JsonParser::AddElement(int field_index, const ::std::string &data) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800544 if (stack_.back().type.FieldIsRepeating(field_index) != in_vector()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800545 fprintf(stderr, "Type and json disagree on if we are in a vector or not\n");
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700546 return false;
547 }
548
Alex Perrycb7da4b2019-08-28 19:35:56 -0700549 const flatbuffers::ElementaryType elementary_type =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800550 stack_.back().type.FieldElementaryType(field_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700551 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 Silvermancf4fb662021-02-10 17:54:53 -0800560 if (stack_.back().type.FieldIsEnum(field_index)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700561 // We have an enum.
Brian Silvermancf4fb662021-02-10 17:54:53 -0800562 const FlatbufferType type = stack_.back().type;
563 const FlatbufferType enum_type = type.FieldType(field_index);
564 CHECK(enum_type.IsEnum());
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700565
James Kuszmaul768c4682023-10-12 21:07:16 -0700566 const std::optional<absl::int128> int_value = enum_type.EnumValue(data);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567
Brian Silvermancf4fb662021-02-10 17:54:53 -0800568 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 Perrycb7da4b2019-08-28 19:35:56 -0700572 return false;
573 }
574
575 if (in_vector()) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800576 stack_.back().vector_elements.emplace_back(*int_value);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700577 } else {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800578 stack_.back().elements.emplace_back(field_index, *int_value);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700579 }
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 Schuh53b1a6f2020-01-10 19:31:28 -0800592 stack_.back().vector_elements.emplace_back(fbb_->CreateString(data));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700593 } else {
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800594 stack_.back().elements.emplace_back(field_index, fbb_->CreateString(data));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700595 }
596 return true;
597}
598
Brian Silvermancf4fb662021-02-10 17:54:53 -0800599bool AddSingleElement(FlatbufferType type, const FieldElement &field_element,
Austin Schuh43c6a352019-09-30 22:22:10 -0700600 ::std::vector<bool> *fields_in_use,
601 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700602 if ((*fields_in_use)[field_element.field_index]) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800603 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 Schuh3e95e5d2019-09-20 00:08:54 -0700606 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 Silvermancf4fb662021-02-10 17:54:53 -0800613 return AddSingleElement(type, field_element.field_index,
Austin Schuh43c6a352019-09-30 22:22:10 -0700614 field_element.element.int_element, fbb);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700615 case Element::ElementType::DOUBLE:
Brian Silvermancf4fb662021-02-10 17:54:53 -0800616 return AddSingleElement(type, field_element.field_index,
Austin Schuh43c6a352019-09-30 22:22:10 -0700617 field_element.element.double_element, fbb);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700618 case Element::ElementType::OFFSET:
Brian Silvermancf4fb662021-02-10 17:54:53 -0800619 return AddSingleElement(type, field_element.field_index,
Austin Schuh43c6a352019-09-30 22:22:10 -0700620 field_element.element.offset_element, fbb);
James Kuszmaul768c4682023-10-12 21:07:16 -0700621 case Element::ElementType::STRUCT:
622 return AddSingleElement(type, field_element.field_index,
623 field_element.element.struct_data, fbb);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700624 }
625 return false;
626}
627
James Kuszmaul768c4682023-10-12 21:07:16 -0700628bool AddSingleElement(FlatbufferType type, int field_index,
629 absl::int128 int_value,
Brian Silvermancf4fb662021-02-10 17:54:53 -0800630 flatbuffers::FlatBufferBuilder *fbb
Austin Schuh43c6a352019-09-30 22:22:10 -0700631
632) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700633 flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset(
634 static_cast<flatbuffers::voffset_t>(field_index));
635
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700636 const flatbuffers::ElementaryType elementary_type =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800637 type.FieldElementaryType(field_index);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700638 switch (elementary_type) {
639 case flatbuffers::ET_BOOL:
James Kuszmaul768c4682023-10-12 21:07:16 -0700640 fbb->AddElement<bool>(field_offset, static_cast<bool>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700641 return true;
642 case flatbuffers::ET_CHAR:
James Kuszmaul768c4682023-10-12 21:07:16 -0700643 fbb->AddElement<int8_t>(field_offset, static_cast<int8_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700644 return true;
645 case flatbuffers::ET_UCHAR:
James Kuszmaul768c4682023-10-12 21:07:16 -0700646 fbb->AddElement<uint8_t>(field_offset, static_cast<uint8_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700647 return true;
648 case flatbuffers::ET_SHORT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700649 fbb->AddElement<int16_t>(field_offset, static_cast<int16_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700650 return true;
651 case flatbuffers::ET_USHORT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700652 fbb->AddElement<uint16_t>(field_offset, static_cast<uint16_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700653 return true;
654 case flatbuffers::ET_INT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700655 fbb->AddElement<int32_t>(field_offset, static_cast<int32_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700656 return true;
657 case flatbuffers::ET_UINT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700658 fbb->AddElement<uint32_t>(field_offset, static_cast<uint32_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700659 return true;
660 case flatbuffers::ET_LONG:
James Kuszmaul768c4682023-10-12 21:07:16 -0700661 fbb->AddElement<int64_t>(field_offset, static_cast<int64_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700662 return true;
663 case flatbuffers::ET_ULONG:
James Kuszmaul768c4682023-10-12 21:07:16 -0700664 fbb->AddElement<uint64_t>(field_offset, static_cast<uint64_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700665 return true;
James Kuszmaul768c4682023-10-12 21:07:16 -0700666 // The floating point cases occur when someone specifies an integer in the
667 // JSON for a double field.
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700668 case flatbuffers::ET_FLOAT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700669 fbb->AddElement<float>(field_offset, static_cast<float>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700670 return true;
671 case flatbuffers::ET_DOUBLE:
James Kuszmaul768c4682023-10-12 21:07:16 -0700672 fbb->AddElement<double>(field_offset, static_cast<double>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700673 return true;
674 case flatbuffers::ET_STRING:
675 case flatbuffers::ET_UTYPE:
Brian Silvermancf4fb662021-02-10 17:54:53 -0800676 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 Schuh3e95e5d2019-09-20 00:08:54 -0700682 return false;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800683 }
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700684 };
685 return false;
686}
687
Brian Silvermancf4fb662021-02-10 17:54:53 -0800688bool AddSingleElement(FlatbufferType type, int field_index, double double_value,
Austin Schuh43c6a352019-09-30 22:22:10 -0700689 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700690 flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset(
691 static_cast<flatbuffers::voffset_t>(field_index));
692
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700693 const flatbuffers::ElementaryType elementary_type =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800694 type.FieldElementaryType(field_index);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700695 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 Silvermancf4fb662021-02-10 17:54:53 -0800707 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 Schuh3e95e5d2019-09-20 00:08:54 -0700713 return false;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800714 }
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700715 case flatbuffers::ET_FLOAT:
Brian Silverman69069232021-11-10 12:26:52 -0800716 fbb->AddElement<float>(field_offset, double_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700717 return true;
718 case flatbuffers::ET_DOUBLE:
Brian Silverman69069232021-11-10 12:26:52 -0800719 fbb->AddElement<double>(field_offset, double_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700720 return true;
721 }
722 return false;
723}
James Kuszmaul768c4682023-10-12 21:07:16 -0700724
Brian Silvermancf4fb662021-02-10 17:54:53 -0800725bool AddSingleElement(FlatbufferType type, int field_index,
Austin Schuh43c6a352019-09-30 22:22:10 -0700726 flatbuffers::Offset<flatbuffers::String> offset_element,
727 flatbuffers::FlatBufferBuilder *fbb) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700728 flatbuffers::voffset_t field_offset = flatbuffers::FieldIndexToOffset(
729 static_cast<flatbuffers::voffset_t>(field_index));
730
731 // Vectors will always be Offset<>'s.
Brian Silvermancf4fb662021-02-10 17:54:53 -0800732 if (type.FieldIsRepeating(field_index)) {
Austin Schuh43c6a352019-09-30 22:22:10 -0700733 fbb->AddOffset(field_offset, offset_element);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700734 return true;
735 }
736
737 const flatbuffers::ElementaryType elementary_type =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800738 type.FieldElementaryType(field_index);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700739 switch (elementary_type) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700740 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 Perrycb7da4b2019-08-28 19:35:56 -0700748 case flatbuffers::ET_UTYPE:
749 case flatbuffers::ET_BOOL:
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700750 case flatbuffers::ET_FLOAT:
Brian Silvermancf4fb662021-02-10 17:54:53 -0800751 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 Schuh3e95e5d2019-09-20 00:08:54 -0700757 return false;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800758 }
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700759 case flatbuffers::ET_STRING:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760 case flatbuffers::ET_SEQUENCE:
Austin Schuh43c6a352019-09-30 22:22:10 -0700761 fbb->AddOffset(field_offset, offset_element);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700762 return true;
763 }
764 return false;
765}
766
James Kuszmaul768c4682023-10-12 21:07:16 -0700767bool 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 Schuh3e95e5d2019-09-20 00:08:54 -0700782bool JsonParser::FinishVector(int field_index) {
Brian Silvermancf4fb662021-02-10 17:54:53 -0800783 // Vectors have a start (unfortunately which needs to know the size)
James Kuszmaul65541cb2022-11-08 14:53:47 -0800784 const size_t inline_size = stack_.back().type.FieldInlineSize(field_index);
James Kuszmaul768c4682023-10-12 21:07:16 -0700785 const size_t alignment = stack_.back().type.FieldInlineAlignment(field_index);
James Kuszmaul65541cb2022-11-08 14:53:47 -0800786 fbb_->StartVector(stack_.back().vector_elements.size(), inline_size,
James Kuszmaul768c4682023-10-12 21:07:16 -0700787 /*align=*/alignment);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700788
789 const flatbuffers::ElementaryType elementary_type =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800790 stack_.back().type.FieldElementaryType(field_index);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700791
792 // Then the data (in reverse order for some reason...)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700793 for (size_t i = stack_.back().vector_elements.size(); i > 0;) {
794 const Element &element = stack_.back().vector_elements[--i];
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700795 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 Kuszmaul768c4682023-10-12 21:07:16 -0700805 case Element::ElementType::STRUCT:
806 if (!PushElement(stack_.back().type.FieldType(field_index),
807 element.struct_data))
808 return false;
809 break;
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700810 }
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 Schuh53b1a6f2020-01-10 19:31:28 -0800816 fbb_->EndVector(stack_.back().vector_elements.size())));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700817 stack_.back().vector_elements.clear();
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700818 return true;
819}
820
821bool JsonParser::PushElement(flatbuffers::ElementaryType elementary_type,
James Kuszmaul768c4682023-10-12 21:07:16 -0700822 absl::int128 int_value) {
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700823 switch (elementary_type) {
824 case flatbuffers::ET_BOOL:
James Kuszmaul768c4682023-10-12 21:07:16 -0700825 fbb_->PushElement<bool>(static_cast<bool>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700826 return true;
827 case flatbuffers::ET_CHAR:
James Kuszmaul768c4682023-10-12 21:07:16 -0700828 fbb_->PushElement<int8_t>(static_cast<int8_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700829 return true;
830 case flatbuffers::ET_UCHAR:
James Kuszmaul768c4682023-10-12 21:07:16 -0700831 fbb_->PushElement<uint8_t>(static_cast<uint8_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700832 return true;
833 case flatbuffers::ET_SHORT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700834 fbb_->PushElement<int16_t>(static_cast<int16_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700835 return true;
836 case flatbuffers::ET_USHORT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700837 fbb_->PushElement<uint16_t>(static_cast<uint16_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700838 return true;
839 case flatbuffers::ET_INT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700840 fbb_->PushElement<int32_t>(static_cast<int32_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700841 return true;
842 case flatbuffers::ET_UINT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700843 fbb_->PushElement<uint32_t>(static_cast<uint32_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700844 return true;
845 case flatbuffers::ET_LONG:
James Kuszmaul768c4682023-10-12 21:07:16 -0700846 fbb_->PushElement<int64_t>(static_cast<int64_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700847 return true;
848 case flatbuffers::ET_ULONG:
James Kuszmaul768c4682023-10-12 21:07:16 -0700849 fbb_->PushElement<uint64_t>(static_cast<uint64_t>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700850 return true;
851 case flatbuffers::ET_FLOAT:
James Kuszmaul768c4682023-10-12 21:07:16 -0700852 fbb_->PushElement<float>(static_cast<float>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700853 return true;
854 case flatbuffers::ET_DOUBLE:
James Kuszmaul768c4682023-10-12 21:07:16 -0700855 fbb_->PushElement<double>(static_cast<double>(int_value));
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700856 return true;
857 case flatbuffers::ET_STRING:
858 case flatbuffers::ET_UTYPE:
859 case flatbuffers::ET_SEQUENCE:
Austin Schuh217a9782019-12-21 23:02:50 -0800860 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 Schuh3e95e5d2019-09-20 00:08:54 -0700864 return false;
865 };
866 return false;
867}
868
869bool 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 Schuh217a9782019-12-21 23:02:50 -0800884 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 Schuh3e95e5d2019-09-20 00:08:54 -0700888 return false;
889 case flatbuffers::ET_FLOAT:
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800890 fbb_->PushElement<float>(double_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700891 return true;
892 case flatbuffers::ET_DOUBLE:
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800893 fbb_->PushElement<double>(double_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700894 return true;
895 }
896 return false;
897}
898
James Kuszmaul768c4682023-10-12 21:07:16 -0700899bool 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 Schuh3e95e5d2019-09-20 00:08:54 -0700910bool 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 Schuh217a9782019-12-21 23:02:50 -0800926 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 Schuh3e95e5d2019-09-20 00:08:54 -0700930 return false;
931 case flatbuffers::ET_STRING:
932 case flatbuffers::ET_SEQUENCE:
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800933 fbb_->PushElement(offset_value);
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700934 return true;
935 }
936 return false;
937}
938
939} // namespace
940
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800941flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
Brian Silvermancf4fb662021-02-10 17:54:53 -0800942 const std::string_view data, FlatbufferType type,
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800943 flatbuffers::FlatBufferBuilder *fbb) {
944 JsonParser p(fbb);
Brian Silvermancf4fb662021-02-10 17:54:53 -0800945 return p.Parse(data, type);
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800946}
947
Brian Silvermancf4fb662021-02-10 17:54:53 -0800948flatbuffers::DetachedBuffer JsonToFlatbuffer(const std::string_view data,
949 FlatbufferType type) {
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800950 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800951 fbb.ForceDefaults(true);
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800952
953 const flatbuffers::Offset<flatbuffers::Table> result =
Brian Silvermancf4fb662021-02-10 17:54:53 -0800954 JsonToFlatbuffer(data, type, &fbb);
Austin Schuh53b1a6f2020-01-10 19:31:28 -0800955 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 Schuh3e95e5d2019-09-20 00:08:54 -0700963}
964
Austin Schuhd3936202020-04-07 20:11:07 -0700965namespace {
966
967// A visitor which manages skipping the contents of vectors that are longer than
968// a specified threshold.
969class 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 Schuh7c75e582020-11-14 16:41:18 -0800986 bool is_repeating, const flatbuffers::TypeTable *type_table,
Austin Schuhd3936202020-04-07 20:11:07 -0700987 const char *name, const uint8_t *val) override {
988 if (should_skip()) return;
Austin Schuh7c75e582020-11-14 16:41:18 -0800989 to_string_.Field(field_idx, set_idx, type, is_repeating, type_table, name,
Austin Schuhd3936202020-04-07 20:11:07 -0700990 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 Schuh041fe9f2021-10-16 23:01:15 -07001062 to_string_.s += "[ \"... " + std::to_string(size) + " elements ...\" ]";
Austin Schuhd3936202020-04-07 20:11:07 -07001063 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 Schuhe93d8642019-10-13 15:27:07 -07001087::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
1088 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -07001089 JsonOptions json_options) {
Austin Schuhe93d8642019-10-13 15:27:07 -07001090 // 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 Jonescf453ab2020-05-06 21:14:53 -07001095 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 Schuhe93d8642019-10-13 15:27:07 -07001098 flatbuffers::IterateObject(reinterpret_cast<const uint8_t *>(t), typetable,
1099 &tostring_visitor);
Austin Schuhd3936202020-04-07 20:11:07 -07001100 return tostring_visitor.string();
Austin Schuh3e95e5d2019-09-20 00:08:54 -07001101}
1102
Austin Schuh3e95e5d2019-09-20 00:08:54 -07001103} // namespace aos