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 
40  bool useMongoObjectIds /* = false */) :
41  useMongoIds(useMongoObjectIds)
42 {
43  jsonSerializer = new armarx::JSONObject(ic);
44  jsonSerializer->setIdField(NATURAL_ID_FIELD_NAME);
45 }
46 
48 
49 DBStorableData
50 MongoSerializer::serialize(const armarx::SerializablePtr& obj, const ::Ice::Current& c)
51 {
52  DBStorableData result;
53  armarx::JSONObjectPtr serializer = new armarx::JSONObject(*jsonSerializer);
54  serializer->reset();
55  obj->serialize(serializer);
56 
57  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
58  serializeMongoId(serializer);
59 
60  result.JSON = serializer->toString();
61  return result;
62 }
63 
64 void
65 MongoSerializer::deserialize(const DBStorableData& objData,
66  const armarx::SerializablePtr& obj,
67  const ::Ice::Current& c)
68 {
69  armarx::JSONObjectPtr deserializer = new armarx::JSONObject(*jsonSerializer);
70 
71  deserializer->reset();
72  deserializer->fromString(objData.JSON);
73 
74  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
75  deserializeMongoId(deserializer);
76 
77  if (obj)
78  {
79  obj->deserialize(deserializer);
80  }
81 }
82 
83 DBStorableData
84 MongoSerializer::serializeIceObject(const armarx::SerializablePtr& obj, const ::Ice::Current&)
85 {
86  DBStorableData result;
87  armarx::JSONObjectPtr serializer = new armarx::JSONObject(*jsonSerializer);
88  serializer->reset();
89  serializer->serializeIceObject(obj);
90 
91  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
92  serializeMongoId(serializer);
93 
94  result.JSON = serializer->toString();
95  return result;
96 }
97 
98 armarx::SerializablePtr
99 MongoSerializer::deserializeIceObject(const DBStorableData& objData, const ::Ice::Current&)
100 {
101  if (objData.JSON.empty())
102  {
103  return armarx::SerializablePtr();
104  }
105 
106  armarx::JSONObjectPtr deserializer = new armarx::JSONObject(*jsonSerializer);
107  deserializer->reset();
108  deserializer->fromString(objData.JSON);
109 
110  // special handling for mongo ids, since they need to be saved as _id : { $oid: { "<idstring>" } }
111  deserializeMongoId(deserializer);
112 
113  return deserializer->deserializeIceObject();
114 }
115 
116 void
117 MongoSerializer::serializeMongoId(const armarx::JSONObjectPtr& serializer)
118 {
119  if (useMongoIds && serializer->hasElement(serializer->getIdField()))
120  {
121  armarx::AbstractObjectSerializerPtr idElem = serializer->createElement();
122  idElem->setString("$oid", serializer->getStringId());
123  serializer->setElement(NATURAL_ID_FIELD_NAME, idElem);
124  }
125 }
126 
127 void
128 MongoSerializer::deserializeMongoId(const armarx::JSONObjectPtr& deserializer)
129 {
130  if (useMongoIds && deserializer->hasElement(NATURAL_ID_FIELD_NAME))
131  {
132  deserializer->setString(NATURAL_ID_FIELD_NAME,
133  deserializer->getString(MONGO_OBJECTID_FIELD_NAME));
134  }
135 }
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:50
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:46
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:99
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:65
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:84
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