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