MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / fromJson

Method fromJson

src/json_export.cpp:63–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61}
62
63JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source) const
64{
65 if(source.is_null())
66 {
67 return Entry{ BT::Any(), BT::TypeInfo::Create<std::nullptr_t>() };
68 }
69
70 if(source.is_string())
71 {
72 return Entry{ BT::Any(source.get<std::string>()),
73 BT::TypeInfo::Create<std::string>() };
74 }
75 if(source.is_number_integer())
76 {
77 return Entry{ BT::Any(source.get<int>()), BT::TypeInfo::Create<int>() };
78 }
79 if(source.is_number_float())
80 {
81 return Entry{ BT::Any(source.get<double>()), BT::TypeInfo::Create<double>() };
82 }
83 if(source.is_boolean())
84 {
85 return Entry{ BT::Any(source.get<bool>()), BT::TypeInfo::Create<bool>() };
86 }
87
88 // basic vectors
89 if(source.is_array() && source.size() > 0 && !source.contains("__type"))
90 {
91 const auto first_element = source[0];
92 if(first_element.is_string())
93 {
94 return Entry{ BT::Any(source.get<std::vector<std::string>>()),
95 BT::TypeInfo::Create<std::vector<std::string>>() };
96 }
97 if(first_element.is_number_integer())
98 {
99 return Entry{ BT::Any(source.get<std::vector<int>>()),
100 BT::TypeInfo::Create<std::vector<int>>() };
101 }
102 if(first_element.is_number_float())
103 {
104 return Entry{ BT::Any(source.get<std::vector<double>>()),
105 BT::TypeInfo::Create<std::vector<double>>() };
106 }
107 if(first_element.is_boolean())
108 {
109 return Entry{ BT::Any(source.get<std::vector<bool>>()),
110 BT::TypeInfo::Create<std::vector<bool>>() };
111 }
112 }
113
114 if(source.is_array())
115 {
116 if(source.empty())
117 return nonstd::make_unexpected("Missing field '__type'");
118 const auto& first = source[0];
119 if(!first.is_object() || !first.contains("__type"))
120 return nonstd::make_unexpected("Missing field '__type'");

Callers 3

convertFromJSONFunction · 0.45
ImportBlackboardFromJSONFunction · 0.45
TEST_FFunction · 0.45

Calls 6

AnyClass · 0.85
make_unexpectedFunction · 0.85
sizeMethod · 0.45
emptyMethod · 0.45
endMethod · 0.45
typeMethod · 0.45

Tested by 1

TEST_FFunction · 0.36