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