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