blob: 9ba50f89711d3d5d620339fbef7ccfaa53be05c1 [file] [log] [blame]
#ifndef AOS_JSON_TO_FLATBUFFER_H_
#define AOS_JSON_TO_FLATBUFFER_H_
#include <cstddef>
#include <fstream>
#include <string>
#include <string_view>
#include "aos/fast_string_builder.h"
#include "aos/flatbuffers.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/reflection.h"
namespace aos {
// Parses the flatbuffer into the buffer, or returns an empty buffer.
flatbuffers::DetachedBuffer JsonToFlatbuffer(
const std::string_view data, const flatbuffers::TypeTable *typetable);
// Parses the flatbuffer into the builder, and returns the offset.
flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
const std::string_view data, const flatbuffers::TypeTable *typetable,
flatbuffers::FlatBufferBuilder *fbb);
// Typed versions of the above methods.
template <typename T>
inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
const std::string_view data) {
return JsonToFlatbuffer(data, T::MiniReflectTypeTable());
}
template <typename T>
inline flatbuffers::Offset<T> JsonToFlatbuffer(
const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
return flatbuffers::Offset<T>(
JsonToFlatbuffer(data, T::MiniReflectTypeTable(), fbb).o);
}
struct JsonOptions {
// controls if the Json is written ouut on multiple lines or one.
bool multi_line = false;
// the contents of vectors longer than max_vector_size will be skipped.
size_t max_vector_size = SIZE_MAX;
};
// Converts a flatbuffer into a Json string.
// The methods below are generally more useful than BufferFlatbufferToJson and
// TableFlatbufferToJson.
::std::string BufferFlatbufferToJson(const uint8_t *buffer,
const flatbuffers::TypeTable *typetable,
JsonOptions json_options = {});
::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
const ::flatbuffers::TypeTable *typetable,
JsonOptions json_options = {});
// Converts a DetachedBuffer holding a flatbuffer to JSON.
inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
const flatbuffers::TypeTable *typetable,
JsonOptions json_options = {}) {
return BufferFlatbufferToJson(buffer.data(), typetable, json_options);
}
// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
template <typename T>
inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
JsonOptions json_options = {}) {
return BufferFlatbufferToJson(
flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), json_options);
}
// Converts a flatbuffer::Table to JSON.
template <typename T>
typename std::enable_if<
std::is_base_of<flatbuffers::Table, T>::value,
std::string>::type inline FlatbufferToJson(const T *flatbuffer,
JsonOptions json_options = {}) {
return TableFlatbufferToJson(
reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Flatbuffer<T>::MiniReflectTypeTable(), json_options);
}
std::string FlatbufferToJson(const reflection::Schema *const schema,
const uint8_t *const data,
JsonOptions json_options = {});
void FlatbufferToJson(FastStringBuilder *builder,
const reflection::Schema *const schema,
const uint8_t *const data, JsonOptions json_options = {});
// Writes a Flatbuffer to a file, or dies.
template <typename T>
inline void WriteFlatbufferToJson(const std::string_view filename,
const Flatbuffer<T> &msg) {
std::ofstream json_file(std::string(filename), std::ios::out);
CHECK(json_file) << ": Couldn't open " << filename;
json_file << FlatbufferToJson(msg);
json_file.close();
}
// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
template <typename T>
inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
const std::string_view path) {
std::ifstream t{std::string(path)};
std::istream_iterator<char> start(t), end;
std::string result(start, end);
return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
}
} // namespace aos
#endif // AOS_JSON_TO_FLATBUFFER_H_