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 
33 
37 
38 #include <VisionX/libraries/armem_human/aron/Profile.aron.generated.h>
39 
40 namespace armarx::human
41 {
42 
43  void
44  arondto::to_json(simox::json::json& j, const Profile& arondto)
45  {
46  j["firstName"] = arondto.firstName;
47  j["lastName"] = arondto.lastName;
48  j["names"] = arondto.names;
49  j["gender"] = arondto.gender;
50  j["handedness"] = arondto.handedness;
51  {
52  DateTime birthday;
53  birthday = arondto.birthday;
54  j["birthday"] = birthday;
55  }
56  j["heightInMillimeters"] = arondto.heightInMillimeters;
57  j["weightInGrams"] = arondto.weightInGrams;
58  {
59  simox::Color favouriteColor;
60  simox::fromAron(arondto.favouriteColor, favouriteColor);
61  j["favouriteColor"] = favouriteColor;
62  }
63 
64  j["roles"] = arondto.roles;
65 
66  if (arondto.attributes)
67  {
69  arondto.attributes);
70  }
71  else
72  {
73  j["attributes"] = simox::json::json::object();
74  }
75  }
76 
77  void
78  arondto::from_json(const simox::json::json& j, Profile& arondto)
79  {
80  using namespace simox::json;
81 
82  get_to_if_exists(j, "firstName", arondto.firstName);
83  get_to_if_exists(j, "lastName", arondto.lastName);
84 
85  get_to_if_exists(j, "names", arondto.names);
86 
87  get_to_if_exists(j, "gender", arondto.gender, Gender::DIVERSE);
88  get_to_if_exists(j, "handedness", arondto.handedness, Handedness::BOTH);
89  {
90  DateTime birthday;
91  get_to_if_exists(j, "birthday", birthday, DateTime::Invalid());
92  arondto.birthday = birthday;
93  }
94  get_to_if_exists(j, "heightInMillimeters", arondto.heightInMillimeters, -1);
95  get_to_if_exists(j, "weightInGrams", arondto.weightInGrams, -1);
96 
97  {
98  simox::Color favouriteColor;
99  get_to_if_exists(j, "favouriteColor", favouriteColor, simox::Color::gray());
100  simox::toAron(arondto.favouriteColor, favouriteColor);
101  }
102 
103  get_to_if_exists(j, "roles", arondto.roles);
104 
105  if (j.count("attributes"))
106  {
107  aron::data::VariantPtr data = nullptr;
109  j.at("attributes"));
110  if (auto dict = std::dynamic_pointer_cast<aron::data::Dict>(data))
111  {
112  arondto.attributes = dict;
113  }
114  else
115  {
116  arondto.attributes = nullptr;
117  }
118  }
119  else
120  {
121  arondto.attributes = nullptr;
122  }
123  }
124 
125  void
126  arondto::to_json(simox::json::json& j, const Gender& arondto)
127  {
128  j = simox::alg::to_lower(arondto.toString());
129  }
130 
131  void
132  arondto::from_json(const simox::json::json& j, Gender& arondto)
133  {
134  arondto.fromString(simox::alg::to_upper(j.get<std::string>()));
135  }
136 
137  void
138  arondto::to_json(simox::json::json& j, const Handedness& arondto)
139  {
140  j = simox::alg::to_lower(arondto.toString());
141  }
142 
143  void
144  arondto::from_json(const simox::json::json& j, Handedness& arondto)
145  {
146  arondto.fromString(simox::alg::to_upper(j.get<std::string>()));
147  }
148 } // namespace armarx::human
armarx::human::arondto::from_json
void from_json(const simox::json::json &j, Gender &arondto)
Definition: json_conversions.cpp:132
armarx::Profile
Definition: ArvizProfileManagerWidget.h:58
armarx::aron::data::converter::AronNlohmannJSONConverter::ConvertToNlohmannJSON
static nlohmann::json ConvertToNlohmannJSON(const data::VariantPtr &)
Definition: NLohmannJSONConverter.cpp:10
DateTime.h
ice_conversions.h
armarx::human::arondto::to_json
void to_json(simox::json::json &j, const Gender &arondto)
Definition: json_conversions.cpp:126
NLohmannJSONConverter.h
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
Color
uint32_t Color
RGBA color.
Definition: color.h:8
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
simox.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::human
Definition: forward_declarations.h:25
armarx::fromAron
void fromAron(const arondto::PackagePath &dto, PackageFileLocation &bo)
armarx::aron::data::converter::AronNlohmannJSONConverter::ConvertFromNlohmannJSON
static void ConvertFromNlohmannJSON(data::VariantPtr &, const nlohmann::json &, const aron::type::VariantPtr &=nullptr)
Definition: NLohmannJSONConverter.cpp:36
json_conversions.h
json_conversions.h
armarx::toAron
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:57