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