blob: 381621e47b5c799a45f62f9ac95f852bd368b5a8 [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
Austin Schuh313677c2020-08-01 14:45:30 -0700100// Writes a Flatbuffer to a binary file, or dies.
101template <typename T>
102inline void WriteFlatbufferToFile(const std::string_view filename,
103 const Flatbuffer<T> &msg) {
104 std::ofstream file(std::string(filename),
105 std::ios::out | std::ofstream::binary);
106 CHECK(file) << ": Couldn't open " << filename;
107 std::copy(msg.span().begin(), msg.span().end(),
108 std::ostreambuf_iterator<char>(file));
109}
110
Brian Silverman8d278412020-06-23 16:37:17 -0700111// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
112template <typename T>
113inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
114 const std::string_view path) {
115 std::ifstream t{std::string(path)};
116 std::istream_iterator<char> start(t), end;
117 std::string result(start, end);
118 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
119}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800120
Austin Schuh313677c2020-08-01 14:45:30 -0700121// Parses a file as a binary flatbuffer or dies.
122template <typename T>
123inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
124 std::ifstream instream(std::string(path), std::ios::in | std::ios::binary);
125 std::vector<uint8_t> data((std::istreambuf_iterator<char>(instream)),
126 std::istreambuf_iterator<char>());
127 return FlatbufferVector<T>(std::move(data));
128}
129
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700130} // namespace aos
131
132#endif // AOS_JSON_TO_FLATBUFFER_H_