BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
BT::JsonExporter Class Reference

#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
 
JsonExporteroperator= (const JsonExporter &)=delete
 
 JsonExporter (JsonExporter &&)=delete
 
JsonExporteroperator= (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 JsonExporterget ()
 

Detailed Description

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>();

Member Function Documentation

◆ addConverter() [1/2]

template<typename T >
void BT::JsonExporter::addConverter ( std::function< void(const nlohmann::json &, T &)>  from_json)
inline

addConverter register a from_json function that converts a json to a type T. The conversions from std::vector<T> is automatically registered.

Parameters
from_jsonthe function with signature void(const nlohmann::json&, T&)

◆ addConverter() [2/2]

template<typename T >
void BT::JsonExporter::addConverter ( std::function< void(const T &, nlohmann::json &)>  to_json,
bool  add_type = true 
)
inline

addConverter register a to_json function that converts a json to a type T. The conversion to std:vector<T> is automatically registered.

Parameters
to_jsonthe function with signature void(const T&, nlohmann::json&)
add_typeif true, add a field called [__type] with the name of the type.

◆ fromJson() [1/2]

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().

Parameters
source
Returns

◆ fromJson() [2/2]

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]

◆ toJson()

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().


The documentation for this class was generated from the following file: