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; |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 50 | // Converts a flatbuffer into a Json string. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 51 | // The methods below are generally more useful than TableFlatbufferToJson. |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 52 | ::std::string TableFlatbufferToJson(const flatbuffers::Table *t, |
| 53 | const ::flatbuffers::TypeTable *typetable, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 54 | JsonOptions json_options = {}); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 55 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 56 | // Converts a Flatbuffer<T> holding a flatbuffer to JSON. |
| 57 | template <typename T> |
| 58 | inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 59 | JsonOptions json_options = {}) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 60 | return TableFlatbufferToJson( |
| 61 | reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()), |
| 62 | Flatbuffer<T>::MiniReflectTypeTable(), json_options); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Converts a flatbuffer::Table to JSON. |
| 66 | template <typename T> |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 67 | typename std::enable_if< |
| 68 | std::is_base_of<flatbuffers::Table, T>::value, |
| 69 | std::string>::type inline FlatbufferToJson(const T *flatbuffer, |
| 70 | JsonOptions json_options = {}) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 71 | return TableFlatbufferToJson( |
| 72 | reinterpret_cast<const flatbuffers::Table *>(flatbuffer), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 73 | Flatbuffer<T>::MiniReflectTypeTable(), json_options); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 74 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 75 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 76 | std::string FlatbufferToJson(const reflection::Schema *schema, |
| 77 | const uint8_t *data, |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 78 | JsonOptions json_options = {}); |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 79 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 80 | void FlatbufferToJson(FastStringBuilder *builder, |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 81 | const reflection::Schema *schema, const uint8_t *data, |
| 82 | JsonOptions json_options = {}); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 83 | |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 84 | // Writes a Flatbuffer to a file, or dies. |
| 85 | template <typename T> |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 86 | inline void WriteFlatbufferToJson(std::string_view filename, |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 87 | const Flatbuffer<T> &msg) { |
| 88 | std::ofstream json_file(std::string(filename), std::ios::out); |
| 89 | CHECK(json_file) << ": Couldn't open " << filename; |
| 90 | json_file << FlatbufferToJson(msg); |
| 91 | json_file.close(); |
| 92 | } |
| 93 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 94 | // Writes a NonSizePrefixedFlatbuffer to a binary file, or dies. |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 95 | template <typename T> |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 96 | inline void WriteFlatbufferToFile(std::string_view filename, |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 97 | const NonSizePrefixedFlatbuffer<T> &msg) { |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 98 | std::ofstream file(std::string(filename), |
| 99 | std::ios::out | std::ofstream::binary); |
| 100 | CHECK(file) << ": Couldn't open " << filename; |
| 101 | std::copy(msg.span().begin(), msg.span().end(), |
| 102 | std::ostreambuf_iterator<char>(file)); |
| 103 | } |
| 104 | |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 105 | // Parses a file as JSON and returns the corresponding Flatbuffer, or dies. |
| 106 | template <typename T> |
| 107 | inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer( |
| 108 | const std::string_view path) { |
| 109 | std::ifstream t{std::string(path)}; |
| 110 | std::istream_iterator<char> start(t), end; |
| 111 | std::string result(start, end); |
| 112 | return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result)); |
| 113 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 115 | // Parses a file as a binary flatbuffer or dies. |
| 116 | template <typename T> |
| 117 | inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) { |
Austin Schuh | bba1028 | 2021-03-20 22:03:28 -0700 | [diff] [blame] | 118 | const std::string data_string = util::ReadFileToStringOrDie(path); |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 119 | ResizeableBuffer data; |
Austin Schuh | bba1028 | 2021-03-20 22:03:28 -0700 | [diff] [blame] | 120 | data.resize(data_string.size()); |
| 121 | memcpy(data.data(), data_string.data(), data_string.size()); |
Austin Schuh | 313677c | 2020-08-01 14:45:30 -0700 | [diff] [blame] | 122 | return FlatbufferVector<T>(std::move(data)); |
| 123 | } |
| 124 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 125 | } // namespace aos |
| 126 | |
| 127 | #endif // AOS_JSON_TO_FLATBUFFER_H_ |