blob: 1f475beb6dd240276ef00472e637cc13de9e7a51 [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 {
41 // controls if the Json is written ouut on multiple lines or one.
42 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;
45};
46
Austin Schuh3e95e5d2019-09-20 00:08:54 -070047// Converts a flatbuffer into a Json string.
Austin Schuhadd6eb32020-11-09 21:24:26 -080048// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070049::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
50 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070051 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070052
Austin Schuhe93d8642019-10-13 15:27:07 -070053// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
54template <typename T>
55inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070056 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080057 return TableFlatbufferToJson(
58 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
59 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070060}
61
62// Converts a flatbuffer::Table to JSON.
63template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070064typename std::enable_if<
65 std::is_base_of<flatbuffers::Table, T>::value,
66 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
67 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070068 return TableFlatbufferToJson(
69 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070070 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070071}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070072
Brian Silvermancf4fb662021-02-10 17:54:53 -080073std::string FlatbufferToJson(const reflection::Schema *schema,
74 const uint8_t *data,
Ravago Jonescf453ab2020-05-06 21:14:53 -070075 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070076
Tyler Chatowfcf16f42020-07-26 12:41:36 -070077void FlatbufferToJson(FastStringBuilder *builder,
Brian Silvermancf4fb662021-02-10 17:54:53 -080078 const reflection::Schema *schema, const uint8_t *data,
79 JsonOptions json_options = {});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070080
Brian Silverman8d278412020-06-23 16:37:17 -070081// Writes a Flatbuffer to a file, or dies.
82template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080083inline void WriteFlatbufferToJson(std::string_view filename,
Brian Silverman8d278412020-06-23 16:37:17 -070084 const Flatbuffer<T> &msg) {
85 std::ofstream json_file(std::string(filename), std::ios::out);
86 CHECK(json_file) << ": Couldn't open " << filename;
87 json_file << FlatbufferToJson(msg);
88 json_file.close();
89}
90
Austin Schuhadd6eb32020-11-09 21:24:26 -080091// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -070092template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080093inline void WriteFlatbufferToFile(std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -080094 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -070095 std::ofstream file(std::string(filename),
96 std::ios::out | std::ofstream::binary);
97 CHECK(file) << ": Couldn't open " << filename;
98 std::copy(msg.span().begin(), msg.span().end(),
99 std::ostreambuf_iterator<char>(file));
100}
101
Brian Silverman8d278412020-06-23 16:37:17 -0700102// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
103template <typename T>
104inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
105 const std::string_view path) {
106 std::ifstream t{std::string(path)};
107 std::istream_iterator<char> start(t), end;
108 std::string result(start, end);
109 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
110}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800111
Austin Schuh313677c2020-08-01 14:45:30 -0700112// Parses a file as a binary flatbuffer or dies.
113template <typename T>
114inline FlatbufferVector<T> FileToFlatbuffer(const std::string_view path) {
Austin Schuhbba10282021-03-20 22:03:28 -0700115 const std::string data_string = util::ReadFileToStringOrDie(path);
Brian Silverman354697a2020-09-22 21:06:32 -0700116 ResizeableBuffer data;
Austin Schuhbba10282021-03-20 22:03:28 -0700117 data.resize(data_string.size());
118 memcpy(data.data(), data_string.data(), data_string.size());
Austin Schuh313677c2020-08-01 14:45:30 -0700119 return FlatbufferVector<T>(std::move(data));
120}
121
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700122} // namespace aos
123
124#endif // AOS_JSON_TO_FLATBUFFER_H_