blob: 45920a8c36e2148eb16d10cdcc617231606a4ac2 [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 Schuhbba10282021-03-20 22:03:28 -070012#include "aos/util/file.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070013#include "flatbuffers/flatbuffers.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080014#include "flatbuffers/reflection.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070015
16namespace aos {
17
Austin Schuh53b1a6f2020-01-10 19:31:28 -080018// Parses the flatbuffer into the buffer, or returns an empty buffer.
Brian Silvermancf4fb662021-02-10 17:54:53 -080019flatbuffers::DetachedBuffer JsonToFlatbuffer(std::string_view data,
20 FlatbufferType type);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070021
Austin Schuh53b1a6f2020-01-10 19:31:28 -080022// Parses the flatbuffer into the builder, and returns the offset.
23flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
Brian Silvermancf4fb662021-02-10 17:54:53 -080024 std::string_view data, FlatbufferType type,
Austin Schuh53b1a6f2020-01-10 19:31:28 -080025 flatbuffers::FlatBufferBuilder *fbb);
26
27// Typed versions of the above methods.
28template <typename T>
29inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
30 const std::string_view data) {
Brian Silvermancf4fb662021-02-10 17:54:53 -080031 return JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()));
Austin Schuh53b1a6f2020-01-10 19:31:28 -080032}
33template <typename T>
34inline flatbuffers::Offset<T> JsonToFlatbuffer(
35 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
36 return flatbuffers::Offset<T>(
Brian Silvermancf4fb662021-02-10 17:54:53 -080037 JsonToFlatbuffer(data, FlatbufferType(T::MiniReflectTypeTable()), fbb).o);
Austin Schuh53b1a6f2020-01-10 19:31:28 -080038}
39
Ravago Jonescf453ab2020-05-06 21:14:53 -070040struct JsonOptions {
Dan Ford44e02512022-06-07 23:45:14 -070041 // controls if the Json is written out on multiple lines or one.
Ravago Jonescf453ab2020-05-06 21:14:53 -070042 bool multi_line = false;
43 // the contents of vectors longer than max_vector_size will be skipped.
44 size_t max_vector_size = SIZE_MAX;
Dan Ford44e02512022-06-07 23:45:14 -070045 // more extensive version of multi_line that prints every single field on its
46 // own line.
47 bool max_multi_line = false;
Ravago Jonescf453ab2020-05-06 21:14:53 -070048};
49
Austin Schuh3e95e5d2019-09-20 00:08:54 -070050// Converts a flatbuffer into a Json string.
Austin Schuhadd6eb32020-11-09 21:24:26 -080051// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070052::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
Austin Schuhe93d8642019-10-13 15:27:07 -070056// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
57template <typename T>
58inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070059 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080060 return TableFlatbufferToJson(
61 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
62 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070063}
64
65// Converts a flatbuffer::Table to JSON.
66template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070067typename std::enable_if<
68 std::is_base_of<flatbuffers::Table, T>::value,
69 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
70 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070071 return TableFlatbufferToJson(
72 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070073 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070074}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070075
Brian Silvermancf4fb662021-02-10 17:54:53 -080076std::string FlatbufferToJson(const reflection::Schema *schema,
77 const uint8_t *data,
Ravago Jonescf453ab2020-05-06 21:14:53 -070078 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070079
Tyler Chatowfcf16f42020-07-26 12:41:36 -070080void FlatbufferToJson(FastStringBuilder *builder,
Brian Silvermancf4fb662021-02-10 17:54:53 -080081 const reflection::Schema *schema, const uint8_t *data,
82 JsonOptions json_options = {});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070083
Brian Silverman8d278412020-06-23 16:37:17 -070084// Writes a Flatbuffer to a file, or dies.
85template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080086inline void WriteFlatbufferToJson(std::string_view filename,
Brian Silverman8d278412020-06-23 16:37:17 -070087 const Flatbuffer<T> &msg) {
88 std::ofstream json_file(std::string(filename), std::ios::out);
89 CHECK(json_file) << ": Couldn't open " << filename;
90 json_file << FlatbufferToJson(msg);
91 json_file.close();
92}
93
Austin Schuhadd6eb32020-11-09 21:24:26 -080094// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -070095template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080096inline void WriteFlatbufferToFile(std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -080097 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -070098 std::ofstream file(std::string(filename),
99 std::ios::out | std::ofstream::binary);
100 CHECK(file) << ": Couldn't open " << filename;
101 std::copy(msg.span().begin(), msg.span().end(),
102 std::ostreambuf_iterator<char>(file));
103}
104
Austin Schuh1674da22022-08-16 17:57:54 -0700105// Parses a file as JSON and returns the corresponding Flatbuffer. Dies if
106// reading the file fails, returns an empty buffer if the contents are invalid.
Brian Silverman8d278412020-06-23 16:37:17 -0700107template <typename T>
108inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
109 const std::string_view path) {
110 std::ifstream t{std::string(path)};
111 std::istream_iterator<char> start(t), end;
112 std::string result(start, end);
113 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
114}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800115
Austin Schuh313677c2020-08-01 14:45:30 -0700116// Parses a file as a binary flatbuffer or dies.
117template <typename T>
118inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
Austin Schuhbba10282021-03-20 22:03:28 -0700119 const std::string data_string = util::ReadFileToStringOrDie(path);
Brian Silverman354697a2020-09-22 21:06:32 -0700120 ResizeableBuffer data;
Austin Schuhbba10282021-03-20 22:03:28 -0700121 data.resize(data_string.size());
122 memcpy(data.data(), data_string.data(), data_string.size());
Austin Schuh313677c2020-08-01 14:45:30 -0700123 return FlatbufferVector<T>(std::move(data));
124}
125
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700126} // namespace aos
127
128#endif // AOS_JSON_TO_FLATBUFFER_H_