json::mapping::ObjectMapper. Support for unsigned int types.

This commit is contained in:
lganzzzo 2020-01-25 01:26:35 +07:00
parent aa61aa7a44
commit f65798957e
10 changed files with 115 additions and 35 deletions

View File

@ -75,9 +75,13 @@ class Object : public oatpp::base::Countable {
public:
typedef oatpp::data::mapping::type::String String;
typedef oatpp::data::mapping::type::Int8 Int8;
typedef oatpp::data::mapping::type::UInt8 UInt8;
typedef oatpp::data::mapping::type::Int16 Int16;
typedef oatpp::data::mapping::type::UInt16 UInt16;
typedef oatpp::data::mapping::type::Int32 Int32;
typedef oatpp::data::mapping::type::UInt32 UInt32;
typedef oatpp::data::mapping::type::Int64 Int64;
typedef oatpp::data::mapping::type::UInt64 UInt64;
typedef oatpp::data::mapping::type::Float32 Float32;
typedef oatpp::data::mapping::type::Float64 Float64;
typedef oatpp::data::mapping::type::Boolean Boolean;

View File

@ -347,10 +347,10 @@ v_int64 Caret::StateSaveGuard::getSavedErrorCode() {
return skipped;
}
long int Caret::parseInt(int base) {
v_int64 Caret::parseInt(int base) {
char* end;
char* start = (char*)&m_data[m_pos];
long int result = std::strtol(start, &end, base);
v_int64 result = (v_int64)std::strtoll(start, &end, base);
if(start == end){
m_errorMessage = ERROR_INVALID_INTEGER;
}
@ -358,10 +358,10 @@ v_int64 Caret::StateSaveGuard::getSavedErrorCode() {
return result;
}
unsigned long int Caret::parseUnsignedInt(int base) {
v_uint64 Caret::parseUnsignedInt(int base) {
char* end;
char* start = (char*)&m_data[m_pos];
long int result = std::strtoul(start, &end, base);
v_uint64 result = (v_uint64)std::strtoull(start, &end, base);
if(start == end){
m_errorMessage = ERROR_INVALID_INTEGER;
}

View File

@ -348,7 +348,7 @@ public:
* @param base - base is passed to std::strtol function
* @return parsed value
*/
long int parseInt(int base = 10);
v_int64 parseInt(int base = 10);
/**
* parse integer value starting from the current position.
@ -359,7 +359,7 @@ public:
* @param base - base is passed to std::strtoul function
* @return parsed value
*/
unsigned long int parseUnsignedInt(int base = 10);
v_uint64 parseUnsignedInt(int base = 10);
/**
* parse float value starting from the current position.

View File

@ -36,10 +36,19 @@ Deserializer::Deserializer(const std::shared_ptr<Config>& config)
m_methods.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
setDeserializerMethod(oatpp::data::mapping::type::__class::String::CLASS_ID, &Deserializer::deserializeString);
setDeserializerMethod(oatpp::data::mapping::type::__class::Int8::CLASS_ID, &Deserializer::deserializeInt<oatpp::Int8>);
setDeserializerMethod(oatpp::data::mapping::type::__class::UInt8::CLASS_ID, &Deserializer::deserializeUInt<oatpp::UInt8>);
setDeserializerMethod(oatpp::data::mapping::type::__class::Int16::CLASS_ID, &Deserializer::deserializeInt<oatpp::Int16>);
setDeserializerMethod(oatpp::data::mapping::type::__class::UInt16::CLASS_ID, &Deserializer::deserializeUInt<oatpp::UInt16>);
setDeserializerMethod(oatpp::data::mapping::type::__class::Int32::CLASS_ID, &Deserializer::deserializeInt<oatpp::Int32>);
setDeserializerMethod(oatpp::data::mapping::type::__class::UInt32::CLASS_ID, &Deserializer::deserializeUInt<oatpp::UInt32>);
setDeserializerMethod(oatpp::data::mapping::type::__class::Int64::CLASS_ID, &Deserializer::deserializeInt<oatpp::Int64>);
setDeserializerMethod(oatpp::data::mapping::type::__class::UInt64::CLASS_ID, &Deserializer::deserializeUInt<oatpp::UInt64>);
setDeserializerMethod(oatpp::data::mapping::type::__class::Float32::CLASS_ID, &Deserializer::deserializeFloat32);
setDeserializerMethod(oatpp::data::mapping::type::__class::Float64::CLASS_ID, &Deserializer::deserializeFloat64);
setDeserializerMethod(oatpp::data::mapping::type::__class::Boolean::CLASS_ID, &Deserializer::deserializeBoolean);

View File

@ -149,6 +149,20 @@ private:
}
template<class T>
static AbstractObjectWrapper deserializeUInt(Deserializer* deserializer, parser::Caret& caret, const Type* const type){
(void) deserializer;
(void) type;
if(caret.isAtText("null", true)){
return AbstractObjectWrapper(T::Class::getType());
} else {
return AbstractObjectWrapper(T::ObjectType::createAbstract((typename T::ObjectType::ValueType) caret.parseUnsignedInt()), T::ObjectWrapper::Class::getType());
}
}
static AbstractObjectWrapper deserializeFloat32(Deserializer* deserializer, parser::Caret& caret, const Type* const type);
static AbstractObjectWrapper deserializeFloat64(Deserializer* deserializer, parser::Caret& caret, const Type* const type);

View File

@ -26,13 +26,18 @@
namespace oatpp { namespace parser { namespace json { namespace mapping {
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig,
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig)
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer::Config>& serializerConfig,
const std::shared_ptr<Deserializer::Config>& deserializerConfig)
: data::mapping::ObjectMapper(getMapperInfo())
, m_serializer(std::make_shared<Serializer>(pSerializerConfig))
, m_deserializer(std::make_shared<Deserializer>(pDeserializerConfig))
, serializerConfig(pSerializerConfig)
, deserializerConfig(pDeserializerConfig)
, m_serializer(std::make_shared<Serializer>(serializerConfig))
, m_deserializer(std::make_shared<Deserializer>(deserializerConfig))
{}
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer>& serializer,
const std::shared_ptr<Deserializer>& deserializer)
: data::mapping::ObjectMapper(getMapperInfo())
, m_serializer(serializer)
, m_deserializer(deserializer)
{}
std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<Serializer::Config>& serializerConfig,
@ -40,9 +45,14 @@ std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<S
return std::make_shared<ObjectMapper>(serializerConfig, deserializerConfig);
}
std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<Serializer>& serializer,
const std::shared_ptr<Deserializer>& deserializer){
return std::make_shared<ObjectMapper>(serializer, deserializer);
}
void ObjectMapper::write(data::stream::ConsistentOutputStream* stream,
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const {
m_serializer->serialize(stream, variant);
m_serializer->serializeToStream(stream, variant);
}
oatpp::data::mapping::type::AbstractObjectWrapper ObjectMapper::read(oatpp::parser::Caret& caret,
@ -50,4 +60,12 @@ oatpp::data::mapping::type::AbstractObjectWrapper ObjectMapper::read(oatpp::pars
return m_deserializer->deserialize(caret, type);
}
std::shared_ptr<Serializer> ObjectMapper::getSerializer() {
return m_serializer;
}
std::shared_ptr<Deserializer> ObjectMapper::getDeserializer() {
return m_deserializer;
}
}}}}

View File

@ -49,11 +49,19 @@ private:
public:
/**
* Constructor.
* @param pSerializerConfig - &id:oatpp::parser::json::mapping::Serializer::Config;.
* @param pDeserializerConfig - &id:oatpp::parser::json::mapping::Deserializer::Config;.
* @param serializerConfig - &id:oatpp::parser::json::mapping::Serializer::Config;.
* @param deserializerConfig - &id:oatpp::parser::json::mapping::Deserializer::Config;.
*/
ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig = Serializer::Config::createShared(),
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig = Deserializer::Config::createShared());
ObjectMapper(const std::shared_ptr<Serializer::Config>& serializerConfig,
const std::shared_ptr<Deserializer::Config>& deserializerConfig);
/**
* Constructor.
* @param serializer
* @param deserializer
*/
ObjectMapper(const std::shared_ptr<Serializer>& serializer = std::make_shared<Serializer>(),
const std::shared_ptr<Deserializer>& deserializer = std::make_shared<Deserializer>());
public:
/**
@ -63,8 +71,18 @@ public:
* @return - `std::shared_ptr` to ObjectMapper.
*/
static std::shared_ptr<ObjectMapper>
createShared(const std::shared_ptr<Serializer::Config>& serializerConfig = Serializer::Config::createShared(),
const std::shared_ptr<Deserializer::Config>& deserializerConfig = Deserializer::Config::createShared());
createShared(const std::shared_ptr<Serializer::Config>& serializerConfig,
const std::shared_ptr<Deserializer::Config>& deserializerConfig);
/**
* Create shared ObjectMapper.
* @param serializer
* @param deserializer
* @return
*/
static std::shared_ptr<ObjectMapper>
createShared(const std::shared_ptr<Serializer>& serializer = std::make_shared<Serializer>(),
const std::shared_ptr<Deserializer>& deserializer = std::make_shared<Deserializer>());
/**
* Implementation of &id:oatpp::data::mapping::ObjectMapper::write;.
@ -83,15 +101,18 @@ public:
oatpp::data::mapping::type::AbstractObjectWrapper read(oatpp::parser::Caret& caret,
const oatpp::data::mapping::type::Type* const type) const override;
/**
* Serializer config.
*/
std::shared_ptr<Serializer::Config> serializerConfig;
/**
* Deserializer config.
* Get serializer.
* @return
*/
std::shared_ptr<Deserializer::Config> deserializerConfig;
std::shared_ptr<Serializer> getSerializer();
/**
* Get deserializer.
* @return
*/
std::shared_ptr<Deserializer> getDeserializer();
};

View File

@ -35,10 +35,19 @@ Serializer::Serializer(const std::shared_ptr<Config>& config)
m_methods.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
setSerializerMethod(oatpp::data::mapping::type::__class::String::CLASS_ID, &Serializer::serializeString);
setSerializerMethod(oatpp::data::mapping::type::__class::Int8::CLASS_ID, &Serializer::serializePrimitive<oatpp::Int8>);
setSerializerMethod(oatpp::data::mapping::type::__class::UInt8::CLASS_ID, &Serializer::serializePrimitive<oatpp::UInt8>);
setSerializerMethod(oatpp::data::mapping::type::__class::Int16::CLASS_ID, &Serializer::serializePrimitive<oatpp::Int16>);
setSerializerMethod(oatpp::data::mapping::type::__class::UInt16::CLASS_ID, &Serializer::serializePrimitive<oatpp::UInt16>);
setSerializerMethod(oatpp::data::mapping::type::__class::Int32::CLASS_ID, &Serializer::serializePrimitive<oatpp::Int32>);
setSerializerMethod(oatpp::data::mapping::type::__class::UInt32::CLASS_ID, &Serializer::serializePrimitive<oatpp::UInt32>);
setSerializerMethod(oatpp::data::mapping::type::__class::Int64::CLASS_ID, &Serializer::serializePrimitive<oatpp::Int64>);
setSerializerMethod(oatpp::data::mapping::type::__class::UInt64::CLASS_ID, &Serializer::serializePrimitive<oatpp::UInt64>);
setSerializerMethod(oatpp::data::mapping::type::__class::Float32::CLASS_ID, &Serializer::serializePrimitive<oatpp::Float32>);
setSerializerMethod(oatpp::data::mapping::type::__class::Float64::CLASS_ID, &Serializer::serializePrimitive<oatpp::Float64>);
setSerializerMethod(oatpp::data::mapping::type::__class::Boolean::CLASS_ID, &Serializer::serializePrimitive<oatpp::Boolean>);
@ -187,6 +196,17 @@ void Serializer::serialize(data::stream::ConsistentOutputStream* stream,
}
}
void Serializer::serializeToStream(data::stream::ConsistentOutputStream* stream,
const data::mapping::type::AbstractObjectWrapper& polymorph)
{
if(m_config->useBeautifier) {
json::Beautifier beautifier(stream, " ", "\n");
serialize(&beautifier, polymorph);
} else {
serialize(stream, polymorph);
}
}
const std::shared_ptr<Serializer::Config>& Serializer::getConfig() {
return m_config;
}

View File

@ -143,6 +143,9 @@ private:
static void serializeObject(Serializer* serializer,
data::stream::ConsistentOutputStream* stream,
const data::mapping::type::AbstractObjectWrapper& polymorph);
void serialize(data::stream::ConsistentOutputStream* stream, const data::mapping::type::AbstractObjectWrapper& polymorph);
private:
std::shared_ptr<Config> m_config;
std::vector<SerializerMethod> m_methods;
@ -152,7 +155,7 @@ public:
void setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method);
void serialize(data::stream::ConsistentOutputStream* stream, const data::mapping::type::AbstractObjectWrapper& polymorph);
void serializeToStream(data::stream::ConsistentOutputStream* stream, const data::mapping::type::AbstractObjectWrapper& polymorph);
const std::shared_ptr<Config>& getConfig();

View File

@ -80,15 +80,6 @@ void DTOMapperPerfTest::onRun() {
auto test1_Text = mapper->writeToString(test1);
OATPP_LOGV(TAG, "json='%s'", (const char*) test1_Text->getData());
{
PerformanceChecker checker("Serializer");
for(v_int32 i = 0; i < numIterations; i ++) {
oatpp::data::stream::BufferOutputStream stream;
serializer2->serialize(&stream, test1);
stream.toString();
}
}
{
PerformanceChecker checker("Serializer");
for(v_int32 i = 0; i < numIterations; i ++) {