Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame^] | 1 | #ifndef AOS_JSON_TO_FLATBUFFER_H_ |
| 2 | #define AOS_JSON_TO_FLATBUFFER_H_ |
| 3 | |
| 4 | #include <cstddef> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "flatbuffers/flatbuffers.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | |
| 11 | // Parses the flatbuffer into the vector, or returns an empty vector. |
| 12 | ::std::vector<uint8_t> JsonToFlatbuffer( |
| 13 | const char *data, const flatbuffers::TypeTable *typetable); |
| 14 | |
| 15 | // Converts a flatbuffer into a Json string. |
| 16 | // |
| 17 | // multi_line controls if the Json is written out on multiple lines or one. |
| 18 | ::std::string FlatbufferToJson(const uint8_t *buffer, |
| 19 | const flatbuffers::TypeTable *typetable, |
| 20 | bool multi_line = false); |
| 21 | |
| 22 | // This class implements the state machine at json.org |
| 23 | class Tokenizer { |
| 24 | public: |
| 25 | Tokenizer(const char *data) : data_(data) {} |
| 26 | |
| 27 | enum class TokenType { |
| 28 | kEnd, |
| 29 | kError, |
| 30 | kStartObject, |
| 31 | kEndObject, |
| 32 | kStartArray, |
| 33 | kEndArray, |
| 34 | kField, |
| 35 | kNumberValue, |
| 36 | kStringValue, |
| 37 | kTrueValue, |
| 38 | kFalseValue, |
| 39 | }; |
| 40 | |
| 41 | // Returns the next token. |
| 42 | TokenType Next(); |
| 43 | |
| 44 | // Returns the last field_name and field_value. These are only valid when |
| 45 | // Next returns them. |
| 46 | const ::std::string &field_name() const { return field_name_; } |
| 47 | const ::std::string &field_value() const { return field_value_; } |
| 48 | |
| 49 | // Parses the current field value as a long long. Returns false if it failed |
| 50 | // to parse. |
| 51 | bool FieldAsInt(long long *value); |
| 52 | // Parses the current field value as a double. Returns false if it failed |
| 53 | // to parse. |
| 54 | bool FieldAsDouble(double *value); |
| 55 | |
| 56 | // Returns true if we are at the end of the input. |
| 57 | bool AtEnd() { return *data_ == '\0'; } |
| 58 | |
| 59 | const char *data_left() const { return data_; } |
| 60 | |
| 61 | private: |
| 62 | // Consumes a string out of data_. Populates s with the string. Returns true |
| 63 | // if a valid string was found, and false otherwise. |
| 64 | // data_ is updated only on success. |
| 65 | bool ConsumeString(::std::string *s); |
| 66 | // Consumes a number out of data_. Populates s with the string containing the |
| 67 | // number. Returns true if a valid number was found, and false otherwise. |
| 68 | // data_ is updated only on success. |
| 69 | bool ConsumeNumber(::std::string *s); |
| 70 | // Consumes a fixed token out of data_. Returns true if the string was found, |
| 71 | // and false otherwise. |
| 72 | // data_ is updated only on success. |
| 73 | bool Consume(const char* token); |
| 74 | // Consumes whitespace out of data_. Returns true if the string was found, |
| 75 | // and false otherwise. |
| 76 | // data_ is unconditionally updated. |
| 77 | void ConsumeWhitespace(); |
| 78 | |
| 79 | // State for the parsing state machine. |
| 80 | enum class State { |
| 81 | kExpectField, |
| 82 | kExpectObjectStart, |
| 83 | kExpectObjectEnd, |
| 84 | kExpectArrayEnd, |
| 85 | kExpectValue, |
| 86 | kExpectEnd, |
| 87 | }; |
| 88 | |
| 89 | State state_ = State::kExpectObjectStart; |
| 90 | |
| 91 | // Data pointer. |
| 92 | const char *data_; |
| 93 | // Current line number used for printing debug. |
| 94 | int linenumber_ = 0; |
| 95 | |
| 96 | // Stack used to track which object type we were in when we recursed. |
| 97 | enum class ObjectType { |
| 98 | kObject, |
| 99 | kArray, |
| 100 | }; |
| 101 | ::std::vector<ObjectType> object_type_; |
| 102 | |
| 103 | // Last field name. |
| 104 | ::std::string field_name_; |
| 105 | // Last field value. |
| 106 | ::std::string field_value_; |
| 107 | }; |
| 108 | } // namespace aos |
| 109 | |
| 110 | #endif // AOS_JSON_TO_FLATBUFFER_H_ |