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