diff --git a/src/oatpp-mongo/bson/Types.cpp b/src/oatpp-mongo/bson/Types.cpp index fc02c90..e1771c4 100644 --- a/src/oatpp-mongo/bson/Types.cpp +++ b/src/oatpp-mongo/bson/Types.cpp @@ -32,6 +32,7 @@ namespace __class { const ClassId InlineDocument::CLASS_ID("oatpp::mongo::InlineDocument"); const ClassId InlineArray::CLASS_ID("oatpp::mongo::InlineArray"); const ClassId ObjectId::CLASS_ID("oatpp::mongo::ObjectId"); + const ClassId DateTime::CLASS_ID("oatpp::mongo::DateTime"); } diff --git a/src/oatpp-mongo/bson/Types.hpp b/src/oatpp-mongo/bson/Types.hpp index 1e0fab6..4df0d73 100644 --- a/src/oatpp-mongo/bson/Types.hpp +++ b/src/oatpp-mongo/bson/Types.hpp @@ -187,6 +187,17 @@ namespace __class { }; + class DateTime { + public: + static const ClassId CLASS_ID; + + static Type *getType() { + static Type type(CLASS_ID, nullptr); + return &type; + } + + }; + } /** @@ -206,6 +217,11 @@ typedef oatpp::data::mapping::type::ObjectWrapper ObjectId; +/** + * DateTime is an ObjectWrapper over `v_int64` and __class::DateTime. + */ +typedef oatpp::data::mapping::type::Primitive DateTime; + }}} #endif // oatpp_mongo_bson_Types_hpp diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.cpp b/src/oatpp-mongo/bson/mapping/Deserializer.cpp index 1251a36..42ea361 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.cpp @@ -74,6 +74,8 @@ Deserializer::Deserializer(const std::shared_ptr& config) setDeserializerMethod(oatpp::mongo::bson::__class::ObjectId::CLASS_ID, &Deserializer::deserializeObjectId); + setDeserializerMethod(oatpp::mongo::bson::__class::DateTime::CLASS_ID, &Deserializer::deserializeDateTime); + } void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method) { @@ -199,6 +201,29 @@ oatpp::Void Deserializer::deserializeBoolean(Deserializer* deserializer, } +oatpp::Void Deserializer::deserializeDateTime(Deserializer* deserializer, + parser::Caret& caret, + const Type* const type, + v_char8 bsonTypeCode) +{ + (void) deserializer; + (void) type; + + switch(bsonTypeCode) { + + case TypeCode::NULL_VALUE: + return oatpp::Void(DateTime::Class::getType()); + + case TypeCode::DATE_TIME: + return DateTime(Utils::readInt64(caret)); + + default: + caret.setError("[oatpp::mongo::bson::mapping::Deserializer::deserializeDateTime()]: Error. Type-code doesn't match DateTime."); + return oatpp::Void(DateTime::Class::getType()); + + } +} + oatpp::Void Deserializer::deserializeString(Deserializer* deserializer, parser::Caret& caret, const Type* const type, diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.hpp b/src/oatpp-mongo/bson/mapping/Deserializer.hpp index e97e270..646d75e 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.hpp @@ -268,6 +268,7 @@ class Deserializer { } static oatpp::Void deserializeBoolean(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode); + static oatpp::Void deserializeDateTime(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode); static oatpp::Void deserializeString(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode); static oatpp::Void deserializeInlineDocs(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode); diff --git a/src/oatpp-mongo/bson/mapping/Serializer.cpp b/src/oatpp-mongo/bson/mapping/Serializer.cpp index fe55ba2..e241145 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.cpp @@ -75,6 +75,8 @@ Serializer::Serializer(const std::shared_ptr& config) setSerializerMethod(oatpp::mongo::bson::__class::ObjectId::CLASS_ID, &Serializer::serializeObjectId); + setSerializerMethod(oatpp::mongo::bson::__class::DateTime::CLASS_ID, &Serializer::serializeDateTime); + } void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) { @@ -86,6 +88,25 @@ void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId } } +void Serializer::serializeDateTime(Serializer* serializer, + data::stream::ConsistentOutputStream* stream, + const data::share::StringKeyLabel& key, + const oatpp::Void& polymorph) +{ + (void) serializer; + + if(!key) { + throw std::runtime_error("[oatpp::mongo::bson::mapping::Serializer::serializeDateTime()]: Error. The key can't be null."); + } + + if(polymorph) { + bson::Utils::writeKey(stream, TypeCode::DATE_TIME, key); + bson::Utils::writeInt64(stream, *static_cast(polymorph.get())); + } else { + bson::Utils::writeKey(stream, TypeCode::NULL_VALUE, key); + } +} + void Serializer::serializeString(Serializer* serializer, data::stream::ConsistentOutputStream* stream, const data::share::StringKeyLabel& key, diff --git a/src/oatpp-mongo/bson/mapping/Serializer.hpp b/src/oatpp-mongo/bson/mapping/Serializer.hpp index 0827ae4..0dcbf9f 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.hpp @@ -104,7 +104,6 @@ class Serializer { } if(polymorph) { - auto str = static_cast(polymorph.get()); bson::Utils::writePrimitive(stream, key, * static_cast(polymorph.get())); } else { bson::Utils::writeKey(stream, TypeCode::NULL_VALUE, key); @@ -181,6 +180,11 @@ class Serializer { } + static void serializeDateTime(Serializer* serializer, + data::stream::ConsistentOutputStream* stream, + const data::share::StringKeyLabel& key, + const oatpp::Void& polymorph); + static void serializeString(Serializer* serializer, data::stream::ConsistentOutputStream* stream, const data::share::StringKeyLabel& key,