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 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 37 | // Converts a flatbuffer into a Json string. |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 38 | // multi_line controls if the Json is written out on multiple lines or one. |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 39 | // The methods below are generally more useful than BufferFlatbufferToJson and |
| 40 | // TableFlatbufferToJson. |
| 41 | ::std::string BufferFlatbufferToJson(const uint8_t *buffer, |
| 42 | const flatbuffers::TypeTable *typetable, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 43 | bool multi_line = false, |
| 44 | size_t max_vector_size = SIZE_MAX); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 45 | |
| 46 | ::std::string TableFlatbufferToJson(const flatbuffers::Table *t, |
| 47 | const ::flatbuffers::TypeTable *typetable, |
Brian Silverman | c8a5b55 | 2020-04-28 16:43:59 -0700 | [diff] [blame] | 48 | bool multi_line, |
| 49 | size_t max_vector_size = SIZE_MAX); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 50 | |
| 51 | // Converts a DetachedBuffer holding a flatbuffer to JSON. |
| 52 | inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer, |
| 53 | const flatbuffers::TypeTable *typetable, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 54 | bool multi_line = false, |
| 55 | size_t max_vector_size = SIZE_MAX) { |
| 56 | return BufferFlatbufferToJson(buffer.data(), typetable, multi_line, |
| 57 | max_vector_size); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Converts a Flatbuffer<T> holding a flatbuffer to JSON. |
| 61 | template <typename T> |
| 62 | inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 63 | bool multi_line = false, |
| 64 | size_t max_vector_size = SIZE_MAX) { |
| 65 | return BufferFlatbufferToJson(flatbuffer.data(), |
| 66 | Flatbuffer<T>::MiniReflectTypeTable(), |
| 67 | multi_line, max_vector_size); |
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> |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 72 | typename std::enable_if<std::is_base_of<flatbuffers::Table, T>::value, |
| 73 | std::string>:: |
| 74 | type inline FlatbufferToJson(const T *flatbuffer, bool multi_line = false, |
| 75 | size_t max_vector_size = SIZE_MAX) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 76 | return TableFlatbufferToJson( |
| 77 | reinterpret_cast<const flatbuffers::Table *>(flatbuffer), |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 78 | Flatbuffer<T>::MiniReflectTypeTable(), multi_line, max_vector_size); |
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, |
Brian Silverman | 8d27841 | 2020-06-23 16:37:17 -0700 | [diff] [blame] | 82 | const uint8_t *const data, bool multi_line = false, |
| 83 | size_t max_vector_size = SIZE_MAX); |
| 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_ |