JsonSimoxShapeSerializer.cpp
Go to the documentation of this file.
2 
3 #ifdef MEMORYX_FOUND
4 
5 #include <SimoxUtility/json.h>
6 #include <VirtualRobot/ManipulationObject.h>
7 
8 #include "SimoxObjectShape.h"
10 #include <SemanticObjectRelations/Shapes.h>
11 #include <SemanticObjectRelations/Shapes/json.h>
12 
13 namespace armarx::semantic
14 {
15  JsonSimoxShapeSerializer::JsonSimoxShapeSerializer(
16  const memoryx::ObjectClassSegmentWrapper* classSegment) :
17  classSegment(classSegment)
18  {
19  }
20 
21  void
22  JsonSimoxShapeSerializer::to_json(nlohmann::json& j, const SimoxObjectShape& object) const
23  {
24  semrel::json::to_json_base(j, object);
25 
26  j["class"] = object.objectClassName;
27  j["instance"] = object.object->getName();
28  j["entityId"] = object.entityId;
29  }
30 
31  void
32  JsonSimoxShapeSerializer::from_json(const nlohmann::json& j, SimoxObjectShape& object) const
33  {
34  const std::string objectClassName = j.at("class").get<std::string>();
35 
36  VirtualRobot::ManipulationObjectPtr manipulationObject;
37  if (classSegment)
38  {
39  std::optional<memoryx::ObjectClassWrapper> objectClass =
40  classSegment->getClass(objectClassName);
41  if (objectClass)
42  {
43  const std::string instance = j.at("instance").get<std::string>();
44  manipulationObject = objectClass->manipulationObject->clone(instance);
45  }
46  }
47 
48  object = SimoxObjectShape(manipulationObject, objectClassName);
50  object.entityId = j.at("entityId");
51  }
52 
53  void
54  JsonSimoxShapeSerializer::registerSerializer(
55  const std::shared_ptr<JsonSimoxShapeSerializer>& instance,
56  bool overwrite)
57  {
58  semrel::json::ShapeSerializers::registerSerializer<SimoxObjectShape>(
59  [instance](nlohmann::json& j, const SimoxObjectShape& object)
60  { instance->to_json(j, object); },
61  [instance](const nlohmann::json& j, SimoxObjectShape& object)
62  { instance->from_json(j, object); },
63  overwrite);
64  }
65 
66 } // namespace armarx::semantic
67 
68 #endif
ObjectClassSegmentWrapper.h
armarx::viz::json::to_json_base
void to_json_base(nlohmann::json &j, const data::Element &element)
Definition: json_base.cpp:86
memoryx::ObjectClassSegmentWrapper
Definition: ObjectClassSegmentWrapper.h:23
armarx::data::from_json
void from_json(const nlohmann::json &j, PackagePath &pp)
Definition: json_conversions.h:45
JsonSimoxShapeSerializer.h
armarx::data::to_json
void to_json(nlohmann::json &j, const PackagePath &pp)
Definition: json_conversions.h:36
armarx::semantic
Definition: ShapesSupportRelations.cpp:32
SimoxObjectShape.h
armarx::viz::json::from_json_base
void from_json_base(const nlohmann::json &j, data::Element &value)
Definition: json_base.cpp:98