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
13namespace 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);
49 semrel::json::from_json_base(j, object);
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