blob: c1aa736739974719d2b2cdf151a719ecf7b1b1d9 [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 Schuhadd6eb32020-11-09 21:24:26 -080046// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070047::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
48 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070049 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070050
Austin Schuhe93d8642019-10-13 15:27:07 -070051// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
52template <typename T>
53inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070054 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080055 return TableFlatbufferToJson(
56 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
57 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070058}
59
60// Converts a flatbuffer::Table to JSON.
61template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070062typename std::enable_if<
63 std::is_base_of<flatbuffers::Table, T>::value,
64 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
65 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070066 return TableFlatbufferToJson(
67 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070068 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070069}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070070
Tyler Chatow5e369a42019-11-23 11:57:31 -080071std::string FlatbufferToJson(const reflection::Schema *const schema,
Ravago Jonescf453ab2020-05-06 21:14:53 -070072 const uint8_t *const data,
73 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070074
Tyler Chatowfcf16f42020-07-26 12:41:36 -070075void FlatbufferToJson(FastStringBuilder *builder,
76 const reflection::Schema *const schema,
77 const uint8_t *const data, JsonOptions json_options = {});
78
Brian Silverman8d278412020-06-23 16:37:17 -070079// Writes a Flatbuffer to a file, or dies.
80template <typename T>
81inline void WriteFlatbufferToJson(const std::string_view filename,
82 const Flatbuffer<T> &msg) {
83 std::ofstream json_file(std::string(filename), std::ios::out);
84 CHECK(json_file) << ": Couldn't open " << filename;
85 json_file << FlatbufferToJson(msg);
86 json_file.close();
87}
88
Austin Schuhadd6eb32020-11-09 21:24:26 -080089// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -070090template <typename T>
91inline void WriteFlatbufferToFile(const std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -080092 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -070093 std::ofstream file(std::string(filename),
94 std::ios::out | std::ofstream::binary);
95 CHECK(file) << ": Couldn't open " << filename;
96 std::copy(msg.span().begin(), msg.span().end(),
97 std::ostreambuf_iterator<char>(file));
98}
99
Brian Silverman8d278412020-06-23 16:37:17 -0700100// 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 Schuh313677c2020-08-01 14:45:30 -0700110// Parses a file as a binary flatbuffer or dies.
111template <typename T>
112inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
113 std::ifstream instream(std::string(path), std::ios::in | std::ios::binary);
Brian Silverman354697a2020-09-22 21:06:32 -0700114 ResizeableBuffer data;
115 std::istreambuf_iterator<char> it(instream);
116 while (it != std::istreambuf_iterator<char>()) {
117 data.push_back(*it);
118 ++it;
119 }
Austin Schuh313677c2020-08-01 14:45:30 -0700120 return FlatbufferVector<T>(std::move(data));
121}
122
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700123} // namespace aos
124
125#endif // AOS_JSON_TO_FLATBUFFER_H_