json_conversions.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package VisionX::ArmarXObjects::armem_images_server
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2021
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "json_conversions.h"
24
25#include <SimoxUtility/algorithm/string/string_tools.h>
26#include <SimoxUtility/color/Color.h>
27#include <SimoxUtility/color/json.h>
28#include <SimoxUtility/json/util.h>
29
34
38
39#include <VisionX/libraries/armem_human/aron/Person.aron.generated.h>
40
41namespace armarx::human
42{
43
44 template <class ValueType>
45 std::optional<ValueType>
46 opt_from_json(const simox::json::json& j, const std::string key)
47 {
48 if (j.count(key))
49 {
50 ValueType value;
51 j.at(key).get_to(value);
52 return std::optional(value);
53 }
54 return std::nullopt;
55 }
56
57 template <class ValueType>
58 void
59 set_if_present(simox::json::json& j,
60 const std::string key,
61 const std::optional<ValueType> optional)
62 {
63 if (optional.has_value())
64 {
65 j[key] = optional.value();
66 }
67 }
68
69 void
70 arondto::to_json(simox::json::json& j, const Person& arondto)
71 {
72 j["firstName"] = arondto.id.firstName;
73 j["lastName"] = arondto.id.lastName;
74 j["names"] = arondto.names;
75
76 j["roles"] = arondto.roles;
77
78 if (arondto.attributes)
79 {
81 arondto.attributes);
82 }
83 else
84 {
85 j["attributes"] = simox::json::json::object();
86 }
87
88 set_if_present(j, "rooms", arondto.rooms);
89 j["physiologicalBiometrics"] = arondto.physiologicalBiometrics;
90 j["previousInteractions"] = arondto.previousInteractions;
91 j["preferences"] = arondto.preferences;
92 j["abilities"] = arondto.abilities;
93 j["medicalConditions"] = arondto.medicalConditions;
94 j["customAttributes"] = arondto.customAttributes;
95 j["customNotes"] = arondto.customNotes;
96 }
97
98 void
99 arondto::from_json(const simox::json::json& j, Person& arondto)
100 {
101 using namespace simox::json;
102
103 ARMARX_INFO << "Converting profile from json to profile";
104
105 get_to_if_exists(j, "firstName", arondto.id.firstName);
106 get_to_if_exists(j, "lastName", arondto.id.lastName);
107
108 get_to_if_exists(j, "names", arondto.names);
109
110 get_to_if_exists(j, "roles", arondto.roles);
111
112 if (j.count("attributes"))
113 {
116 j.at("attributes"));
117 if (auto dict = std::dynamic_pointer_cast<aron::data::Dict>(data))
118 {
119 arondto.attributes = dict;
120 }
121 else
122 {
123 arondto.attributes = nullptr;
124 }
125 }
126 else
127 {
128 arondto.attributes = nullptr;
129 }
130
131 arondto.rooms = opt_from_json<PersonalRooms>(j, "rooms");
132 get_to_if_exists(j, "physiologicalBiometrics", arondto.physiologicalBiometrics);
133 get_to_if_exists(j, "previousInteractions", arondto.previousInteractions);
134 get_to_if_exists(j, "preferences", arondto.preferences);
135 get_to_if_exists(j, "abilities", arondto.abilities);
136 get_to_if_exists(j, "medicalConditions", arondto.medicalConditions);
137 get_to_if_exists(j, "customAttributes", arondto.customAttributes);
138 get_to_if_exists(j, "customNotes", arondto.customNotes);
139 }
140
141 void
142 arondto::to_json(simox::json::json& j, const Gender& arondto)
143 {
144 j = simox::alg::to_lower(arondto.toString());
145 }
146
147 void
148 arondto::from_json(const simox::json::json& j, Gender& arondto)
149 {
150 arondto.fromString(simox::alg::to_upper(j.get<std::string>()));
151 }
152
153 void
154 arondto::to_json(simox::json::json& j, const Handedness& arondto)
155 {
156 j = simox::alg::to_lower(arondto.toString());
157 }
158
159 void
160 arondto::from_json(const simox::json::json& j, Handedness& arondto)
161 {
162 arondto.fromString(simox::alg::to_upper(j.get<std::string>()));
163 }
164
165 void
166 arondto::to_json(simox::json::json& j, const PhysicalData& arondto)
167 {
168 j["gender"] = arondto.gender;
169 j["handedness"] = arondto.handedness;
170 set_if_present(j, "birthday", arondto.birthday);
171 set_if_present(j, "heightInMillimeters", arondto.heightInMillimeters);
172 set_if_present(j, "weightInGrams", arondto.weightInGrams);
173 }
174
175 void
176 arondto::from_json(const simox::json::json& j, PhysicalData& arondto)
177 {
178 using namespace simox::json;
179
180 get_to_if_exists(j, "gender", arondto.gender, Gender::DIVERSE);
181 get_to_if_exists(j, "handedness", arondto.handedness, Handedness::BOTH);
182
183 arondto.birthday = opt_from_json<DateTime>(j, "birthday");
184 arondto.heightInMillimeters = opt_from_json<float>(j, "heightInMillimeters");
185 arondto.weightInGrams = opt_from_json<float>(j, "weightInGrams");
186 }
187
188 void
189 arondto::to_json(simox::json::json& j, const Abilities& arondto)
190 {
191 set_if_present(j, "canGraspLeft", arondto.canGraspLeft);
192 set_if_present(j, "canGraspRight", arondto.canGraspRight);
193 set_if_present(j, "canStand", arondto.canStand);
194 set_if_present(j, "canWalk", arondto.canWalk);
195 set_if_present(j, "isBlind", arondto.isBlind);
196 set_if_present(j, "isColorblind", arondto.isColorblind);
197 set_if_present(j, "isDeaf", arondto.isDeaf);
198 }
199
200 void
201 arondto::from_json(const simox::json::json& j, Abilities& arondto)
202 {
203 arondto.canGraspLeft = opt_from_json<bool>(j, "canGraspLeft");
204 arondto.canGraspRight = opt_from_json<bool>(j, "canGraspRight");
205 arondto.canStand = opt_from_json<bool>(j, "canStand");
206 arondto.canWalk = opt_from_json<bool>(j, "canWalk");
207 arondto.isBlind = opt_from_json<bool>(j, "isBlind");
208 arondto.isColorblind = opt_from_json<bool>(j, "isColorblind");
209 arondto.isDeaf = opt_from_json<bool>(j, "isDeaf");
210 }
211
212 void
213 arondto::to_json(simox::json::json& j, const Preference& arondto)
214 {
215 j["preferred"] = arondto.preferred;
216 j["disliked"] = arondto.disliked;
217 }
218
219 void
220 arondto::from_json(const simox::json::json& j, Preference& arondto)
221 {
222 using namespace simox::json;
223 get_to_if_exists(j, "preferred", arondto.preferred);
224 get_to_if_exists(j, "disliked", arondto.disliked);
225 }
226
227 void
228 arondto::to_json(simox::json::json& j, const Preferences& arondto)
229 {
230 set_if_present(j, "communicationStyle", arondto.communicationStyle);
231 set_if_present(j, "color", arondto.color);
232 set_if_present(j, "drink", arondto.drink);
233 set_if_present(j, "food", arondto.food);
234 set_if_present(j, "hobbies", arondto.hobbies);
235 set_if_present(j, "music", arondto.music);
236 set_if_present(j, "fashion", arondto.fashion);
237 set_if_present(j, "travel", arondto.travel);
238 set_if_present(j, "people", arondto.people);
239 set_if_present(j, "work", arondto.work);
240 set_if_present(j, "technology", arondto.technology);
241 set_if_present(j, "valuesAndBeliefs", arondto.valuesAndBeliefs);
242 }
243
244 void
245 arondto::from_json(const simox::json::json& j, Preferences& arondto)
246 {
247 arondto.communicationStyle = opt_from_json<Preference>(j, "communicationStyle");
248 arondto.color = opt_from_json<Preference>(j, "color");
249 arondto.drink = opt_from_json<Preference>(j, "drink");
250 arondto.food = opt_from_json<Preference>(j, "food");
251 arondto.hobbies = opt_from_json<Preference>(j, "hobbies");
252 arondto.music = opt_from_json<Preference>(j, "music");
253 arondto.fashion = opt_from_json<Preference>(j, "fashion");
254 arondto.travel = opt_from_json<Preference>(j, "travel");
255 arondto.people = opt_from_json<Preference>(j, "people");
256 arondto.work = opt_from_json<Preference>(j, "work");
257 arondto.technology = opt_from_json<Preference>(j, "technology");
258 arondto.valuesAndBeliefs = opt_from_json<Preference>(j, "valuesAndBeliefs");
259 }
260
261 void
262 arondto::to_json(simox::json::json& j, const PersonalRooms& arondto)
263 {
264 j["bedroom"] = arondto.bedroom;
265 }
266
267 void
268 arondto::from_json(const simox::json::json& j, PersonalRooms& arondto)
269 {
270 simox::json::get_to_if_exists(j, "bedroom", arondto.bedroom);
271 }
272
273} // namespace armarx::human
static void ConvertFromNlohmannJSON(data::VariantPtr &, const nlohmann::json &, const aron::type::VariantPtr &=nullptr)
static nlohmann::json ConvertToNlohmannJSON(const data::VariantPtr &)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::shared_ptr< Variant > VariantPtr
void to_json(simox::json::json &j, const Gender &arondto)
void from_json(const simox::json::json &j, Gender &arondto)
std::optional< ValueType > opt_from_json(const simox::json::json &j, const std::string key)
void set_if_present(simox::json::json &j, const std::string key, const std::optional< ValueType > optional)