blob: 052fa9ca078d806a90654142fc1d0b0eb10380b0 [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"
Brian Silvermancf4fb662021-02-10 17:54:53 -080010#include "aos/flatbuffer_utils.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070011#include "aos/flatbuffers.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070012#include "flatbuffers/flatbuffers.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080013#include "flatbuffers/reflection.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070014
15namespace aos {
16
Austin Schuh53b1a6f2020-01-10 19:31:28 -080017// Parses the flatbuffer into the buffer, or returns an empty buffer.
Brian Silvermancf4fb662021-02-10 17:54:53 -080018flatbuffers::DetachedBuffer JsonToFlatbuffer(std::string_view data,
19 FlatbufferType type);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070020
Austin Schuh53b1a6f2020-01-10 19:31:28 -080021// Parses the flatbuffer into the builder, and returns the offset.
22flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
Brian Silvermancf4fb662021-02-10 17:54:53 -080023 std::string_view data, FlatbufferType type,
Austin Schuh53b1a6f2020-01-10 19:31:28 -080024 flatbuffers::FlatBufferBuilder *fbb);
25
26// Typed versions of the above methods.
27template <typename T>
28inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
29 const std::string_view data) {
Brian Silvermancf4fb662021-02-10 17:54:53 -080030 return JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()));
Austin Schuh53b1a6f2020-01-10 19:31:28 -080031}
32template <typename T>
33inline flatbuffers::Offset<T> JsonToFlatbuffer(
34 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
35 return flatbuffers::Offset<T>(
Brian Silvermancf4fb662021-02-10 17:54:53 -080036 JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()), fbb).o);
Austin Schuh53b1a6f2020-01-10 19:31:28 -080037}
38
Ravago Jonescf453ab2020-05-06 21:14:53 -070039struct JsonOptions {
40 // controls if the Json is written ouut on multiple lines or one.
41 bool multi_line = false;
42 // the contents of vectors longer than max_vector_size will be skipped.
43 size_t max_vector_size = SIZE_MAX;
44};
45
Austin Schuh3e95e5d2019-09-20 00:08:54 -070046// Converts a flatbuffer into a Json string.
Austin Schuhadd6eb32020-11-09 21:24:26 -080047// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070048::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
49 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070050 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070051
Austin Schuhe93d8642019-10-13 15:27:07 -070052// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
53template <typename T>
54inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070055 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080056 return TableFlatbufferToJson(
57 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
58 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070059}
60
61// Converts a flatbuffer::Table to JSON.
62template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070063typename std::enable_if<
64 std::is_base_of<flatbuffers::Table, T>::value,
65 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
66 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070067 return TableFlatbufferToJson(
68 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070069 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070070}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070071
Brian Silvermancf4fb662021-02-10 17:54:53 -080072std::string FlatbufferToJson(const reflection::Schema *schema,
73 const uint8_t *data,
Ravago Jonescf453ab2020-05-06 21:14:53 -070074 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070075
Tyler Chatowfcf16f42020-07-26 12:41:36 -070076void FlatbufferToJson(FastStringBuilder *builder,
Brian Silvermancf4fb662021-02-10 17:54:53 -080077 const reflection::Schema *schema, const uint8_t *data,
78 JsonOptions json_options = {});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070079
Brian Silverman8d278412020-06-23 16:37:17 -070080// Writes a Flatbuffer to a file, or dies.
81template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080082inline void WriteFlatbufferToJson(std::string_view filename,
Brian Silverman8d278412020-06-23 16:37:17 -070083 const Flatbuffer<T> &msg) {
84 std::ofstream json_file(std::string(filename), std::ios::out);
85 CHECK(json_file) << ": Couldn't open " << filename;
86 json_file << FlatbufferToJson(msg);
87 json_file.close();
88}
89
Austin Schuhadd6eb32020-11-09 21:24:26 -080090// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -070091template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080092inline void WriteFlatbufferToFile(std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -080093 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -070094 std::ofstream file(std::string(filename),
95 std::ios::out | std::ofstream::binary);
96 CHECK(file) << ": Couldn't open " << filename;
97 std::copy(msg.span().begin(), msg.span().end(),
98 std::ostreambuf_iterator<char>(file));
99}
100
Brian Silverman8d278412020-06-23 16:37:17 -0700101// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
102template <typename T>
103inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
104 const std::string_view path) {
105 std::ifstream t{std::string(path)};
106 std::istream_iterator<char> start(t), end;
107 std::string result(start, end);
108 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
109}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800110
Austin Schuh313677c2020-08-01 14:45:30 -0700111// Parses a file as a binary flatbuffer or dies.
112template <typename T>
113inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
114 std::ifstream instream(std::string(path), std::ios::in | std::ios::binary);
Brian Silverman354697a2020-09-22 21:06:32 -0700115 ResizeableBuffer data;
116 std::istreambuf_iterator<char> it(instream);
117 while (it != std::istreambuf_iterator<char>()) {
118 data.push_back(*it);
119 ++it;
120 }
Austin Schuh313677c2020-08-01 14:45:30 -0700121 return FlatbufferVector<T>(std::move(data));
122}
123
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700124} // namespace aos
125
126#endif // AOS_JSON_TO_FLATBUFFER_H_