blob: 183e6b2c7187fa2a1bb0a79a3c07e5b0469db210 [file] [log] [blame]
Austin Schuh3e95e5d2019-09-20 00:08:54 -07001#ifndef AOS_JSON_TO_FLATBUFFER_H_
2#define AOS_JSON_TO_FLATBUFFER_H_
3
4#include <cstddef>
5#include <string>
6
Austin Schuhd339a9b2019-10-05 21:33:32 -07007#include "absl/strings/string_view.h"
Austin Schuhe93d8642019-10-13 15:27:07 -07008#include "aos/flatbuffers.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -07009#include "flatbuffers/flatbuffers.h"
10
11namespace aos {
12
13// Parses the flatbuffer into the vector, or returns an empty vector.
Austin Schuhe93d8642019-10-13 15:27:07 -070014flatbuffers::DetachedBuffer JsonToFlatbuffer(
Austin Schuhd339a9b2019-10-05 21:33:32 -070015 const absl::string_view data, const flatbuffers::TypeTable *typetable);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070016
17// Converts a flatbuffer into a Json string.
Austin Schuh3e95e5d2019-09-20 00:08:54 -070018// multi_line controls if the Json is written out on multiple lines or one.
Austin Schuhe93d8642019-10-13 15:27:07 -070019// 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.
30inline ::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.
37template <typename T>
38inline ::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.
45template <typename T>
46typename 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 Schuh3e95e5d2019-09-20 00:08:54 -070054
Austin Schuh3e95e5d2019-09-20 00:08:54 -070055} // namespace aos
56
57#endif // AOS_JSON_TO_FLATBUFFER_H_