7 #include <flatbuffers/base.h>
8 #include <flatbuffers/idl.h>
9 #include <fmt/format.h>
17 flatbuffers::Parser
const& parser,
18 uint8_t
const* buffer,
19 std::string& out_json);
20 std::string
toJson(flatbuffers::Parser
const& parser, uint8_t
const* buffer);
22 flatbuffers::Parser& parser, std::string
const& json_str);
24 template <
typename TMessage>
26 flatbuffers::Parser parser;
27 parser.opts.strict_json =
true;
28 parser.opts.skip_unexpected_fields_in_json =
false;
29 parser.opts.output_default_scalars_in_json =
true;
30 parser.opts.indent_step = -1;
31 parser.opts.size_prefixed =
true;
32 flatbuffers::span<uint8_t const> schema_data =
33 TMessage::NativeTableType::GetSchemaData();
34 parser.Deserialize(schema_data.data(), schema_data.size());
38 template <
typename TMessage>
44 thread_local
static flatbuffers::Parser s_parser =
45 deserializeReflectionParser<TMessage>();
47 s_parser.opts.indent_step = 2;
49 s_parser.opts.indent_step = -1;
54 template <
typename TMessage>
56 uint8_t
const* buffer,
57 std::string& out_json,
59 toJson(reflectionParser<TMessage>(format), buffer, out_json);
62 template <
typename TMessage>
65 return toJson(reflectionParser<TMessage>(format), buffer);
68 template <
typename TMessage>
70 TMessage
const* table,
71 std::string& out_json,
74 char const* error_str = flatbuffers::GenTextFromTable(
75 reflectionParser<TMessage>(format),
77 TMessage::GetFullyQualifiedName(),
79 if (error_str !=
nullptr) {
81 throw std::runtime_error(error_str);
85 template <
typename TMessage>
89 toJson<TMessage>(table, out_json, format);
93 template <
typename TMessageT>
95 TMessageT
const&
object,
96 std::string& out_json,
98 using TableType =
typename TMessageT::TableType;
99 flatbuffers::FlatBufferBuilder builder;
100 builder.FinishSizePrefixed(TableType::Pack(builder, &
object));
102 reflectionParser<TableType>(format),
103 builder.GetBufferPointer(),
107 template <
typename TMessageT>
110 std::string out_json;
111 toJson<TMessageT>(
object, out_json, format);
118 template <
typename TMessage>
119 TMessage
const*
fromJson(std::string
const& json_str) {
121 return flatbuffers::GetSizePrefixedRoot<TMessage>(span.data());
124 template <
typename TMessageT>
125 void fromJson(std::string
const& json_str, TMessageT& out_message) {
126 fromJson<typename TMessageT::TableType>(json_str)->UnPackTo(&out_message);
133 template <
typename TMessage>
137 }
catch (std::runtime_error
const& e) {
138 throw std::runtime_error(
139 fmt::format(
"Failed to parse json file: {}\n{}", json_path, e.what()));
143 template <
typename TMessageT>
144 void fromJsonFile(cpp::fs::path
const& json_path, TMessageT& out_message) {
145 fromJsonFile<typename TMessageT::TableType>(json_path)->UnPackTo(
149 template <
typename TMessageT>
151 cpp::fs::path
const& json_path,
152 TMessageT
const& message,
154 std::ofstream out_json(json_path);
155 out_json <<
toJson(message, format);
158 template <
typename TMessage>
160 cpp::fs::path
const& json_path,
161 TMessage
const* message,
163 std::ofstream out_json(json_path);
164 out_json <<
toJson(message, format);
std::string loadJsonFile(cpp::fs::path const &json_path)
Definition: any_message_input.h:15
flatbuffers::span< uint8_t const > bufferSpanFromJson(flatbuffers::Parser &parser, std::string const &json_str)
TMessage const * fromJson(std::string const &json_str)
Definition: fb_json.h:119
flatbuffers::Parser & reflectionParser(JsonFormat format=JsonFormat::SINGLE_LINE)
Definition: fb_json.h:39
void toJsonFile(cpp::fs::path const &json_path, TMessageT const &message, JsonFormat format=JsonFormat::SINGLE_LINE)
Definition: fb_json.h:150
TMessage const * fromJsonFile(cpp::fs::path const &json_path)
Definition: fb_json.h:134
void toJson(AnyMessageView const &view, std::string &out_json, JsonFormat format=JsonFormat::SINGLE_LINE)
flatbuffers::Parser deserializeReflectionParser()
Definition: fb_json.h:25
JsonFormat
Definition: fb_json.h:14