json_export.cpp
Go to the documentation of this file.
2 
3 namespace BT
4 {
5 
7 {
8  static JsonExporter global_instance;
9  return global_instance;
10 }
11 
12 bool JsonExporter::toJson(const Any& any, nlohmann::json& dst) const
13 {
15  auto const& type = any.castedType();
16 
17  if(any.isString())
18  {
19  dst = any.cast<std::string>();
20  }
21  else if(type == typeid(int64_t))
22  {
23  dst = any.cast<int64_t>();
24  }
25  else if(type == typeid(uint64_t))
26  {
27  dst = any.cast<uint64_t>();
28  }
29  else if(type == typeid(double))
30  {
31  dst = any.cast<double>();
32  }
33  else
34  {
35  auto it = to_json_converters_.find(type);
36  if(it != to_json_converters_.end())
37  {
38  it->second(any, dst);
39  }
40  else
41  {
42  return false;
43  }
44  }
45  return true;
46 }
47 
49 {
50  if(source.is_null())
51  {
52  return nonstd::make_unexpected("json object is null");
53  }
54  if(source.is_string())
55  {
56  return Entry{ BT::Any(source.get<std::string>()),
57  BT::TypeInfo::Create<std::string>() };
58  }
59  if(source.is_number_unsigned())
60  {
61  return Entry{ BT::Any(source.get<uint64_t>()), BT::TypeInfo::Create<uint64_t>() };
62  }
63  if(source.is_number_integer())
64  {
65  return Entry{ BT::Any(source.get<int64_t>()), BT::TypeInfo::Create<int64_t>() };
66  }
67  if(source.is_number_float())
68  {
69  return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
70  }
71  if(source.is_boolean())
72  {
73  return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
74  }
75 
76  if(!source.contains("__type"))
77  {
78  return nonstd::make_unexpected("Missing field '__type'");
79  }
80  auto type_it = type_names_.find(source["__type"]);
81  if(type_it == type_names_.end())
82  {
83  return nonstd::make_unexpected("Type not found in registered list");
84  }
85  auto func_it = from_json_converters_.find(type_it->second.type());
86  if(func_it == from_json_converters_.end())
87  {
88  return nonstd::make_unexpected("Type not found in registered list");
89  }
90  return func_it->second(source);
91 }
92 
94  std::type_index type) const
95 {
96  auto func_it = from_json_converters_.find(type);
97  if(func_it == from_json_converters_.end())
98  {
99  return nonstd::make_unexpected("Type not found in registered list");
100  }
101  return func_it->second(source);
102 }
103 
104 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::JsonExporter::toJson
bool toJson(const BT::Any &any, nlohmann::json &destination) const
toJson adds the content of "any" to the JSON "destination".
Definition: json_export.cpp:12
BT::Any
Definition: safe_any.hpp:36
BT::JsonExporter::Entry
std::pair< BT::Any, BT::TypeInfo > Entry
This information is needed to create a BT::Blackboard::entry.
Definition: json_export.h:63
basic_json
namespace for Niels Lohmann
Definition: json.hpp:3411
BT::JsonExporter::type_names_
std::unordered_map< std::string, BT::TypeInfo > type_names_
Definition: json_export.h:108
BT::JsonExporter::from_json_converters_
std::unordered_map< std::type_index, FromJonConverter > from_json_converters_
Definition: json_export.h:107
BT::JsonExporter
Definition: json_export.h:49
json_export.h
BT::JsonExporter::get
static JsonExporter & get()
Definition: json_export.cpp:6
BT::JsonExporter::ExpectedEntry
nonstd::expected< Entry, std::string > ExpectedEntry
Definition: json_export.h:65
BT::JsonExporter::to_json_converters_
std::unordered_map< std::type_index, ToJonConverter > to_json_converters_
Definition: json_export.h:106
json
basic_json<> json
default specialization
Definition: json.hpp:3422
BT::JsonExporter::fromJson
ExpectedEntry fromJson(const nlohmann::json &source) const
fromJson will return an Entry (value wrappedn in Any + TypeInfo) from a json source....
Definition: json_export.cpp:48
lexyd::any
constexpr auto any
Matches anything and consumes all remaining characters.
Definition: 3rdparty/lexy/include/lexy/dsl/any.hpp:42


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