Fix serialization error when running parallel tasks

This commit is contained in:
doufu3344 2022-11-01 09:50:43 +08:00
parent 1564eba64b
commit 9ae9644cfe
3 changed files with 132 additions and 94 deletions

View File

@ -64,7 +64,6 @@ Serializer::Serializer(const std::shared_ptr<Config>& config)
setSerializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, &Serializer::serializeMap); setSerializerMethod(data::mapping::type::__class::AbstractPairList::CLASS_ID, &Serializer::serializeMap);
setSerializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, &Serializer::serializeMap); setSerializerMethod(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, &Serializer::serializeMap);
m_context = std::unique_ptr<Context>(new Context());
} }
void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) { void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
@ -76,42 +75,40 @@ void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId
} }
template<> template<>
void Serializer::serializePrimitive<Float32>(Serializer* serializer, void Serializer::serializePrimitive<Float32>(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph){ const oatpp::Void& polymorph){
if (polymorph) { if (polymorph) {
std::string format; std::string format;
if (!serializer->m_context->info.format.empty()) { if (!context->info.format.empty()) {
format = serializer->m_context->info.format; format = context->info.format;
} }
else if (!serializer->m_config->floatStringFormat->empty()) { else if (!context->serializer->m_config->floatStringFormat->empty()) {
format = serializer->m_config->floatStringFormat; format = context->serializer->m_config->floatStringFormat;
} }
stream->writeAsString(*static_cast<v_float32*>(polymorph.get()), context->stream->writeAsString(*static_cast<v_float32*>(polymorph.get()),
format.c_str()); format.c_str());
} }
else { else {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
} }
} }
template<> template<>
void Serializer::serializePrimitive<Float64>(Serializer* serializer, void Serializer::serializePrimitive<Float64>(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) { const oatpp::Void& polymorph) {
if (polymorph) { if (polymorph) {
std::string format; std::string format;
if (!serializer->m_context->info.format.empty()) { if (!context->info.format.empty()) {
format = serializer->m_context->info.format; format = context->info.format;
} }
else if (!serializer->m_config->floatStringFormat->empty()) { else if (!context->serializer->m_config->floatStringFormat->empty()) {
format = serializer->m_config->floatStringFormat; format = context->serializer->m_config->floatStringFormat;
} }
stream->writeAsString(*static_cast<v_float64*>(polymorph.get()), context->stream->writeAsString(*static_cast<v_float64*>(polymorph.get()),
format.c_str()); format.c_str());
} }
else { else {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
} }
} }
@ -122,39 +119,36 @@ void Serializer::serializeString(data::stream::ConsistentOutputStream* stream, c
stream->writeCharSimple('\"'); stream->writeCharSimple('\"');
} }
void Serializer::serializeString(Serializer* serializer, void Serializer::serializeString(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
if(!polymorph) { if(!polymorph) {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
return; return;
} }
auto str = static_cast<std::string*>(polymorph.get()); auto str = static_cast<std::string*>(polymorph.get());
serializeString(stream, str->data(), str->size(), serializer->m_config->escapeFlags); serializeString(context->stream, str->data(), str->size(), context->serializer->m_config->escapeFlags);
} }
void Serializer::serializeAny(Serializer* serializer, void Serializer::serializeAny(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
if(!polymorph) { if(!polymorph) {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
return; return;
} }
auto anyHandle = static_cast<data::mapping::type::AnyHandle*>(polymorph.get()); auto anyHandle = static_cast<data::mapping::type::AnyHandle*>(polymorph.get());
serializer->serialize(stream, oatpp::Void(anyHandle->ptr, anyHandle->type)); context->serializer->serialize(context, oatpp::Void(anyHandle->ptr, anyHandle->type));
} }
void Serializer::serializeEnum(Serializer* serializer, void Serializer::serializeEnum(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::PolymorphicDispatcher*>( auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::PolymorphicDispatcher*>(
@ -162,7 +156,7 @@ void Serializer::serializeEnum(Serializer* serializer,
); );
data::mapping::type::EnumInterpreterError e = data::mapping::type::EnumInterpreterError::OK; data::mapping::type::EnumInterpreterError e = data::mapping::type::EnumInterpreterError::OK;
serializer->serialize(stream, polymorphicDispatcher->toInterpretation(polymorph, e)); context->serializer->serialize(context, polymorphicDispatcher->toInterpretation(polymorph, e));
if(e == data::mapping::type::EnumInterpreterError::OK) { if(e == data::mapping::type::EnumInterpreterError::OK) {
return; return;
@ -177,13 +171,12 @@ void Serializer::serializeEnum(Serializer* serializer,
} }
void Serializer::serializeCollection(Serializer* serializer, void Serializer::serializeCollection(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
if(!polymorph) { if(!polymorph) {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
return; return;
} }
@ -191,31 +184,32 @@ void Serializer::serializeCollection(Serializer* serializer,
polymorph.getValueType()->polymorphicDispatcher polymorph.getValueType()->polymorphicDispatcher
); );
stream->writeCharSimple('['); context->stream->writeCharSimple('[');
bool first = true; bool first = true;
auto iterator = dispatcher->beginIteration(polymorph); auto iterator = dispatcher->beginIteration(polymorph);
while (!iterator->finished()) { while (!iterator->finished()) {
const auto& value = iterator->get(); const auto& value = iterator->get();
if(value || serializer->getConfig()->includeNullFields || serializer->getConfig()->alwaysIncludeNullCollectionElements) { if(value ||
(first) ? first = false : stream->writeSimple(",", 1); context->serializer->getConfig()->includeNullFields ||
serializer->serialize(stream, value); context->serializer->getConfig()->alwaysIncludeNullCollectionElements) {
(first) ? first = false : context->stream->writeSimple(",", 1);
context->serializer->serialize(context, value);
} }
iterator->next(); iterator->next();
} }
stream->writeCharSimple(']'); context->stream->writeCharSimple(']');
} }
void Serializer::serializeMap(Serializer* serializer, void Serializer::serializeMap(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
if(!polymorph) { if(!polymorph) {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
return; return;
} }
@ -228,39 +222,40 @@ void Serializer::serializeMap(Serializer* serializer,
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serializeMap()]: Invalid json map key. Key should be String"); throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serializeMap()]: Invalid json map key. Key should be String");
} }
stream->writeCharSimple('{'); context->stream->writeCharSimple('{');
bool first = true; bool first = true;
auto iterator = dispatcher->beginIteration(polymorph); auto iterator = dispatcher->beginIteration(polymorph);
while (!iterator->finished()) { while (!iterator->finished()) {
const auto& value = iterator->getValue(); const auto& value = iterator->getValue();
if(value || serializer->m_config->includeNullFields || serializer->m_config->alwaysIncludeNullCollectionElements) { if(value ||
(first) ? first = false : stream->writeSimple(",", 1); context->serializer->m_config->includeNullFields ||
context->serializer->m_config->alwaysIncludeNullCollectionElements) {
(first) ? first = false : context->stream->writeSimple(",", 1);
const auto& untypedKey = iterator->getKey(); const auto& untypedKey = iterator->getKey();
const auto& key = oatpp::String(std::static_pointer_cast<std::string>(untypedKey.getPtr())); const auto& key = oatpp::String(std::static_pointer_cast<std::string>(untypedKey.getPtr()));
serializeString(stream, key->data(), key->size(), serializer->m_config->escapeFlags); serializeString(context->stream, key->data(), key->size(), context->serializer->m_config->escapeFlags);
stream->writeSimple(":", 1); context->stream->writeSimple(":", 1);
serializer->serialize(stream, value); context->serializer->serialize(context, value);
} }
iterator->next(); iterator->next();
} }
stream->writeCharSimple('}'); context->stream->writeCharSimple('}');
} }
void Serializer::serializeObject(Serializer* serializer, void Serializer::serializeObject(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream, const oatpp::Void& polymorph)
const oatpp::Void& polymorph)
{ {
if(!polymorph) { if(!polymorph) {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
return; return;
} }
stream->writeCharSimple('{'); context->stream->writeCharSimple('{');
bool first = true; bool first = true;
auto dispatcher = static_cast<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>( auto dispatcher = static_cast<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(
@ -268,8 +263,7 @@ void Serializer::serializeObject(Serializer* serializer,
); );
auto fields = dispatcher->getProperties()->getList(); auto fields = dispatcher->getProperties()->getList();
auto object = static_cast<oatpp::BaseObject*>(polymorph.get()); auto object = static_cast<oatpp::BaseObject*>(polymorph.get());
auto config = serializer->m_config; auto config = context->serializer->m_config;
auto &context = serializer->m_context;
for (auto const& field : fields) { for (auto const& field : fields) {
@ -282,32 +276,31 @@ void Serializer::serializeObject(Serializer* serializer,
} }
if (value || config->includeNullFields || (field->info.required && config->alwaysIncludeRequired)) { if (value || config->includeNullFields || (field->info.required && config->alwaysIncludeRequired)) {
(first) ? first = false : stream->writeSimple(",", 1); (first) ? first = false : context->stream->writeSimple(",", 1);
serializeString(stream, field->name, std::strlen(field->name), serializer->m_config->escapeFlags); serializeString(context->stream, field->name, std::strlen(field->name), context->serializer->m_config->escapeFlags);
stream->writeSimple(":", 1); context->stream->writeSimple(":", 1);
context->info = field->info; context->info = field->info;
serializer->serialize(stream, value); context->serializer->serialize(context, value);
} }
} }
stream->writeCharSimple('}'); context->stream->writeCharSimple('}');
} }
void Serializer::serialize(data::stream::ConsistentOutputStream* stream, void Serializer::serialize(const std::unique_ptr<Context>& context,
const oatpp::Void& polymorph) const oatpp::Void& polymorph)
{ {
auto id = polymorph.getValueType()->classId.id; auto id = polymorph.getValueType()->classId.id;
auto& method = m_methods[id]; auto& method = m_methods[id];
if(method) { if(method) {
(*method)(this, stream, polymorph); (*method)(context, polymorph);
} else { } else {
auto* interpretation = polymorph.getValueType()->findInterpretation(m_config->enabledInterpretations); auto* interpretation = polymorph.getValueType()->findInterpretation(m_config->enabledInterpretations);
if(interpretation) { if(interpretation) {
serialize(stream, interpretation->toInterpretation(polymorph)); serialize(context, interpretation->toInterpretation(polymorph));
} else { } else {
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serialize()]: " throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serialize()]: "
"Error. No serialize method for type '" + "Error. No serialize method for type '" +
@ -322,9 +315,11 @@ void Serializer::serializeToStream(data::stream::ConsistentOutputStream* stream,
{ {
if(m_config->useBeautifier) { if(m_config->useBeautifier) {
json::Beautifier beautifier(stream, " ", "\n"); json::Beautifier beautifier(stream, " ", "\n");
serialize(&beautifier, polymorph); auto context = std::unique_ptr<Context>(new Context(this, &beautifier));
serialize(context, polymorph);
} else { } else {
serialize(stream, polymorph); auto context = std::unique_ptr<Context>(new Context(this,stream));
serialize(context, polymorph);
} }
} }

View File

@ -129,29 +129,29 @@ private:
/** /**
* Constructor. * Constructor.
*/ */
Context() Context(Serializer *serializer, data::stream::ConsistentOutputStream* stream)
: serializer(serializer), stream(stream)
{} {}
public: public:
Serializer* serializer;
data::stream::ConsistentOutputStream* stream;
data::mapping::type::BaseObject::Property::Info info; data::mapping::type::BaseObject::Property::Info info;
}; };
public: public:
typedef void (*SerializerMethod)(Serializer*, typedef void (*SerializerMethod)(const std::unique_ptr<Context>&,
data::stream::ConsistentOutputStream*,
const oatpp::Void&); const oatpp::Void&);
private: private:
template<class T> template<class T>
static void serializePrimitive(Serializer* serializer, static void serializePrimitive(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph){ const oatpp::Void& polymorph){
(void) serializer;
if(polymorph){ if(polymorph){
stream->writeAsString(* static_cast<typename T::ObjectType*>(polymorph.get())); context->stream->writeAsString(* static_cast<typename T::ObjectType*>(polymorph.get()));
} else { } else {
stream->writeSimple("null", 4); context->stream->writeSimple("null", 4);
} }
} }
@ -160,35 +160,28 @@ private:
v_buff_size size, v_buff_size size,
v_uint32 escapeFlags); v_uint32 escapeFlags);
static void serializeString(Serializer* serializer, static void serializeString(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
static void serializeAny(Serializer* serializer, static void serializeAny(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
static void serializeEnum(Serializer* serializer, static void serializeEnum(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
static void serializeCollection(Serializer* serializer, static void serializeCollection(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
static void serializeMap(Serializer* serializer, static void serializeMap(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
static void serializeObject(Serializer* serializer, static void serializeObject(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream,
const oatpp::Void& polymorph); const oatpp::Void& polymorph);
void serialize(data::stream::ConsistentOutputStream* stream, const oatpp::Void& polymorph); void serialize(const std::unique_ptr<Context>& context, const oatpp::Void& polymorph);
private: private:
std::shared_ptr<Config> m_config; std::shared_ptr<Config> m_config;
std::unique_ptr<Context> m_context;
std::vector<SerializerMethod> m_methods; std::vector<SerializerMethod> m_methods;
public: public:
@ -221,14 +214,12 @@ public:
}; };
template<> template<>
void Serializer::serializePrimitive<Float32>(Serializer* serializer, void Serializer::serializePrimitive<Float32>(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream, const oatpp::Void& polymorph);
const oatpp::Void& polymorph);
template<> template<>
void Serializer::serializePrimitive<Float64>(Serializer* serializer, void Serializer::serializePrimitive<Float64>(const std::unique_ptr<Context>& context,
data::stream::ConsistentOutputStream* stream, const oatpp::Void& polymorph);
const oatpp::Void& polymorph);
}}}} }}}}

View File

@ -22,6 +22,8 @@
* *
***************************************************************************/ ***************************************************************************/
#include <thread>
#include "FloatTest.hpp" #include "FloatTest.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp" #include "oatpp/parser/json/mapping/ObjectMapper.hpp"
@ -179,6 +181,56 @@ void FloatTest::onRun() {
OATPP_LOGI(TAG, "OK"); OATPP_LOGI(TAG, "OK");
} }
{
mapperFmt.getSerializer()->getConfig()->useBeautifier = true;
auto test = DTO_64_0::createShared();
test->f64 = 123456.123456;
OATPP_LOGI(TAG, "using config's \"%%.1f\" format(useBeautifier) ...");
auto json = mapperFmt.writeToString(test);
OATPP_LOGD(TAG, "json='%s'", json->c_str());
OATPP_ASSERT(json == "{\n \"f64\": 123456.1\n}");
OATPP_LOGI(TAG, "OK");
}
{
int count = 10000;
bool parallel_result = true;
auto test1 = DTO_64_0::createShared();
test1->f64 = 123456.123456;
OATPP_LOGI(TAG, "parallel 1: using default format...");
OATPP_LOGI(TAG, "expect: json1='%s'", "{\"f64\":123456.123456}");
std::thread proc1([&]() {
for (int i = 0; i < count; ++i) {
auto json = mapper.writeToString(test1);
if (json != "{\"f64\":123456.123456}") {
OATPP_LOGE(TAG, "json1='%s'", json->c_str());
parallel_result = false;
break;
}
}
});
auto test2 = DTO_64_1::createShared();
test2->f64 = 123456.123456;
OATPP_LOGI(TAG, "parallel 2: using \"%%.2f\" format...");
OATPP_LOGI(TAG, "expect: json2='%s'", "{\"f64\":123456.12}");
std::thread proc2([&]() {
for (int i = 0; i < count; ++i) {
auto json = mapper.writeToString(test2);
if (json != "{\"f64\":123456.12}") {
OATPP_LOGE(TAG, "json2='%s'", json->c_str());
parallel_result = false;
break;
}
}
});
proc1.join();
proc2.join();
OATPP_ASSERT(parallel_result == true);
OATPP_LOGI(TAG, "parallel OK");
}
} }
}}}}} }}}}}