blob: 37560eccf59c1ab1352f845bdfe072db2165be18 [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,
Austin Schuhd3936202020-04-07 20:11:07 -070042 bool multi_line = false,
43 size_t max_vector_size = SIZE_MAX);
Austin Schuhe93d8642019-10-13 15:27:07 -070044
45::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
46 const ::flatbuffers::TypeTable *typetable,
Brian Silvermanc8a5b552020-04-28 16:43:59 -070047 bool multi_line,
48 size_t max_vector_size = SIZE_MAX);
Austin Schuhe93d8642019-10-13 15:27:07 -070049
50// Converts a DetachedBuffer holding a flatbuffer to JSON.
51inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
52 const flatbuffers::TypeTable *typetable,
Austin Schuhd3936202020-04-07 20:11:07 -070053 bool multi_line = false,
54 size_t max_vector_size = SIZE_MAX) {
55 return BufferFlatbufferToJson(buffer.data(), typetable, multi_line,
56 max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070057}
58
59// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
60template <typename T>
61inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Austin Schuhd3936202020-04-07 20:11:07 -070062 bool multi_line = false,
63 size_t max_vector_size = SIZE_MAX) {
64 return BufferFlatbufferToJson(flatbuffer.data(),
65 Flatbuffer<T>::MiniReflectTypeTable(),
66 multi_line, max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070067}
68
69// Converts a flatbuffer::Table to JSON.
70template <typename T>
Austin Schuhd3936202020-04-07 20:11:07 -070071typename std::enable_if<std::is_base_of<flatbuffers::Table, T>::value,
72 std::string>::
73 type inline FlatbufferToJson(const T *flatbuffer, bool multi_line = false,
74 size_t max_vector_size = SIZE_MAX) {
Austin Schuhe93d8642019-10-13 15:27:07 -070075 return TableFlatbufferToJson(
76 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Austin Schuhd3936202020-04-07 20:11:07 -070077 Flatbuffer<T>::MiniReflectTypeTable(), multi_line, max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070078}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070079
Tyler Chatow5e369a42019-11-23 11:57:31 -080080std::string FlatbufferToJson(const reflection::Schema *const schema,
Austin Schuhd3936202020-04-07 20:11:07 -070081 const uint8_t *const data,
82 size_t max_vector_size = SIZE_MAX);
Tyler Chatow5e369a42019-11-23 11:57:31 -080083
Austin Schuh3e95e5d2019-09-20 00:08:54 -070084} // namespace aos
85
86#endif // AOS_JSON_TO_FLATBUFFER_H_