mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
json::ObjectMapper. New configurable Serializer.
This commit is contained in:
parent
e457d14765
commit
3c5c0d8f67
@ -232,7 +232,7 @@ add_library(oatpp
|
||||
oatpp/web/url/mapping/Pattern.cpp
|
||||
oatpp/web/url/mapping/Pattern.hpp
|
||||
oatpp/web/url/mapping/Router.hpp
|
||||
)
|
||||
oatpp/parser/json/mapping/Serializer2.cpp oatpp/parser/json/mapping/Serializer2.hpp)
|
||||
|
||||
set_target_properties(oatpp PROPERTIES
|
||||
CXX_STANDARD 11
|
||||
|
@ -24,6 +24,8 @@
|
||||
|
||||
#include "ObjectMapper.hpp"
|
||||
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
|
||||
namespace oatpp { namespace data { namespace mapping {
|
||||
|
||||
ObjectMapper::ObjectMapper(const Info& info)
|
||||
@ -35,9 +37,9 @@ const ObjectMapper::Info& ObjectMapper::getInfo() const {
|
||||
}
|
||||
|
||||
oatpp::String ObjectMapper::writeToString(const type::AbstractObjectWrapper& variant) const {
|
||||
auto stream = stream::ChunkedBuffer::createShared();
|
||||
write(stream, variant);
|
||||
return stream->toString();
|
||||
stream::BufferOutputStream stream;
|
||||
write(&stream, variant);
|
||||
return stream.toString();
|
||||
}
|
||||
|
||||
}}}
|
@ -29,7 +29,6 @@
|
||||
#include "type/Object.hpp"
|
||||
#include "type/Type.hpp"
|
||||
|
||||
#include "oatpp/core/data/stream/ChunkedBuffer.hpp"
|
||||
#include "oatpp/core/data/stream/Stream.hpp"
|
||||
|
||||
#include "oatpp/core/parser/Caret.hpp"
|
||||
@ -81,10 +80,10 @@ public:
|
||||
|
||||
/**
|
||||
* Serialize object to stream. Implement this method.
|
||||
* @param stream - &id:oatpp::data::stream::OutputStream; to serialize object to.
|
||||
* @param stream - &id:oatpp::data::stream::ConsistentOutputStream; to serialize object to.
|
||||
* @param variant - Object to serialize.
|
||||
*/
|
||||
virtual void write(const std::shared_ptr<oatpp::data::stream::ConsistentOutputStream>& stream,
|
||||
virtual void write(data::stream::ConsistentOutputStream* stream,
|
||||
const type::AbstractObjectWrapper& variant) const = 0;
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@
|
||||
namespace oatpp { namespace data { namespace mapping { namespace type {
|
||||
|
||||
namespace __class {
|
||||
const ClassId const AbstractList::CLASS_ID("List");
|
||||
const ClassId AbstractList::CLASS_ID("List");
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
@ -49,6 +49,10 @@ ClassId::ClassId(const char* pName, const char* pQualifier)
|
||||
, id(ID_COUNTER ++)
|
||||
{}
|
||||
|
||||
int ClassId::getClassCount() {
|
||||
return ID_COUNTER;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Type::Properties
|
||||
|
@ -38,6 +38,8 @@ class Type; // FWD
|
||||
class ClassId {
|
||||
private:
|
||||
static std::atomic_int ID_COUNTER;
|
||||
public:
|
||||
static int getClassCount();
|
||||
public:
|
||||
ClassId(const char* pName, const char* pQualifier = nullptr);
|
||||
const char* const name;
|
||||
|
@ -26,21 +26,22 @@
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig,
|
||||
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer2::Config>& pSerializerConfig,
|
||||
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig)
|
||||
: oatpp::data::mapping::ObjectMapper(getMapperInfo())
|
||||
: data::mapping::ObjectMapper(getMapperInfo())
|
||||
, m_serializer(std::make_shared<Serializer2>(pSerializerConfig))
|
||||
, serializerConfig(pSerializerConfig)
|
||||
, deserializerConfig(pDeserializerConfig)
|
||||
{}
|
||||
|
||||
std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<Serializer::Config>& serializerConfig,
|
||||
std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<Serializer2::Config>& serializerConfig,
|
||||
const std::shared_ptr<Deserializer::Config>& deserializerConfig){
|
||||
return std::make_shared<ObjectMapper>(serializerConfig, deserializerConfig);
|
||||
}
|
||||
|
||||
void ObjectMapper::write(const std::shared_ptr<oatpp::data::stream::ConsistentOutputStream>& stream,
|
||||
void ObjectMapper::write(data::stream::ConsistentOutputStream* stream,
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const {
|
||||
Serializer::serialize(stream, variant, serializerConfig);
|
||||
m_serializer->serialize(stream, variant);
|
||||
}
|
||||
|
||||
oatpp::data::mapping::type::AbstractObjectWrapper ObjectMapper::read(oatpp::parser::Caret& caret,
|
||||
|
@ -25,7 +25,7 @@
|
||||
#ifndef oatpp_parser_json_mapping_ObjectMapper_hpp
|
||||
#define oatpp_parser_json_mapping_ObjectMapper_hpp
|
||||
|
||||
#include "./Serializer.hpp"
|
||||
#include "./Serializer2.hpp"
|
||||
#include "./Deserializer.hpp"
|
||||
|
||||
#include "oatpp/core/data/mapping/ObjectMapper.hpp"
|
||||
@ -43,13 +43,15 @@ private:
|
||||
static Info info("application/json");
|
||||
return info;
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<Serializer2> m_serializer;
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param pSerializerConfig - &id:oatpp::parser::json::mapping::Serializer::Config;.
|
||||
* @param pDeserializerConfig - &id:oatpp::parser::json::mapping::Deserializer::Config;.
|
||||
*/
|
||||
ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig = Serializer::Config::createShared(),
|
||||
ObjectMapper(const std::shared_ptr<Serializer2::Config>& pSerializerConfig = Serializer2::Config::createShared(),
|
||||
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig = Deserializer::Config::createShared());
|
||||
public:
|
||||
|
||||
@ -60,7 +62,7 @@ public:
|
||||
* @return - `std::shared_ptr` to ObjectMapper.
|
||||
*/
|
||||
static std::shared_ptr<ObjectMapper>
|
||||
createShared(const std::shared_ptr<Serializer::Config>& serializerConfig = Serializer::Config::createShared(),
|
||||
createShared(const std::shared_ptr<Serializer2::Config>& serializerConfig = Serializer2::Config::createShared(),
|
||||
const std::shared_ptr<Deserializer::Config>& deserializerConfig = Deserializer::Config::createShared());
|
||||
|
||||
/**
|
||||
@ -68,7 +70,7 @@ public:
|
||||
* @param stream - stream to write serializerd data to &id:oatpp::data::stream::ConsistentOutputStream;.
|
||||
* @param variant - object to serialize &id:oatpp::data::mapping::type::AbstractObjectWrapper;.
|
||||
*/
|
||||
void write(const std::shared_ptr<oatpp::data::stream::ConsistentOutputStream>& stream,
|
||||
void write(data::stream::ConsistentOutputStream* stream,
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const override;
|
||||
|
||||
/**
|
||||
@ -83,7 +85,7 @@ public:
|
||||
/**
|
||||
* Serializer config.
|
||||
*/
|
||||
std::shared_ptr<Serializer::Config> serializerConfig;
|
||||
std::shared_ptr<Serializer2::Config> serializerConfig;
|
||||
|
||||
/**
|
||||
* Deserializer config.
|
||||
|
194
src/oatpp/parser/json/mapping/Serializer2.cpp
Normal file
194
src/oatpp/parser/json/mapping/Serializer2.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#include "Serializer2.hpp"
|
||||
|
||||
#include "oatpp/parser/json/Utils.hpp"
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
Serializer2::Serializer2(const std::shared_ptr<Config>& config)
|
||||
: m_config(config)
|
||||
{
|
||||
|
||||
m_methods.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
|
||||
|
||||
m_methods[oatpp::data::mapping::type::__class::String::CLASS_ID.id] = &Serializer2::serializeString;
|
||||
m_methods[oatpp::data::mapping::type::__class::Int8::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Int8>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Int16::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Int16>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Int32::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Int32>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Int64::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Int64>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Float32::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Float32>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Float64::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Float64>;
|
||||
m_methods[oatpp::data::mapping::type::__class::Boolean::CLASS_ID.id] = &Serializer2::serializePrimitive<oatpp::Boolean>;
|
||||
|
||||
m_methods[oatpp::data::mapping::type::__class::AbstractObject::CLASS_ID.id] = &Serializer2::serializeObject;
|
||||
m_methods[oatpp::data::mapping::type::__class::AbstractList::CLASS_ID.id] = &Serializer2::serializeList;
|
||||
m_methods[oatpp::data::mapping::type::__class::AbstractListMap::CLASS_ID.id] = &Serializer2::serializeFieldsMap;
|
||||
|
||||
}
|
||||
|
||||
void Serializer2::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
|
||||
auto id = classId.id;
|
||||
if(id < m_methods.size()) {
|
||||
m_methods[id] = method;
|
||||
} else {
|
||||
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::setSerializerMethod()]: Error. Unknown classId");
|
||||
}
|
||||
}
|
||||
|
||||
void Serializer2::serializeString(oatpp::data::stream::ConsistentOutputStream* stream, p_char8 data, v_buff_size size) {
|
||||
auto encodedValue = Utils::escapeString(data, size, false);
|
||||
stream->writeCharSimple('\"');
|
||||
stream->writeSimple(encodedValue);
|
||||
stream->writeCharSimple('\"');
|
||||
}
|
||||
|
||||
void Serializer2::serializeString(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph)
|
||||
{
|
||||
|
||||
if(!polymorph) {
|
||||
stream->writeSimple("null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
auto str = static_cast<oatpp::base::StrBuffer*>(polymorph.get());
|
||||
|
||||
serializeString(stream, str->getData(), str->getSize());
|
||||
|
||||
}
|
||||
|
||||
void Serializer2::serializeList(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph)
|
||||
{
|
||||
|
||||
if(!polymorph) {
|
||||
stream->writeSimple("null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
auto* list = static_cast<AbstractList*>(polymorph.get());
|
||||
|
||||
stream->writeCharSimple('[');
|
||||
bool first = true;
|
||||
auto curr = list->getFirstNode();
|
||||
|
||||
while(curr != nullptr){
|
||||
auto value = curr->getData();
|
||||
if(value || serializer->getConfig()->includeNullFields) {
|
||||
(first) ? first = false : stream->writeSimple(",", 1);
|
||||
serializer->serialize(stream, curr->getData());
|
||||
}
|
||||
curr = curr->getNext();
|
||||
}
|
||||
|
||||
stream->writeCharSimple(']');
|
||||
|
||||
}
|
||||
|
||||
void Serializer2::serializeFieldsMap(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph)
|
||||
{
|
||||
|
||||
if(!polymorph) {
|
||||
stream->writeSimple("null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
auto map = static_cast<AbstractFieldsMap*>(polymorph.get());
|
||||
|
||||
stream->writeCharSimple('{');
|
||||
bool first = true;
|
||||
auto curr = map->getFirstEntry();
|
||||
|
||||
while(curr != nullptr){
|
||||
auto value = curr->getValue();
|
||||
if(value || serializer->getConfig()->includeNullFields) {
|
||||
(first) ? first = false : stream->writeSimple(",", 1);
|
||||
auto key = curr->getKey();
|
||||
serializeString(stream, key->getData(), key->getSize());
|
||||
stream->writeSimple(":", 1);
|
||||
serializer->serialize(stream, curr->getValue());
|
||||
}
|
||||
curr = curr->getNext();
|
||||
}
|
||||
|
||||
stream->writeCharSimple('}');
|
||||
|
||||
}
|
||||
|
||||
void Serializer2::serializeObject(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph)
|
||||
{
|
||||
|
||||
if(!polymorph) {
|
||||
stream->writeSimple("null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
stream->writeCharSimple('{');
|
||||
|
||||
bool first = true;
|
||||
auto fields = polymorph.valueType->properties->getList();
|
||||
Object* object = static_cast<Object*>(polymorph.get());
|
||||
|
||||
for (auto const& field : fields) {
|
||||
|
||||
auto value = field->get(object);
|
||||
if(value || serializer->getConfig()->includeNullFields) {
|
||||
(first) ? first = false : stream->writeSimple(",", 1);
|
||||
serializeString(stream, (p_char8)field->name, std::strlen(field->name));
|
||||
stream->writeSimple(":", 1);
|
||||
serializer->serialize(stream, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
stream->writeCharSimple('}');
|
||||
|
||||
}
|
||||
|
||||
void Serializer2::serialize(data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph)
|
||||
{
|
||||
auto id = polymorph.valueType->classId.id;
|
||||
auto& method = m_methods[id];
|
||||
if(method) {
|
||||
(*method)(this, stream, polymorph);
|
||||
} else {
|
||||
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serialize()]: "
|
||||
"Error. No serialize method for type '" + std::string(polymorph.valueType->classId.name) + "'");
|
||||
}
|
||||
}
|
||||
|
||||
const std::shared_ptr<Serializer2::Config>& Serializer2::getConfig() {
|
||||
return m_config;
|
||||
}
|
||||
|
||||
}}}}
|
166
src/oatpp/parser/json/mapping/Serializer2.hpp
Normal file
166
src/oatpp/parser/json/mapping/Serializer2.hpp
Normal file
@ -0,0 +1,166 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_parser_json_mapping_Serializer2_hpp
|
||||
#define oatpp_parser_json_mapping_Serializer2_hpp
|
||||
|
||||
#include "oatpp/parser/json/Beautifier.hpp"
|
||||
|
||||
#include "oatpp/core/data/mapping/type/ListMap.hpp"
|
||||
#include "oatpp/core/data/mapping/type/List.hpp"
|
||||
#include "oatpp/core/data/mapping/type/Object.hpp"
|
||||
#include "oatpp/core/data/mapping/type/Primitive.hpp"
|
||||
#include "oatpp/core/data/mapping/type/Type.hpp"
|
||||
#include "oatpp/core/data/stream/ChunkedBuffer.hpp"
|
||||
|
||||
#include "oatpp/core/parser/Caret.hpp"
|
||||
|
||||
#include "oatpp/core/collection/LinkedList.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
/**
|
||||
* Json Serializer.
|
||||
* Serializes oatpp DTO object to json. See [Data Transfer Object(DTO) component](https://oatpp.io/docs/components/dto/).
|
||||
*/
|
||||
class Serializer2 {
|
||||
public:
|
||||
typedef oatpp::data::mapping::type::Type Type;
|
||||
typedef oatpp::data::mapping::type::Type::Property Property;
|
||||
typedef oatpp::data::mapping::type::Type::Properties Properties;
|
||||
|
||||
typedef oatpp::data::mapping::type::Object Object;
|
||||
typedef oatpp::String String;
|
||||
|
||||
template<class T>
|
||||
using PolymorphicWrapper = data::mapping::type::PolymorphicWrapper<T>;
|
||||
|
||||
typedef oatpp::data::mapping::type::AbstractObjectWrapper AbstractObjectWrapper;
|
||||
typedef oatpp::data::mapping::type::List<AbstractObjectWrapper> AbstractList;
|
||||
typedef oatpp::data::mapping::type::ListMap<String, AbstractObjectWrapper> AbstractFieldsMap;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Serializer config.
|
||||
*/
|
||||
class Config : public oatpp::base::Countable {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Config()
|
||||
{}
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create shared config.
|
||||
* @return - `std::shared_ptr` to Config.
|
||||
*/
|
||||
static std::shared_ptr<Config> createShared(){
|
||||
return std::make_shared<Config>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Include fields with value == nullptr into serialized json.
|
||||
*/
|
||||
bool includeNullFields = true;
|
||||
|
||||
/**
|
||||
* If `true` - insert string `"<unknown-type>"` in json field value in case unknown field found.
|
||||
* Fail if `false`.
|
||||
* Known types for this serializer are:<br>
|
||||
* (String, Int8, Int16, Int32, Int64, Float32, Float64, Boolean, DTOs, List, Fields).
|
||||
*/
|
||||
bool throwOnUnknownTypes = true;
|
||||
|
||||
/**
|
||||
* Use JSON Beautifier.
|
||||
*/
|
||||
bool useBeautifier = false;
|
||||
|
||||
/**
|
||||
* Beautifier Indent.
|
||||
*/
|
||||
oatpp::String beautifierIndent = " ";
|
||||
|
||||
/**
|
||||
* Beautifier new line.
|
||||
*/
|
||||
oatpp::String beautifierNewLine = "\n";
|
||||
|
||||
};
|
||||
public:
|
||||
typedef void (*SerializerMethod)(Serializer2*,
|
||||
data::stream::ConsistentOutputStream*,
|
||||
const data::mapping::type::AbstractObjectWrapper&);
|
||||
private:
|
||||
|
||||
template<class T>
|
||||
static void serializePrimitive(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph){
|
||||
if(polymorph){
|
||||
stream->writeAsString(static_cast<typename T::ObjectType*>(polymorph.get())->getValue());
|
||||
} else {
|
||||
stream->writeSimple("null", 4);
|
||||
}
|
||||
}
|
||||
|
||||
static void serializeString(oatpp::data::stream::ConsistentOutputStream* stream, p_char8 data, v_buff_size size);
|
||||
static void serializeString(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph);
|
||||
|
||||
static void serializeList(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph);
|
||||
|
||||
static void serializeFieldsMap(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph);
|
||||
|
||||
static void serializeObject(Serializer2* serializer,
|
||||
data::stream::ConsistentOutputStream* stream,
|
||||
const data::mapping::type::AbstractObjectWrapper& polymorph);
|
||||
private:
|
||||
std::vector<SerializerMethod> m_methods;
|
||||
std::shared_ptr<Config> m_config;
|
||||
public:
|
||||
|
||||
Serializer2(const std::shared_ptr<Config>& config = std::make_shared<Config>());
|
||||
|
||||
void setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method);
|
||||
|
||||
void serialize(data::stream::ConsistentOutputStream* stream, const data::mapping::type::AbstractObjectWrapper& polymorph);
|
||||
|
||||
const std::shared_ptr<Config>& getConfig();
|
||||
|
||||
};
|
||||
|
||||
}}}}
|
||||
|
||||
#endif /* oatpp_parser_json_mapping_Serializer2_hpp */
|
@ -37,7 +37,7 @@ BodyDecoder::decodeToStringAsync(const Headers& headers, const std::shared_ptr<d
|
||||
const BodyDecoder* m_decoder;
|
||||
Headers m_headers;
|
||||
std::shared_ptr<oatpp::data::stream::InputStream> m_bodyStream;
|
||||
std::shared_ptr<oatpp::data::stream::ChunkedBuffer> m_chunkedBuffer;
|
||||
std::shared_ptr<oatpp::data::stream::BufferOutputStream> m_outputStream;
|
||||
public:
|
||||
|
||||
ToStringDecoder(const BodyDecoder* decoder,
|
||||
@ -46,15 +46,15 @@ BodyDecoder::decodeToStringAsync(const Headers& headers, const std::shared_ptr<d
|
||||
: m_decoder(decoder)
|
||||
, m_headers(headers)
|
||||
, m_bodyStream(bodyStream)
|
||||
, m_chunkedBuffer(data::stream::ChunkedBuffer::createShared())
|
||||
, m_outputStream(std::make_shared<data::stream::BufferOutputStream>())
|
||||
{}
|
||||
|
||||
Action act() override {
|
||||
return m_decoder->decodeAsync(m_headers, m_bodyStream, m_chunkedBuffer).next(yieldTo(&ToStringDecoder::onDecoded));
|
||||
return m_decoder->decodeAsync(m_headers, m_bodyStream, m_outputStream).next(yieldTo(&ToStringDecoder::onDecoded));
|
||||
}
|
||||
|
||||
Action onDecoded() {
|
||||
return _return(m_chunkedBuffer->toString());
|
||||
return _return(m_outputStream->toString());
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/data/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
#include "oatpp/core/async/Coroutine.hpp"
|
||||
|
||||
namespace oatpp { namespace web { namespace protocol { namespace http { namespace incoming {
|
||||
@ -47,7 +48,7 @@ private:
|
||||
Headers m_headers;
|
||||
std::shared_ptr<oatpp::data::stream::InputStream> m_bodyStream;
|
||||
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_objectMapper;
|
||||
std::shared_ptr<oatpp::data::stream::ChunkedBuffer> m_chunkedBuffer = oatpp::data::stream::ChunkedBuffer::createShared();
|
||||
std::shared_ptr<oatpp::data::stream::BufferOutputStream> m_outputStream;
|
||||
public:
|
||||
|
||||
ToDtoDecoder(const BodyDecoder* decoder,
|
||||
@ -58,14 +59,15 @@ private:
|
||||
, m_headers(headers)
|
||||
, m_bodyStream(bodyStream)
|
||||
, m_objectMapper(objectMapper)
|
||||
, m_outputStream(std::make_shared<oatpp::data::stream::BufferOutputStream>())
|
||||
{}
|
||||
|
||||
oatpp::async::Action act() override {
|
||||
return m_decoder->decodeAsync(m_headers, m_bodyStream, m_chunkedBuffer).next(this->yieldTo(&ToDtoDecoder::onDecoded));
|
||||
return m_decoder->decodeAsync(m_headers, m_bodyStream, m_outputStream).next(this->yieldTo(&ToDtoDecoder::onDecoded));
|
||||
}
|
||||
|
||||
oatpp::async::Action onDecoded() {
|
||||
auto body = m_chunkedBuffer->toString();
|
||||
auto body = m_outputStream->toString();
|
||||
oatpp::parser::Caret caret(body);
|
||||
auto dto = m_objectMapper->readFromCaret<Type>(caret);
|
||||
if(caret.hasError()) {
|
||||
@ -109,7 +111,7 @@ public:
|
||||
* @return - &oatpp::String;.
|
||||
*/
|
||||
oatpp::String decodeToString(const Headers& headers, data::stream::InputStream* bodyStream) const {
|
||||
oatpp::data::stream::ChunkedBuffer stream;
|
||||
oatpp::data::stream::BufferOutputStream stream;
|
||||
decode(headers, bodyStream, &stream);
|
||||
return stream.toString();
|
||||
}
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/data/mapping/type/Type.hpp"
|
||||
#include "oatpp/core/data/stream/ChunkedBuffer.hpp"
|
||||
|
||||
|
||||
namespace oatpp { namespace web { namespace server { namespace handler {
|
||||
|
@ -63,7 +63,7 @@ void runTests() {
|
||||
|
||||
OATPP_LOGD("aaa", "coroutine size=%d", sizeof(oatpp::async::AbstractCoroutine));
|
||||
OATPP_LOGD("aaa", "action size=%d", sizeof(oatpp::async::Action));
|
||||
/*
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::base::RegRuleTest);
|
||||
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
|
||||
|
||||
@ -87,9 +87,9 @@ void runTests() {
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::parser::CaretTest);
|
||||
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DeserializerTest);
|
||||
*/
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperPerfTest);
|
||||
/*
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperTest);
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::encoding::Base64Test);
|
||||
@ -169,7 +169,7 @@ void runTests() {
|
||||
test_port.run();
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,9 @@
|
||||
#include "DTOMapperPerfTest.hpp"
|
||||
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/parser/json/mapping/Serializer2.hpp"
|
||||
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
|
||||
#include "oatpp/core/macro/basic.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
@ -35,7 +38,7 @@ namespace oatpp { namespace test { namespace parser { namespace json { namespace
|
||||
|
||||
namespace {
|
||||
|
||||
typedef oatpp::parser::json::mapping::Serializer Serializer;
|
||||
typedef oatpp::parser::json::mapping::Serializer2 Serializer;
|
||||
typedef oatpp::parser::json::mapping::Deserializer Deserializer;
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
@ -68,12 +71,23 @@ typedef oatpp::parser::json::mapping::Deserializer Deserializer;
|
||||
void DTOMapperPerfTest::onRun() {
|
||||
|
||||
v_int32 numIterations = 1000000;
|
||||
|
||||
|
||||
auto serializer2 = std::make_shared<oatpp::parser::json::mapping::Serializer2>();
|
||||
auto mapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
|
||||
auto test1 = Test1::createTestInstance();
|
||||
auto test1_Text = mapper->writeToString(test1);
|
||||
OATPP_LOGV(TAG, "json='%s'", (const char*) test1_Text->getData());
|
||||
|
||||
{
|
||||
PerformanceChecker checker("Serializer2");
|
||||
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 ++) {
|
||||
|
@ -35,7 +35,7 @@ namespace {
|
||||
|
||||
typedef oatpp::data::mapping::type::Object DTO;
|
||||
typedef oatpp::parser::Caret ParsingCaret;
|
||||
typedef oatpp::parser::json::mapping::Serializer Serializer;
|
||||
typedef oatpp::parser::json::mapping::Serializer2 Serializer;
|
||||
typedef oatpp::parser::json::mapping::Deserializer Deserializer;
|
||||
|
||||
class EmptyDto : public DTO {
|
||||
|
Loading…
Reference in New Issue
Block a user