FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
BehaviorTree.CPP/src/json_export.cpp at master · BehaviorTree/BehaviorTree.CPP · GitHub
BehaviorTree
/
BehaviorTree.CPP
Public
Notifications
You must be signed in to change notification settings
Fork
857
Star
4.2k
Code
Issues
33
Pull requests
21
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
BehaviorTree.CPP
/
src
/
json_export.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
158 lines (146 loc) · 4.13 KB
Breadcrumbs
BehaviorTree.CPP
/
src
/
json_export.cpp
Copy path
File metadata and controls
158 lines (146 loc) · 4.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#
include
"
behaviortree_cpp/json_export.h
"
namespace
BT
{
JsonExporter&
JsonExporter::get
()
{
static
JsonExporter global_instance;
return
global_instance;
}
bool
JsonExporter::toJson
(
const
Any& any, nlohmann::json& dst)
const
{
auto
const
& type = any.
castedType
();
if
(any.
isString
())
{
dst = any.
cast
<std::string>();
}
else
if
(type ==
typeid
(
int64_t
))
{
dst = any.
cast
<
int64_t
>();
}
else
if
(type ==
typeid
(
uint64_t
))
{
dst = any.
cast
<
uint64_t
>();
}
else
if
(type ==
typeid
(
double
))
{
dst = any.
cast
<
double
>();
}
else
if
(type ==
typeid
(std::vector<
double
>))
{
dst = any.
cast
<std::vector<
double
>>();
}
else
if
(type ==
typeid
(std::vector<
int
>))
{
dst = any.
cast
<std::vector<
int
>>();
}
else
if
(type ==
typeid
(std::vector<std::string>))
{
dst = any.
cast
<std::vector<std::string>>();
}
else
if
(type ==
typeid
(std::vector<
bool
>))
{
dst = any.
cast
<std::vector<
bool
>>();
}
else
{
auto
it = to_json_converters_.
find
(type);
if
(it != to_json_converters_.
end
())
{
it->
second
(any, dst);
}
else
{
return
false
;
}
}
return
true
;
}
JsonExporter::ExpectedEntry
JsonExporter::fromJson
(
const
nlohmann::json& source)
const
{
if
(source.
is_null
())
{
return
Entry{
BT::Any
(),
BT
::TypeInfo::Create<std::
nullptr_t
>() };
}
if
(source.
is_string
())
{
return
Entry{
BT::Any
(source.
get
<std::string>()),
BT
::TypeInfo::Create<std::string>() };
}
if
(source.
is_number_integer
())
{
return
Entry{
BT::Any
(source.
get
<
int
>()),
BT
::TypeInfo::Create<
int
>() };
}
if
(source.
is_number_float
())
{
return
Entry{
BT::Any
(source.
get
<
double
>()),
BT
::TypeInfo::Create<
double
>() };
}
if
(source.
is_boolean
())
{
return
Entry{
BT::Any
(source.
get
<
bool
>()),
BT
::TypeInfo::Create<
bool
>() };
}
//
basic vectors
if
(source.
is_array
() && source.
size
() >
0
&& !source.
contains
(
"
__type
"
))
{
const
auto
first_element = source[
0
];
if
(first_element.
is_string
())
{
return
Entry{
BT::Any
(source.
get
<std::vector<std::string>>()),
BT
::TypeInfo::Create<std::vector<std::string>>() };
}
if
(first_element.
is_number_integer
())
{
return
Entry{
BT::Any
(source.
get
<std::vector<
int
>>()),
BT
::TypeInfo::Create<std::vector<
int
>>() };
}
if
(first_element.
is_number_float
())
{
return
Entry{
BT::Any
(source.
get
<std::vector<
double
>>()),
BT
::TypeInfo::Create<std::vector<
double
>>() };
}
if
(first_element.
is_boolean
())
{
return
Entry{
BT::Any
(source.
get
<std::vector<
bool
>>()),
BT
::TypeInfo::Create<std::vector<
bool
>>() };
}
}
if
(source.
is_array
())
{
if
(source.
empty
())
return
nonstd::make_unexpected
(
"
Missing field '__type'
"
);
const
auto
& first = source[
0
];
if
(!first.
is_object
() || !first.
contains
(
"
__type
"
))
return
nonstd::make_unexpected
(
"
Missing field '__type'
"
);
if
(!first[
"
__type
"
].
is_string
())
return
nonstd::make_unexpected
(
"
Invalid '__type' (must be string)
"
);
}
else
{
if
(!source.
is_object
() || !source.
contains
(
"
__type
"
) || !source[
"
__type
"
].
is_string
())
return
nonstd::make_unexpected
(
"
Missing field '__type'
"
);
}
auto
& from_converters =
source.
is_array
() ? from_json_array_converters_ : from_json_converters_;
auto
type_field = source.
is_array
() ? source[
0
][
"
__type
"
] : source[
"
__type
"
];
auto
type_it = type_names_.
find
(type_field);
if
(type_it == type_names_.
end
())
{
return
nonstd::make_unexpected
(
"
Type not found in registered list
"
);
}
auto
func_it = from_converters.
find
(type_it->
second
.
type
());
if
(func_it == from_converters.
end
())
{
return
nonstd::make_unexpected
(
"
Type not found in registered list
"
);
}
return
func_it->
second
(source);
}
JsonExporter::ExpectedEntry
JsonExporter::fromJson
(
const
nlohmann::json& source,
std::type_index type)
const
{
auto
func_it = from_json_converters_.
find
(type);
if
(func_it == from_json_converters_.
end
())
{
return
nonstd::make_unexpected
(
"
Type not found in registered list
"
);
}
return
func_it->
second
(source);
}
}
//
namespace BT
Back
|
FazBrowse Home
|
New Git URL