blob: c061c69544e0b9ed214b826f570a2f339838da71 [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>
James Kuszmaul3ae42262019-11-08 12:33:41 -08006#include <string_view>
Austin Schuh3e95e5d2019-09-20 00:08:54 -07007
Austin Schuhe93d8642019-10-13 15:27:07 -07008#include "aos/flatbuffers.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -07009#include "flatbuffers/flatbuffers.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080010#include "flatbuffers/reflection.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070011
12namespace aos {
13
Austin Schuh53b1a6f2020-01-10 19:31:28 -080014// Parses the flatbuffer into the buffer, or returns an empty buffer.
Austin Schuhe93d8642019-10-13 15:27:07 -070015flatbuffers::DetachedBuffer JsonToFlatbuffer(
Tyler Chatow5e369a42019-11-23 11:57:31 -080016 const std::string_view data, const flatbuffers::TypeTable *typetable);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070017
Austin Schuh53b1a6f2020-01-10 19:31:28 -080018// Parses the flatbuffer into the builder, and returns the offset.
19flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
20 const std::string_view data, const flatbuffers::TypeTable *typetable,
21 flatbuffers::FlatBufferBuilder *fbb);
22
23// Typed versions of the above methods.
24template <typename T>
25inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
26 const std::string_view data) {
27 return JsonToFlatbuffer(data, T::MiniReflectTypeTable());
28}
29template <typename T>
30inline flatbuffers::Offset<T> JsonToFlatbuffer(
31 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
32 return flatbuffers::Offset<T>(
33 JsonToFlatbuffer(data, T::MiniReflectTypeTable(), fbb).o);
34}
35
Austin Schuh3e95e5d2019-09-20 00:08:54 -070036// Converts a flatbuffer into a Json string.
Austin Schuh3e95e5d2019-09-20 00:08:54 -070037// multi_line controls if the Json is written out on multiple lines or one.
Austin Schuhe93d8642019-10-13 15:27:07 -070038// The methods below are generally more useful than BufferFlatbufferToJson and
39// TableFlatbufferToJson.
40::std::string BufferFlatbufferToJson(const uint8_t *buffer,
41 const flatbuffers::TypeTable *typetable,
42 bool multi_line = false);
43
44::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
45 const ::flatbuffers::TypeTable *typetable,
46 bool multi_line);
47
48// Converts a DetachedBuffer holding a flatbuffer to JSON.
49inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
50 const flatbuffers::TypeTable *typetable,
51 bool multi_line = false) {
52 return BufferFlatbufferToJson(buffer.data(), typetable, multi_line);
53}
54
55// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
56template <typename T>
57inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
58 bool multi_line = false) {
59 return BufferFlatbufferToJson(
60 flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), multi_line);
61}
62
63// Converts a flatbuffer::Table to JSON.
64template <typename T>
65typename std::enable_if<
66 std::is_base_of<flatbuffers::Table, T>::value,
67 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
68 bool multi_line = false) {
69 return TableFlatbufferToJson(
70 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
71 Flatbuffer<T>::MiniReflectTypeTable(), multi_line);
72}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070073
Tyler Chatow5e369a42019-11-23 11:57:31 -080074std::string FlatbufferToJson(const reflection::Schema *const schema,
75 const uint8_t *const data);
76
Austin Schuh3e95e5d2019-09-20 00:08:54 -070077} // namespace aos
78
79#endif // AOS_JSON_TO_FLATBUFFER_H_