MongoSerializer.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 MemoryX::CommonStorage
17 * @author Alexey Kozlov ( kozlov at kit dot edu)
18 * @date Sep 19, 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "MongoSerializer.h"
24 
26 
27 
28 using namespace memoryx;
29 
30 // NOTE: Mongo uses special datatype, "ObjectID" to store auto-generated document ids in collection
31 // It has a JSON representation which consists of 2 enclosed fields: _id : { $oid: { "<idstring>" } }
32 // For "natural" or user-defined ids this is not the case, they're stored as just _id: "idstring"
33 // Since MongoSerializer needs to support both id flavors and, more important, we store ids of either type
34 // as plain strings in C++ objects, some special processing is required
35 // See serializeMongoId()/deserializeMongoId functions for details
36 const std::string NATURAL_ID_FIELD_NAME = "_id";
37 const std::string MONGO_OBJECTID_FIELD_NAME = "_id.$oid";
38 
39 MongoSerializer::MongoSerializer(const Ice::CommunicatorPtr ic, bool useMongoObjectIds /* = false */) :
40  useMongoIds(useMongoObjectIds)
41 {
42  jsonSerializer = new armarx::JSONObject(ic);
43  jsonSerializer->setIdField(NATURAL_ID_FIELD_NAME);
44 }
45 
47  = default;
48 
49 DBStorableData MongoSerializer::serialize(const armarx::SerializablePtr& obj, const ::Ice::Current& c)
50 {
51  DBStorableData result;
52  armarx::JSONObjectPtr serializer = new armarx::JSONObject(*jsonSerializer);
53  serializer->reset();
54  obj->serialize(serializer);
55 
56  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
57  serializeMongoId(serializer);
58 
59  result.JSON = serializer->toString();
60  return result;
61 }
62 
63 void MongoSerializer::deserialize(const DBStorableData& objData, const armarx::SerializablePtr& obj, const ::Ice::Current& c)
64 {
65  armarx::JSONObjectPtr deserializer = new armarx::JSONObject(*jsonSerializer);
66 
67  deserializer->reset();
68  deserializer->fromString(objData.JSON);
69 
70  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
71  deserializeMongoId(deserializer);
72 
73  if (obj)
74  {
75  obj->deserialize(deserializer);
76  }
77 }
78 
79 DBStorableData MongoSerializer::serializeIceObject(const armarx::SerializablePtr& obj, const ::Ice::Current&)
80 {
81  DBStorableData result;
82  armarx::JSONObjectPtr serializer = new armarx::JSONObject(*jsonSerializer);
83  serializer->reset();
84  serializer->serializeIceObject(obj);
85 
86  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
87  serializeMongoId(serializer);
88 
89  result.JSON = serializer->toString();
90  return result;
91 }
92 
93 armarx::SerializablePtr MongoSerializer::deserializeIceObject(const DBStorableData& objData, const ::Ice::Current&)
94 {
95  if (objData.JSON.empty())
96  {
97  return armarx::SerializablePtr();
98  }
99 
100  armarx::JSONObjectPtr deserializer = new armarx::JSONObject(*jsonSerializer);
101  deserializer->reset();
102  deserializer->fromString(objData.JSON);
103 
104  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
105  deserializeMongoId(deserializer);
106 
107  return deserializer->deserializeIceObject();
108 }
109 
110 void MongoSerializer::serializeMongoId(const armarx::JSONObjectPtr& serializer)
111 {
112  if (useMongoIds && serializer->hasElement(serializer->getIdField()))
113  {
114  armarx::AbstractObjectSerializerPtr idElem = serializer->createElement();
115  idElem->setString("$oid", serializer->getStringId());
116  serializer->setElement(NATURAL_ID_FIELD_NAME, idElem);
117  }
118 }
119 
120 void MongoSerializer::deserializeMongoId(const armarx::JSONObjectPtr& deserializer)
121 {
122  if (useMongoIds && deserializer->hasElement(NATURAL_ID_FIELD_NAME))
123  {
124  deserializer->setString(NATURAL_ID_FIELD_NAME, deserializer->getString(MONGO_OBJECTID_FIELD_NAME));
125  }
126 }
127 
memoryx::MongoSerializer::serialize
DBStorableData serialize(const armarx::SerializablePtr &obj, const ::Ice::Current &=Ice::emptyCurrent) override
Takes a SerializablePtr and transforms it into a JSON object using the JSONObject serialization mecha...
Definition: MongoSerializer.cpp:49
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
JSONObject.h
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
MongoSerializer.h
memoryx::MongoSerializer::MongoSerializer
MongoSerializer(const Ice::CommunicatorPtr ic=Ice::CommunicatorPtr(), bool useMongoObjectIds=false)
Definition: MongoSerializer.cpp:39
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
IceInternal::Handle< ::Ice::Communicator >
memoryx::MongoSerializer::deserializeIceObject
armarx::SerializablePtr deserializeIceObject(const DBStorableData &objData, const ::Ice::Current &=Ice::emptyCurrent)
Deserializes the objData DBStorableData object into an instance of Serializable and returns it.
Definition: MongoSerializer.cpp:93
memoryx::MongoSerializer::~MongoSerializer
~MongoSerializer() override
memoryx::MongoSerializer::deserialize
void deserialize(const DBStorableData &objData, const armarx::SerializablePtr &obj, const ::Ice::Current &=Ice::emptyCurrent) override
Uses the JSON content of DBStorableDate objData and deserializes it into the obj pointer.
Definition: MongoSerializer.cpp:63
memoryx::MongoSerializer::serializeIceObject
DBStorableData serializeIceObject(const armarx::SerializablePtr &obj, const ::Ice::Current &=Ice::emptyCurrent)
Serialize an Ice object passed in the obj parameter.
Definition: MongoSerializer.cpp:79
MONGO_OBJECTID_FIELD_NAME
const std::string MONGO_OBJECTID_FIELD_NAME
Definition: MongoSerializer.cpp:37
NATURAL_ID_FIELD_NAME
const std::string NATURAL_ID_FIELD_NAME
Definition: MongoSerializer.cpp:36