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