#include <json_export.h>
Public Types | |
using | Entry = std::pair< BT::Any, BT::TypeInfo > |
This information is needed to create a BT::Blackboard::entry. More... | |
using | ExpectedEntry = nonstd::expected< Entry, std::string > |
Public Member Functions | |
template<typename T > | |
void | addConverter () |
template<typename T > | |
void | addConverter (std::function< void(const nlohmann::json &, T &)> from_json) |
Register custom from_json converter directly. More... | |
template<typename T > | |
void | addConverter (std::function< void(const T &, nlohmann::json &)> to_json, bool add_type=true) |
addConverter register a to_json function that converts a json to a type T. More... | |
ExpectedEntry | fromJson (const nlohmann::json &source) const |
fromJson will return an Entry (value wrappedn in Any + TypeInfo) from a json source. If it is a custom type, you might register it first with addConverter(). More... | |
template<typename T > | |
Expected< T > | fromJson (const nlohmann::json &source) const |
ExpectedEntry | fromJson (const nlohmann::json &source, std::type_index type) const |
bool | toJson (const BT::Any &any, nlohmann::json &destination) const |
toJson adds the content of "any" to the JSON "destination". More... | |
Static Public Member Functions | |
static JsonExporter & | get () |
Private Types | |
using | FromJonConverter = std::function< Entry(const nlohmann::json &)> |
using | ToJonConverter = std::function< void(const BT::Any &, nlohmann::json &)> |
Private Attributes | |
std::unordered_map< std::type_index, FromJonConverter > | from_json_converters_ |
std::unordered_map< std::type_index, ToJonConverter > | to_json_converters_ |
std::unordered_map< std::string, BT::TypeInfo > | type_names_ |
To add new type to the JSON library, you should follow these isntructions: https://json.nlohmann.me/features/arbitrary_types/
Considering for instance the type:
struct Point2D { double x; double y; };
This would require the implementation of:
void to_json(nlohmann::json& j, const Point2D& point); void from_json(const nlohmann::json& j, Point2D& point);
To avoid repeating yourself, we provide the macro BT_JSON_CONVERTION that implements both those function, at once. Usage:
BT_JSON_CONVERTER(Point2D, point) { add_field("x", &point.x); add_field("y", &point.y); }
Later, you MUST register the type using:
BT::RegisterJsonDefinition<Point2D>(); Use RegisterJsonDefinition<Foo>();
Definition at line 49 of file json_export.h.
using BT::JsonExporter::Entry = std::pair<BT::Any, BT::TypeInfo> |
This information is needed to create a BT::Blackboard::entry.
Definition at line 63 of file json_export.h.
using BT::JsonExporter::ExpectedEntry = nonstd::expected<Entry, std::string> |
Definition at line 65 of file json_export.h.
|
private |
Definition at line 104 of file json_export.h.
|
private |
Definition at line 103 of file json_export.h.
|
inline |
Register new JSON converters with addConverter<Foo>(). You should have used first the macro BT_JSON_CONVERTER
Definition at line 130 of file json_export.h.
|
inline |
Register custom from_json converter directly.
Definition at line 170 of file json_export.h.
|
inline |
addConverter register a to_json function that converts a json to a type T.
to_json | the function with signature void(const T&, nlohmann::json&) |
add_type | if true, add a field called [__type] with the name ofthe type. |
Definition at line 155 of file json_export.h.
JsonExporter::ExpectedEntry BT::JsonExporter::fromJson | ( | const nlohmann::json & | source | ) | const |
fromJson will return an Entry (value wrappedn in Any + TypeInfo) from a json source. If it is a custom type, you might register it first with addConverter().
source |
Definition at line 48 of file json_export.cpp.
|
inline |
Definition at line 112 of file json_export.h.
JsonExporter::ExpectedEntry BT::JsonExporter::fromJson | ( | const nlohmann::json & | source, |
std::type_index | type | ||
) | const |
Same as the other, but providing the specific type, To be preferred if the JSON doesn't contain the field [__type]
Definition at line 93 of file json_export.cpp.
|
static |
Definition at line 6 of file json_export.cpp.
bool BT::JsonExporter::toJson | ( | const BT::Any & | any, |
nlohmann::json & | destination | ||
) | const |
toJson adds the content of "any" to the JSON "destination".
It will return false if the conversion toJson is not possible If it is a custom type, you might register it first with addConverter().
Definition at line 12 of file json_export.cpp.
|
private |
Definition at line 107 of file json_export.h.
|
private |
Definition at line 106 of file json_export.h.
|
private |
Definition at line 108 of file json_export.h.