blob: 9ba50f89711d3d5d620339fbef7ccfaa53be05c1 [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>
Brian Silverman8d278412020-06-23 16:37:17 -07005#include <fstream>
Austin Schuh3e95e5d2019-09-20 00:08:54 -07006#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08007#include <string_view>
Austin Schuh3e95e5d2019-09-20 00:08:54 -07008
Tyler Chatowfcf16f42020-07-26 12:41:36 -07009#include "aos/fast_string_builder.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070010#include "aos/flatbuffers.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070011#include "flatbuffers/flatbuffers.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080012#include "flatbuffers/reflection.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070013
14namespace aos {
15
Austin Schuh53b1a6f2020-01-10 19:31:28 -080016// Parses the flatbuffer into the buffer, or returns an empty buffer.
Austin Schuhe93d8642019-10-13 15:27:07 -070017flatbuffers::DetachedBuffer JsonToFlatbuffer(
Tyler Chatow5e369a42019-11-23 11:57:31 -080018 const std::string_view data, const flatbuffers::TypeTable *typetable);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070019
Austin Schuh53b1a6f2020-01-10 19:31:28 -080020// Parses the flatbuffer into the builder, and returns the offset.
21flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
22 const std::string_view data, const flatbuffers::TypeTable *typetable,
23 flatbuffers::FlatBufferBuilder *fbb);
24
25// Typed versions of the above methods.
26template <typename T>
27inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
28 const std::string_view data) {
29 return JsonToFlatbuffer(data, T::MiniReflectTypeTable());
30}
31template <typename T>
32inline flatbuffers::Offset<T> JsonToFlatbuffer(
33 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
34 return flatbuffers::Offset<T>(
35 JsonToFlatbuffer(data, T::MiniReflectTypeTable(), fbb).o);
36}
37
Ravago Jonescf453ab2020-05-06 21:14:53 -070038struct JsonOptions {
39 // controls if the Json is written ouut on multiple lines or one.
40 bool multi_line = false;
41 // the contents of vectors longer than max_vector_size will be skipped.
42 size_t max_vector_size = SIZE_MAX;
43};
44
Austin Schuh3e95e5d2019-09-20 00:08:54 -070045// Converts a flatbuffer into a Json string.
Austin Schuhe93d8642019-10-13 15:27:07 -070046// The methods below are generally more useful than BufferFlatbufferToJson and
47// TableFlatbufferToJson.
48::std::string BufferFlatbufferToJson(const uint8_t *buffer,
49 const flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070050 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070051
52::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
53 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070054 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070055
56// Converts a DetachedBuffer holding a flatbuffer to JSON.
57inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
58 const flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070059 JsonOptions json_options = {}) {
60 return BufferFlatbufferToJson(buffer.data(), typetable, json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070061}
62
63// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
64template <typename T>
65inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070066 JsonOptions json_options = {}) {
67 return BufferFlatbufferToJson(
68 flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070069}
70
71// Converts a flatbuffer::Table to JSON.
72template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070073typename std::enable_if<
74 std::is_base_of<flatbuffers::Table, T>::value,
75 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
76 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070077 return TableFlatbufferToJson(
78 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070079 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070080}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070081
Tyler Chatow5e369a42019-11-23 11:57:31 -080082std::string FlatbufferToJson(const reflection::Schema *const schema,
Ravago Jonescf453ab2020-05-06 21:14:53 -070083 const uint8_t *const data,
84 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070085
Tyler Chatowfcf16f42020-07-26 12:41:36 -070086void FlatbufferToJson(FastStringBuilder *builder,
87 const reflection::Schema *const schema,
88 const uint8_t *const data, JsonOptions json_options = {});
89
Brian Silverman8d278412020-06-23 16:37:17 -070090// Writes a Flatbuffer to a file, or dies.
91template <typename T>
92inline void WriteFlatbufferToJson(const std::string_view filename,
93 const Flatbuffer<T> &msg) {
94 std::ofstream json_file(std::string(filename), std::ios::out);
95 CHECK(json_file) << ": Couldn't open " << filename;
96 json_file << FlatbufferToJson(msg);
97 json_file.close();
98}
99
100// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
101template <typename T>
102inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
103 const std::string_view path) {
104 std::ifstream t{std::string(path)};
105 std::istream_iterator<char> start(t), end;
106 std::string result(start, end);
107 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
108}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800109
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700110} // namespace aos
111
112#endif // AOS_JSON_TO_FLATBUFFER_H_