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