Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Attributes | List of all members
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. 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 JsonExporterget ()
 

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, FromJonConverterfrom_json_converters_
 
std::unordered_map< std::type_index, ToJonConverterto_json_converters_
 
std::unordered_map< std::string, BT::TypeInfotype_names_
 

Detailed Description

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.

Member Typedef Documentation

◆ Entry

This information is needed to create a BT::Blackboard::entry.

Definition at line 63 of file json_export.h.

◆ ExpectedEntry

using BT::JsonExporter::ExpectedEntry = nonstd::expected<Entry, std::string>

Definition at line 65 of file json_export.h.

◆ FromJonConverter

using BT::JsonExporter::FromJonConverter = std::function<Entry(const nlohmann::json&)>
private

Definition at line 104 of file json_export.h.

◆ ToJonConverter

using BT::JsonExporter::ToJonConverter = std::function<void(const BT::Any&, nlohmann::json&)>
private

Definition at line 103 of file json_export.h.

Member Function Documentation

◆ addConverter() [1/3]

template<typename T >
void BT::JsonExporter::addConverter
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.

◆ addConverter() [2/3]

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

Register custom from_json converter directly.

Definition at line 170 of file json_export.h.

◆ addConverter() [3/3]

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.

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

Definition at line 155 of file json_export.h.

◆ fromJson() [1/3]

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

Parameters
source
Returns

Definition at line 48 of file json_export.cpp.

◆ fromJson() [2/3]

template<typename T >
Expected< T > BT::JsonExporter::fromJson ( const nlohmann::json source) const
inline

Definition at line 112 of file json_export.h.

◆ fromJson() [3/3]

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.

◆ get()

JsonExporter & BT::JsonExporter::get ( )
static

Definition at line 6 of file json_export.cpp.

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

Definition at line 12 of file json_export.cpp.

Member Data Documentation

◆ from_json_converters_

std::unordered_map<std::type_index, FromJonConverter> BT::JsonExporter::from_json_converters_
private

Definition at line 107 of file json_export.h.

◆ to_json_converters_

std::unordered_map<std::type_index, ToJonConverter> BT::JsonExporter::to_json_converters_
private

Definition at line 106 of file json_export.h.

◆ type_names_

std::unordered_map<std::string, BT::TypeInfo> BT::JsonExporter::type_names_
private

Definition at line 108 of file json_export.h.


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


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:09