blob: b765db30f7adbb58b8c3e3855184d3fce6bccbfa [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
Austin Schuhe93d8642019-10-13 15:27:07 -07009#include "aos/flatbuffers.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070010#include "flatbuffers/flatbuffers.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080011#include "flatbuffers/reflection.h"
Austin Schuh3e95e5d2019-09-20 00:08:54 -070012
13namespace aos {
14
Austin Schuh53b1a6f2020-01-10 19:31:28 -080015// Parses the flatbuffer into the buffer, or returns an empty buffer.
Austin Schuhe93d8642019-10-13 15:27:07 -070016flatbuffers::DetachedBuffer JsonToFlatbuffer(
Tyler Chatow5e369a42019-11-23 11:57:31 -080017 const std::string_view data, const flatbuffers::TypeTable *typetable);
Austin Schuh3e95e5d2019-09-20 00:08:54 -070018
Austin Schuh53b1a6f2020-01-10 19:31:28 -080019// Parses the flatbuffer into the builder, and returns the offset.
20flatbuffers::Offset<flatbuffers::Table> JsonToFlatbuffer(
21 const std::string_view data, const flatbuffers::TypeTable *typetable,
22 flatbuffers::FlatBufferBuilder *fbb);
23
24// Typed versions of the above methods.
25template <typename T>
26inline flatbuffers::DetachedBuffer JsonToFlatbuffer(
27 const std::string_view data) {
28 return JsonToFlatbuffer(data, T::MiniReflectTypeTable());
29}
30template <typename T>
31inline flatbuffers::Offset<T> JsonToFlatbuffer(
32 const std::string_view data, flatbuffers::FlatBufferBuilder *fbb) {
33 return flatbuffers::Offset<T>(
34 JsonToFlatbuffer(data, T::MiniReflectTypeTable(), fbb).o);
35}
36
Austin Schuh3e95e5d2019-09-20 00:08:54 -070037// Converts a flatbuffer into a Json string.
Austin Schuh3e95e5d2019-09-20 00:08:54 -070038// multi_line controls if the Json is written out on multiple lines or one.
Austin Schuhe93d8642019-10-13 15:27:07 -070039// The methods below are generally more useful than BufferFlatbufferToJson and
40// TableFlatbufferToJson.
41::std::string BufferFlatbufferToJson(const uint8_t *buffer,
42 const flatbuffers::TypeTable *typetable,
Austin Schuhd3936202020-04-07 20:11:07 -070043 bool multi_line = false,
44 size_t max_vector_size = SIZE_MAX);
Austin Schuhe93d8642019-10-13 15:27:07 -070045
46::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
47 const ::flatbuffers::TypeTable *typetable,
Brian Silvermanc8a5b552020-04-28 16:43:59 -070048 bool multi_line,
49 size_t max_vector_size = SIZE_MAX);
Austin Schuhe93d8642019-10-13 15:27:07 -070050
51// Converts a DetachedBuffer holding a flatbuffer to JSON.
52inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
53 const flatbuffers::TypeTable *typetable,
Austin Schuhd3936202020-04-07 20:11:07 -070054 bool multi_line = false,
55 size_t max_vector_size = SIZE_MAX) {
56 return BufferFlatbufferToJson(buffer.data(), typetable, multi_line,
57 max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070058}
59
60// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
61template <typename T>
62inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Austin Schuhd3936202020-04-07 20:11:07 -070063 bool multi_line = false,
64 size_t max_vector_size = SIZE_MAX) {
65 return BufferFlatbufferToJson(flatbuffer.data(),
66 Flatbuffer<T>::MiniReflectTypeTable(),
67 multi_line, max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070068}
69
70// Converts a flatbuffer::Table to JSON.
71template <typename T>
Austin Schuhd3936202020-04-07 20:11:07 -070072typename std::enable_if<std::is_base_of<flatbuffers::Table, T>::value,
73 std::string>::
74 type inline FlatbufferToJson(const T *flatbuffer, bool multi_line = false,
75 size_t max_vector_size = SIZE_MAX) {
Austin Schuhe93d8642019-10-13 15:27:07 -070076 return TableFlatbufferToJson(
77 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Austin Schuhd3936202020-04-07 20:11:07 -070078 Flatbuffer<T>::MiniReflectTypeTable(), multi_line, max_vector_size);
Austin Schuhe93d8642019-10-13 15:27:07 -070079}
Austin Schuh3e95e5d2019-09-20 00:08:54 -070080
Tyler Chatow5e369a42019-11-23 11:57:31 -080081std::string FlatbufferToJson(const reflection::Schema *const schema,
Brian Silverman8d278412020-06-23 16:37:17 -070082 const uint8_t *const data, bool multi_line = false,
83 size_t max_vector_size = SIZE_MAX);
84
85// Writes a Flatbuffer to a file, or dies.
86template <typename T>
87inline void WriteFlatbufferToJson(const std::string_view filename,
88 const Flatbuffer<T> &msg) {
89 std::ofstream json_file(std::string(filename), std::ios::out);
90 CHECK(json_file) << ": Couldn't open " << filename;
91 json_file << FlatbufferToJson(msg);
92 json_file.close();
93}
94
95// Parses a file as JSON and returns the corresponding Flatbuffer, or dies.
96template <typename T>
97inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
98 const std::string_view path) {
99 std::ifstream t{std::string(path)};
100 std::istream_iterator<char> start(t), end;
101 std::string result(start, end);
102 return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
103}
Tyler Chatow5e369a42019-11-23 11:57:31 -0800104
Austin Schuh3e95e5d2019-09-20 00:08:54 -0700105} // namespace aos
106
107#endif // AOS_JSON_TO_FLATBUFFER_H_