blob: 6eab22a80b455319d0d3e5ff0846162d54d216f9 [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;
Brian J Griglak043e0e22022-08-18 12:51:18 -060048 // Will integers be printed in hexadecimal form instead of decimal.
49 bool use_hex = false;
Ravago Jonescf453ab2020-05-06 21:14:53 -070050};
51
Austin Schuh3e95e5d2019-09-20 00:08:54 -070052// Converts a flatbuffer into a Json string.
Austin Schuhadd6eb32020-11-09 21:24:26 -080053// The methods below are generally more useful than TableFlatbufferToJson.
Austin Schuhe93d8642019-10-13 15:27:07 -070054::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
55 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070056 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070057
Austin Schuhe93d8642019-10-13 15:27:07 -070058// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
59template <typename T>
60inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070061 JsonOptions json_options = {}) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080062 return TableFlatbufferToJson(
63 reinterpret_cast<const flatbuffers::Table *>(&flatbuffer.message()),
64 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070065}
66
67// Converts a flatbuffer::Table to JSON.
68template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070069typename std::enable_if<
70 std::is_base_of<flatbuffers::Table, T>::value,
71 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
72 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070073 return TableFlatbufferToJson(
74 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070075 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070076}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070077
Brian Silvermancf4fb662021-02-10 17:54:53 -080078std::string FlatbufferToJson(const reflection::Schema *schema,
79 const uint8_t *data,
Ravago Jonescf453ab2020-05-06 21:14:53 -070080 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070081
Tyler Chatowfcf16f42020-07-26 12:41:36 -070082void FlatbufferToJson(FastStringBuilder *builder,
Brian Silvermancf4fb662021-02-10 17:54:53 -080083 const reflection::Schema *schema, const uint8_t *data,
84 JsonOptions json_options = {});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070085
Brian Silverman8d278412020-06-23 16:37:17 -070086// Writes a Flatbuffer to a file, or dies.
87template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080088inline void WriteFlatbufferToJson(std::string_view filename,
Brian Silverman8d278412020-06-23 16:37:17 -070089 const Flatbuffer<T> &msg) {
90 std::ofstream json_file(std::string(filename), std::ios::out);
91 CHECK(json_file) << ": Couldn't open " << filename;
92 json_file << FlatbufferToJson(msg);
93 json_file.close();
94}
95
Austin Schuhadd6eb32020-11-09 21:24:26 -080096// Writes a NonSizePrefixedFlatbuffer to a binary file, or dies.
Austin Schuh313677c2020-08-01 14:45:30 -070097template <typename T>
Brian Silvermancf4fb662021-02-10 17:54:53 -080098inline void WriteFlatbufferToFile(std::string_view filename,
Austin Schuhadd6eb32020-11-09 21:24:26 -080099 const NonSizePrefixedFlatbuffer<T> &msg) {
Austin Schuh313677c2020-08-01 14:45:30 -0700100 std::ofstream file(std::string(filename),
101 std::ios::out | std::ofstream::binary);
102 CHECK(file) << ": Couldn't open " << filename;
103 std::copy(msg.span().begin(), msg.span().end(),
104 std::ostreambuf_iterator<char>(file));
105}
106
Austin Schuh1674da22022-08-16 17:57:54 -0700107// Parses a file as JSON and returns the corresponding Flatbuffer. Dies if
108// reading the file fails, returns an empty buffer if the contents are invalid.
Brian Silverman8d278412020-06-23 16:37:17 -0700109template <typename T>
110inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
111 const std::string_view path) {
Alexander Yeee61cac32023-02-11 19:40:40 -0800112 return FlatbufferDetachedBuffer<T>(
113 JsonToFlatbuffer<T>(util::ReadFileToStringOrDie(path)));
Brian Silverman8d278412020-06-23 16:37:17 -0700114}
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_