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> |
| 5 | #include <string> |
| 6 | |
Austin Schuh | d339a9b | 2019-10-05 21:33:32 -0700 | [diff] [blame] | 7 | #include "absl/strings/string_view.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 8 | #include "aos/flatbuffers.h" |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 9 | #include "flatbuffers/flatbuffers.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // Parses the flatbuffer into the vector, or returns an empty vector. |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 14 | flatbuffers::DetachedBuffer JsonToFlatbuffer( |
Austin Schuh | d339a9b | 2019-10-05 21:33:32 -0700 | [diff] [blame] | 15 | const absl::string_view data, const flatbuffers::TypeTable *typetable); |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 16 | |
| 17 | // Converts a flatbuffer into a Json string. |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 18 | // 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] | 19 | // The methods below are generally more useful than BufferFlatbufferToJson and |
| 20 | // TableFlatbufferToJson. |
| 21 | ::std::string BufferFlatbufferToJson(const uint8_t *buffer, |
| 22 | const flatbuffers::TypeTable *typetable, |
| 23 | bool multi_line = false); |
| 24 | |
| 25 | ::std::string TableFlatbufferToJson(const flatbuffers::Table *t, |
| 26 | const ::flatbuffers::TypeTable *typetable, |
| 27 | bool multi_line); |
| 28 | |
| 29 | // Converts a DetachedBuffer holding a flatbuffer to JSON. |
| 30 | inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer, |
| 31 | const flatbuffers::TypeTable *typetable, |
| 32 | bool multi_line = false) { |
| 33 | return BufferFlatbufferToJson(buffer.data(), typetable, multi_line); |
| 34 | } |
| 35 | |
| 36 | // Converts a Flatbuffer<T> holding a flatbuffer to JSON. |
| 37 | template <typename T> |
| 38 | inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer, |
| 39 | bool multi_line = false) { |
| 40 | return BufferFlatbufferToJson( |
| 41 | flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), multi_line); |
| 42 | } |
| 43 | |
| 44 | // Converts a flatbuffer::Table to JSON. |
| 45 | template <typename T> |
| 46 | typename std::enable_if< |
| 47 | std::is_base_of<flatbuffers::Table, T>::value, |
| 48 | std::string>::type inline FlatbufferToJson(const T *flatbuffer, |
| 49 | bool multi_line = false) { |
| 50 | return TableFlatbufferToJson( |
| 51 | reinterpret_cast<const flatbuffers::Table *>(flatbuffer), |
| 52 | Flatbuffer<T>::MiniReflectTypeTable(), multi_line); |
| 53 | } |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 54 | |
Austin Schuh | 3e95e5d | 2019-09-20 00:08:54 -0700 | [diff] [blame] | 55 | } // namespace aos |
| 56 | |
| 57 | #endif // AOS_JSON_TO_FLATBUFFER_H_ |