|
BehaviorTree
Core Library to create and execute Behavior Trees
|
#include <json_export.h>
Public Types | |
| using | Entry = std::pair< BT::Any, BT::TypeInfo > |
| This information is needed to create a BT::Blackboard::entry. | |
| using | ExpectedEntry = nonstd::expected< Entry, std::string > |
Public Member Functions | |
| JsonExporter (const JsonExporter &)=delete | |
| JsonExporter & | operator= (const JsonExporter &)=delete |
| JsonExporter (JsonExporter &&)=delete | |
| JsonExporter & | operator= (JsonExporter &&)=delete |
| bool | toJson (const BT::Any &any, nlohmann::json &destination) const |
| toJson adds the content of "any" to the JSON "destination". | |
| 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(). | |
| ExpectedEntry | fromJson (const nlohmann::json &source, std::type_index type) const |
| template<typename T > | |
| Expected< T > | fromJson (const nlohmann::json &source) const |
| template<typename T > | |
| void | addConverter () |
| Register new JSON converters with addConverter<Foo>(). You should used first the macro BT_JSON_CONVERTER. The conversions from/to vector<T> are automatically registered. | |
| 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. The conversion to std:vector<T> is automatically registered. | |
| template<typename T > | |
| void | addConverter (std::function< void(const nlohmann::json &, T &)> from_json) |
| addConverter register a from_json function that converts a json to a type T. The conversions from std::vector<T> is automatically registered. | |
Static Public Member Functions | |
| static JsonExporter & | get () |
To add new type to the JSON library, you should follow these instructions: 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>();
|
inline |
addConverter register a from_json function that converts a json to a type T. The conversions from std::vector<T> is automatically registered.
| from_json | the function with signature void(const nlohmann::json&, T&) |
|
inline |
addConverter register a to_json function that converts a json to a type T. The conversion to std:vector<T> is automatically registered.
| to_json | the function with signature void(const T&, nlohmann::json&) |
| add_type | if true, add a field called [__type] with the name of the type. |
| 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 |
| 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]
| 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().