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> |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 5 | #include <fstream> |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 6 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 7 | #include <string_view> |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "flatbuffers/flatbuffers.h" |
| 10 | #include "flatbuffers/reflection.h" |
| 11 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 12 | #include "aos/fast_string_builder.h" |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 13 | #include "aos/flatbuffer_utils.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 14 | #include "aos/flatbuffers.h" |
Austin Schuh | bba1028 | 2021-03-20 22:03:28 -0700 | [diff] [blame] | 15 | #include "aos/util/file.h" |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
| 18 | |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 19 | // Parses the flatbuffer into the buffer, or returns an empty buffer. |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 20 | flatbuffers::DetachedBuffer JsonToFlatbuffer(std::string_view data, |
| 21 | FlatbufferType type); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 22 | |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 23 | // Parses the flatbuffer into the builder, and returns the offset. |
| 24 | flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer( |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 25 | std::string_view data, FlatbufferType type, |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 26 | flatbuffers::FlatBufferBuilder *fbb); |
| 27 | |
| 28 | // Typed versions of the above methods. |
| 29 | template <typename T> |
| 30 | inline flatbuffers::DetachedBuffer JsonToFlatbuffer( |
| 31 | const std::string_view data) { |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 32 | return JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable())); |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 33 | } |
| 34 | template <typename T> |
| 35 | inline flatbuffers::Offset<T> JsonToFlatbuffer( |
| 36 | const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) { |
| 37 | return flatbuffers::Offset<T>( |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 38 | JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()), fbb).o); |
Austin Schuh | 53b1a6f | 2020-01-10 19:31:28 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 41 | struct JsonOptions { |
Dan Ford | 44e0251 | 2022-06-07 23:45:14 -0700 | [diff] [blame] | 42 | // controls if the Json is written out on multiple lines or one. |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 43 | bool multi_line = false; |
| 44 | // the contents of vectors longer than max_vector_size will be skipped. |
| 45 | size_t max_vector_size = SIZE_MAX; |
Dan Ford | 44e0251 | 2022-06-07 23:45:14 -0700 | [diff] [blame] | 46 | // more extensive version of multi_line that prints every single field on its |
| 47 | // own line. |
| 48 | bool max_multi_line = false; |
Brian J Griglak | 043e0e2 | 2022-08-18 12:51:18 -0600 | [diff] [blame] | 49 | // Will integers be printed in hexadecimal form instead of decimal. |
| 50 | bool use_hex = false; |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 53 | // Converts a flatbuffer into a Json string. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 54 | // The methods below are generally more useful than TableFlatbufferToJson. |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 55 | ::std::string TableFlatbufferToJson(const flatbuffers::Table *t, |
| 56 | const ::flatbuffers::TypeTable *typetable, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 57 | JsonOptions json_options = {}); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 58 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 59 | // Converts a Flatbuffer<T> holding a flatbuffer to JSON. |
| 60 | template <typename T> |
| 61 | inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 62 | JsonOptions json_options = {}) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 63 | return TableFlatbufferToJson( |
| 64 | reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()), |
| 65 | Flatbuffer<T>::MiniReflectTypeTable(), json_options); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Converts a flatbuffer::Table to JSON. |
| 69 | template <typename T> |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 70 | typename std::enable_if< |
| 71 | std::is_base_of<flatbuffers::Table, T>::value, |
| 72 | std::string>::type inline FlatbufferToJson(const T *flatbuffer, |
| 73 | JsonOptions json_options = {}) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 74 | return TableFlatbufferToJson( |
| 75 | reinterpret_cast<const flatbuffers::Table *>(flatbuffer), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 76 | Flatbuffer<T>::MiniReflectTypeTable(), json_options); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 77 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 78 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 79 | std::string FlatbufferToJson(const reflection::Schema *schema, |
| 80 | const uint8_t *data, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 81 | JsonOptions json_options = {}); |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 82 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 83 | void FlatbufferToJson(FastStringBuilder *builder, |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 84 | const reflection::Schema *schema, const uint8_t *data, |
| 85 | JsonOptions json_options = {}); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 86 | |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 87 | // Writes a Flatbuffer to a file, or dies. |
| 88 | template <typename T> |
Ajay Penmatcha | 4c132ca | 2023-07-20 12:30:56 -0700 | [diff] [blame^] | 89 | inline void WriteFlatbufferToJson(std::string_view filename, const T *msg) { |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 90 | std::ofstream json_file(std::string(filename), std::ios::out); |
| 91 | CHECK(json_file) << ": Couldn't open " << filename; |
| 92 | json_file << FlatbufferToJson(msg); |
| 93 | json_file.close(); |
| 94 | } |
| 95 | |
Ajay Penmatcha | 4c132ca | 2023-07-20 12:30:56 -0700 | [diff] [blame^] | 96 | template <typename T> |
| 97 | inline void WriteFlatbufferToJson(std::string_view filename, |
| 98 | const Flatbuffer<T> &msg) { |
| 99 | WriteFlatbufferToJson(filename, &msg.message()); |
| 100 | } |
| 101 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 102 | // Writes a NonSizePrefixedFlatbuffer to a binary file, or dies. |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 103 | template <typename T> |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 104 | inline void WriteFlatbufferToFile(std::string_view filename, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 105 | const NonSizePrefixedFlatbuffer<T> &msg) { |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 106 | std::ofstream file(std::string(filename), |
| 107 | std::ios::out | std::ofstream::binary); |
| 108 | CHECK(file) << ": Couldn't open " << filename; |
| 109 | std::copy(msg.span().begin(), msg.span().end(), |
| 110 | std::ostreambuf_iterator<char>(file)); |
| 111 | } |
| 112 | |
Austin Schuh | 1674da2 | 2022-08-16 17:57:54 -0700 | [diff] [blame] | 113 | // Parses a file as JSON and returns the corresponding Flatbuffer. Dies if |
| 114 | // reading the file fails, returns an empty buffer if the contents are invalid. |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 115 | template <typename T> |
| 116 | inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer( |
| 117 | const std::string_view path) { |
Alexander Yee | e61cac3 | 2023-02-11 19:40:40 -0800 | [diff] [blame] | 118 | return FlatbufferDetachedBuffer<T>( |
| 119 | JsonToFlatbuffer<T>(util::ReadFileToStringOrDie(path))); |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 120 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 121 | |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 122 | // Parses a file as a binary flatbuffer or dies. |
| 123 | template <typename T> |
| 124 | inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) { |
Austin Schuh | bba1028 | 2021-03-20 22:03:28 -0700 | [diff] [blame] | 125 | const std::string data_string = util::ReadFileToStringOrDie(path); |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 126 | ResizeableBuffer data; |
Austin Schuh | bba1028 | 2021-03-20 22:03:28 -0700 | [diff] [blame] | 127 | data.resize(data_string.size()); |
| 128 | memcpy(data.data(), data_string.data(), data_string.size()); |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 129 | return FlatbufferVector<T>(std::move(data)); |
| 130 | } |
| 131 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 132 | } // namespace aos |
| 133 | |
| 134 | #endif // AOS_JSON_TO_FLATBUFFER_H_ |