blob: aebd0690853c06fa391f4e0ba5ed78dec7ab74d5 [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
Ravago Jonescf453ab2020-05-06 21:14:53 -070037struct JsonOptions {
38 // controls if the Json is written ouut on multiple lines or one.
39 bool multi_line = false;
40 // the contents of vectors longer than max_vector_size will be skipped.
41 size_t max_vector_size = SIZE_MAX;
42};
43
Austin Schuh3e95e5d2019-09-20 00:08:54 -070044// Converts a flatbuffer into a Json string.
Austin Schuhe93d8642019-10-13 15:27:07 -070045// The methods below are generally more useful than BufferFlatbufferToJson and
46// TableFlatbufferToJson.
47::std::string BufferFlatbufferToJson(const uint8_t *buffer,
48 const flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070049 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070050
51::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
52 const ::flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070053 JsonOptions json_options = {});
Austin Schuhe93d8642019-10-13 15:27:07 -070054
55// Converts a DetachedBuffer holding a flatbuffer to JSON.
56inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
57 const flatbuffers::TypeTable *typetable,
Ravago Jonescf453ab2020-05-06 21:14:53 -070058 JsonOptions json_options = {}) {
59 return BufferFlatbufferToJson(buffer.data(), typetable, json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070060}
61
62// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
63template <typename T>
64inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
Ravago Jonescf453ab2020-05-06 21:14:53 -070065 JsonOptions json_options = {}) {
66 return BufferFlatbufferToJson(
67 flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), json_options);
Austin Schuhe93d8642019-10-13 15:27:07 -070068}
69
70// Converts a flatbuffer::Table to JSON.
71template <typename T>
Ravago Jonescf453ab2020-05-06 21:14:53 -070072typename std::enable_if<
73 std::is_base_of<flatbuffers::Table, T>::value,
74 std::string>::type inline FlatbufferToJson(const T *flatbuffer,
75 JsonOptions json_options = {}) {
Austin Schuhe93d8642019-10-13 15:27:07 -070076 return TableFlatbufferToJson(
77 reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
Ravago Jonescf453ab2020-05-06 21:14:53 -070078 Flatbuffer<T>::MiniReflectTypeTable(), json_options);
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,
Ravago Jonescf453ab2020-05-06 21:14:53 -070082 const uint8_t *const data,
83 JsonOptions json_options = {});
Brian Silverman8d278412020-06-23 16:37:17 -070084
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_