blob: f83bab906ef23702d5db9281768fc5577022c1c1 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "flatbuffers/flatbuffers.h"
10#include "flatbuffers/reflection.h"
11
Tyler Chatowfcf16f42020-07-26 12:41:36 -070012#include "aos/fast_string_builder.h"
Brian Silvermancf4fb662021-02-10 17:54:53 -080013#include "aos/flatbuffer_utils.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070014#include "aos/flatbuffers.h"
James Kuszmaulf5eb4682023-09-22 17:16:59 -070015#include "aos/flatbuffers/builder.h"
Austin Schuhbba10282021-03-20 22:03:28 -070016#include "aos/util/file.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070017
18namespace aos {
19
Austin Schuh53b1a6f2020-01-10 19:31:28 -080020// Parses the flatbuffer into the buffer, or returns an empty buffer.
Brian Silvermancf4fb662021-02-10 17:54:53 -080021flatbuffers::DetachedBuffer JsonToFlatbuffer(std::string_view data,
22 FlatbufferType type);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070023
Austin Schuh53b1a6f2020-01-10 19:31:28 -080024// Parses the flatbuffer into the builder, and returns the offset.
25flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
Brian Silvermancf4fb662021-02-10 17:54:53 -080026 std::string_view data, FlatbufferType type,
Austin Schuh53b1a6f2020-01-10 19:31:28 -080027 flatbuffers::FlatBufferBuilder *fbb);
28
29// Typed versions of the above methods.
30template <typename T>
31inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
32 const std::string_view data) {
Brian Silvermancf4fb662021-02-10 17:54:53 -080033 return JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()));
Austin Schuh53b1a6f2020-01-10 19:31:28 -080034}
35template <typename T>
36inline flatbuffers::Offset<T> JsonToFlatbuffer(
37 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
38 return flatbuffers::Offset<T>(
Brian Silvermancf4fb662021-02-10 17:54:53 -080039 JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()), fbb).o);
Austin Schuh53b1a6f2020-01-10 19:31:28 -080040}
41
James Kuszmaulf5eb4682023-09-22 17:16:59 -070042template <typename T>
43inline fbs::Builder<T> JsonToStaticFlatbuffer(const std::string_view data) {
44 const aos::FlatbufferDetachedBuffer<typename T::Flatbuffer> fbs =
45 JsonToFlatbuffer<typename T::Flatbuffer>(data);
Austin Schuh02e0d772024-05-30 16:41:06 -070046 fbs::Builder<T> builder(std::make_unique<aos::fbs::AlignedVectorAllocator>());
James Kuszmaulf5eb4682023-09-22 17:16:59 -070047 CHECK(builder.get()->FromFlatbuffer(&fbs.message()));
48 return builder;
49}
50
Ravago Jonescf453ab2020-05-06 21:14:53 -070051struct JsonOptions {
Dan Ford44e02512022-06-07 23:45:14 -070052 // controls if the Json is written out on multiple lines or one.
Ravago Jonescf453ab2020-05-06 21:14:53 -070053 bool multi_line = false;
54 // the contents of vectors longer than max_vector_size will be skipped.
55 size_t max_vector_size = SIZE_MAX;
Dan Ford44e02512022-06-07 23:45:14 -070056 // more extensive version of multi_line that prints every single field on its
57 // own line.
58 bool max_multi_line = false;
Brian J Griglak043e0e22022-08-18 12:51:18 -060059 // Will integers be printed in hexadecimal form instead of decimal.
60 bool use_hex = false;
Ravago Jonescf453ab2020-05-06 21:14:53 -070061};
62
Austin Schuh3e95e5d2019-09-20 00:08:54 -070063// Converts a flatbuffer into a Json string.
Austin Schuhadd6eb32020-11-09 21:24:26 -080064// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070065::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
James Kuszmaulf5eb4682023-09-22 17:16:59 -070066 const flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070067 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070068
Austin Schuhe93d8642019-10-13 15:27:07 -070069// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
70template <typename T>
71inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070072 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080073 return TableFlatbufferToJson(
74 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
75 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070076}
77
James Kuszmaul9052bdb2023-12-20 11:51:46 -080078template <typename T, typename Enable = T::Flatbuffer>
79inline ::std::string FlatbufferToJson(const fbs::Builder<T> &flatbuffer,
80 JsonOptions json_options = {}) {
81 return FlatbufferToJson(flatbuffer.AsFlatbufferSpan(), json_options);
82}
83
Austin Schuhe93d8642019-10-13 15:27:07 -070084// Converts a flatbuffer::Table to JSON.
85template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070086typename std::enable_if<
87 std::is_base_of<flatbuffers::Table, T>::value,
88 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
89 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070090 return TableFlatbufferToJson(
91 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070092 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070093}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070094
Brian Silvermancf4fb662021-02-10 17:54:53 -080095std::string FlatbufferToJson(const reflection::Schema *schema,
96 const uint8_t *data,
Ravago Jonescf453ab2020-05-06 21:14:53 -070097 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070098
Tyler Chatowfcf16f42020-07-26 12:41:36 -070099void FlatbufferToJson(FastStringBuilder *builder,
Brian Silvermancf4fb662021-02-10 17:54:53 -0800100 const reflection::Schema *schema, const uint8_t *data,
101 JsonOptions json_options = {});
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700102
Brian Silverman8d278412020-06-23 16:37:17 -0700103// Writes a Flatbuffer to a file, or dies.
104template <typename T>
Ajay Penmatcha4c132ca2023-07-20 12:30:56 -0700105inline void WriteFlatbufferToJson(std::string_view filename, const T *msg) {
Brian Silverman8d278412020-06-23 16:37:17 -0700106 std::ofstream json_file(std::string(filename), std::ios::out);
107 CHECK(json_file) << ": Couldn't open " << filename;
108 json_file << FlatbufferToJson(msg);
109 json_file.close();
110}
111
Ajay Penmatcha4c132ca2023-07-20 12:30:56 -0700112template <typename T>
113inline void WriteFlatbufferToJson(std::string_view filename,
114 const Flatbuffer<T> &msg) {
115 WriteFlatbufferToJson(filename, &msg.message());
116}
117
Austin Schuhadd6eb32020-11-09 21:24:26 -0800118// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -0700119template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -0800120inline void WriteFlatbufferToFile(std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -0800121 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -0700122 std::ofstream file(std::string(filename),
123 std::ios::out | std::ofstream::binary);
124 CHECK(file) << ": Couldn't open " << filename;
125 std::copy(msg.span().begin(), msg.span().end(),
126 std::ostreambuf_iterator<char>(file));
127}
128
Austin Schuh1674da22022-08-16 17:57:54 -0700129// Parses a file as JSON and returns the corresponding Flatbuffer. Dies if
130// reading the file fails, returns an empty buffer if the contents are invalid.
Brian Silverman8d278412020-06-23 16:37:17 -0700131template <typename T>
132inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
133 const std::string_view path) {
Alexander Yeee61cac32023-02-11 19:40:40 -0800134 return FlatbufferDetachedBuffer<T>(
135 JsonToFlatbuffer<T>(util::ReadFileToStringOrDie(path)));
Brian Silverman8d278412020-06-23 16:37:17 -0700136}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800137
Austin Schuh313677c2020-08-01 14:45:30 -0700138// Parses a file as a binary flatbuffer or dies.
139template <typename T>
140inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
Austin Schuhbba10282021-03-20 22:03:28 -0700141 const std::string data_string = util::ReadFileToStringOrDie(path);
Brian Silverman354697a2020-09-22 21:06:32 -0700142 ResizeableBuffer data;
Austin Schuhbba10282021-03-20 22:03:28 -0700143 data.resize(data_string.size());
144 memcpy(data.data(), data_string.data(), data_string.size());
Austin Schuh313677c2020-08-01 14:45:30 -0700145 return FlatbufferVector<T>(std::move(data));
146}
147
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700148} // namespace aos
149
150#endif // AOS_JSON_TO_FLATBUFFER_H_