mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
parser::json. Correct Enum Ser/De.
This commit is contained in:
parent
babeec5e2c
commit
9ee3c80626
@ -34,6 +34,36 @@
|
||||
|
||||
namespace oatpp { namespace data { namespace mapping { namespace type {
|
||||
|
||||
/**
|
||||
* Errors of enum interpretation.
|
||||
*/
|
||||
enum class EnumInterpreterError : v_int32 {
|
||||
|
||||
/**
|
||||
* The interpretation was successful.
|
||||
*/
|
||||
OK = 0,
|
||||
|
||||
/**
|
||||
* Wrong `Interpreter` is used to interpret the variable. <br>
|
||||
* This may also occur if for example: <br>
|
||||
* `oatpp::Enum<T>` is passed to interpreter of `oatpp::Enum<T>::NotNull`.
|
||||
*/
|
||||
TYPE_MISMATCH_ENUM = 1,
|
||||
|
||||
/**
|
||||
* Wrong &id:oatpp::data::mapping::type::Primitive; is passed to interpreter.
|
||||
*/
|
||||
TYPE_MISMATCH_ENUM_VALUE = 2,
|
||||
|
||||
/**
|
||||
* Interpreter constraint is violated. <br>
|
||||
* The constraint was set to `NotNull` but interpretation to/from `nullptr` is requested.
|
||||
*/
|
||||
CONSTRAINT_NOT_NULL = 3
|
||||
|
||||
};
|
||||
|
||||
namespace __class {
|
||||
|
||||
class AbstractEnum {
|
||||
@ -50,8 +80,8 @@ namespace __class {
|
||||
|
||||
const bool notNull;
|
||||
|
||||
virtual type::Void toInterpretation(const type::Void& enumValue) const = 0;
|
||||
virtual type::Void fromInterpretation(const type::Void& interValue) const = 0;
|
||||
virtual type::Void toInterpretation(const type::Void& enumValue, EnumInterpreterError& error) const = 0;
|
||||
virtual type::Void fromInterpretation(const type::Void& interValue, EnumInterpreterError& error) const = 0;
|
||||
virtual type::Type* getInterpretationType() const = 0;
|
||||
|
||||
};
|
||||
@ -100,29 +130,29 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
template<class T, bool nullable>
|
||||
template<class T, bool notnull>
|
||||
class EnumInterpreterAsString {
|
||||
public:
|
||||
template <bool N>
|
||||
using InterpreterType = EnumInterpreterAsString<T, N>;
|
||||
public:
|
||||
constexpr static bool notNull = nullable;
|
||||
constexpr static bool notNull = notnull;
|
||||
public:
|
||||
static Void toInterpretation(const Void& enumValue);
|
||||
static Void fromInterpretation(const Void& interValue);
|
||||
static Void toInterpretation(const Void& enumValue, EnumInterpreterError& error);
|
||||
static Void fromInterpretation(const Void& interValue, EnumInterpreterError& error);
|
||||
static Type* getInterpretationType();
|
||||
};
|
||||
|
||||
template<class T, bool nullable>
|
||||
template<class T, bool notnull>
|
||||
class EnumInterpreterAsInteger {
|
||||
public:
|
||||
template <bool N>
|
||||
using InterpreterType = EnumInterpreterAsInteger<T, N>;
|
||||
public:
|
||||
constexpr static bool notNull = nullable;
|
||||
constexpr static bool notNull = notnull;
|
||||
public:
|
||||
static Void toInterpretation(const Void& enumValue);
|
||||
static Void fromInterpretation(const Void& interValue);
|
||||
static Void toInterpretation(const Void& enumValue, EnumInterpreterError& error);
|
||||
static Void fromInterpretation(const Void& interValue, EnumInterpreterError& error);
|
||||
static Type* getInterpretationType();
|
||||
};
|
||||
|
||||
@ -296,12 +326,21 @@ public:
|
||||
template <class T>
|
||||
using Enum = EnumObjectWrapper<T, EnumInterpreterAsString<T, false>>;
|
||||
|
||||
template<class T, bool nullable>
|
||||
Void EnumInterpreterAsString<T, nullable>::toInterpretation(const Void& enumValue) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsString<T, nullable>> EnumOW;
|
||||
template<class T, bool notnull>
|
||||
Void EnumInterpreterAsString<T, notnull>::toInterpretation(const Void& enumValue, EnumInterpreterError& error) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsString<T, notnull>> EnumOW;
|
||||
|
||||
if(enumValue.valueType != EnumOW::Class::getType()) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::EnumInterpreterAsString::toInterpretation()]: Error. Enum type doesn't match.");
|
||||
error = EnumInterpreterError::TYPE_MISMATCH_ENUM;
|
||||
return Void(nullptr, String::Class::getType());
|
||||
}
|
||||
|
||||
if(!enumValue) {
|
||||
if(notnull) {
|
||||
error = EnumInterpreterError::CONSTRAINT_NOT_NULL;
|
||||
return Void(nullptr, String::Class::getType());
|
||||
}
|
||||
return Void(nullptr, String::Class::getType());
|
||||
}
|
||||
|
||||
const auto& ow = enumValue.staticCast<EnumOW>();
|
||||
@ -309,32 +348,50 @@ Void EnumInterpreterAsString<T, nullable>::toInterpretation(const Void& enumValu
|
||||
return entry.name.toString();
|
||||
}
|
||||
|
||||
template<class T, bool nullable>
|
||||
Void EnumInterpreterAsString<T, nullable>::fromInterpretation(const Void& interValue) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsString<T, nullable>> EnumOW;
|
||||
template<class T, bool notnull>
|
||||
Void EnumInterpreterAsString<T, notnull>::fromInterpretation(const Void& interValue, EnumInterpreterError& error) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsString<T, notnull>> EnumOW;
|
||||
|
||||
if(interValue.valueType != String::Class::getType()) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::EnumInterpreterAsString::fromInterpretation()]: Error. Interpretation must be a String.");
|
||||
error = EnumInterpreterError::TYPE_MISMATCH_ENUM_VALUE;
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
|
||||
const auto& entry = EnumObjectWrapper<T, EnumInterpreterAsString<T, nullable>>::getEntryByName(interValue.staticCast<String>());
|
||||
if(!interValue) {
|
||||
if(notnull) {
|
||||
error = EnumInterpreterError::CONSTRAINT_NOT_NULL;
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
|
||||
const auto& entry = EnumObjectWrapper<T, EnumInterpreterAsString<T, notnull>>::getEntryByName(interValue.staticCast<String>());
|
||||
return EnumOW(entry.value);
|
||||
}
|
||||
|
||||
template<class T, bool nullable>
|
||||
Type* EnumInterpreterAsString<T, nullable>::getInterpretationType() {
|
||||
template<class T, bool notnull>
|
||||
Type* EnumInterpreterAsString<T, notnull>::getInterpretationType() {
|
||||
return String::Class::getType();
|
||||
}
|
||||
|
||||
template<class T, bool nullable>
|
||||
Void EnumInterpreterAsInteger<T, nullable>::toInterpretation(const Void& enumValue) {
|
||||
template<class T, bool notnull>
|
||||
Void EnumInterpreterAsInteger<T, notnull>::toInterpretation(const Void& enumValue, EnumInterpreterError& error) {
|
||||
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsInteger<T, nullable>> EnumOW;
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsInteger<T, notnull>> EnumOW;
|
||||
typedef typename std::underlying_type<T>::type EnumUT;
|
||||
typedef typename ObjectWrapperByUnderlyingType<EnumUT>::ObjectWrapper UTOW;
|
||||
|
||||
if(enumValue.valueType != EnumOW::Class::getType()) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::EnumInterpreterAsInteger::toInterpretation()]: Error. Enum type doesn't match.");
|
||||
error = EnumInterpreterError::TYPE_MISMATCH_ENUM;
|
||||
return Void(nullptr, UTOW::Class::getType());
|
||||
}
|
||||
|
||||
if(!enumValue) {
|
||||
if(notnull) {
|
||||
error = EnumInterpreterError::CONSTRAINT_NOT_NULL;
|
||||
return Void(nullptr, UTOW::Class::getType());
|
||||
}
|
||||
return Void(nullptr, UTOW::Class::getType());
|
||||
}
|
||||
|
||||
const auto& ow = enumValue.staticCast<EnumOW>();
|
||||
@ -342,23 +399,32 @@ Void EnumInterpreterAsInteger<T, nullable>::toInterpretation(const Void& enumVal
|
||||
|
||||
}
|
||||
|
||||
template<class T, bool nullable>
|
||||
Void EnumInterpreterAsInteger<T, nullable>::fromInterpretation(const Void& interValue) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsInteger<T, nullable>> EnumOW;
|
||||
template<class T, bool notnull>
|
||||
Void EnumInterpreterAsInteger<T, notnull>::fromInterpretation(const Void& interValue, EnumInterpreterError& error) {
|
||||
typedef EnumObjectWrapper<T, EnumInterpreterAsInteger<T, notnull>> EnumOW;
|
||||
|
||||
typedef typename std::underlying_type<T>::type EnumUT;
|
||||
typedef typename ObjectWrapperByUnderlyingType<EnumUT>::ObjectWrapper OW;
|
||||
|
||||
if(interValue.valueType != OW::Class::getType()) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::EnumInterpreterAsInteger::fromInterpretation()]: Error. Interpretation value type doesn't match.");
|
||||
error = EnumInterpreterError::TYPE_MISMATCH_ENUM_VALUE;
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
|
||||
const auto& entry = EnumObjectWrapper<T, EnumInterpreterAsInteger<T, nullable>>::getEntryByUnderlyingValue(interValue.staticCast<OW>());
|
||||
if(!interValue) {
|
||||
if(notnull) {
|
||||
error = EnumInterpreterError::CONSTRAINT_NOT_NULL;
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
return Void(nullptr, EnumOW::Class::getType());
|
||||
}
|
||||
|
||||
const auto& entry = EnumObjectWrapper<T, EnumInterpreterAsInteger<T, notnull>>::getEntryByUnderlyingValue(interValue.staticCast<OW>());
|
||||
return EnumOW(entry.value);
|
||||
}
|
||||
|
||||
template<class T, bool nullable>
|
||||
Type* EnumInterpreterAsInteger<T, nullable>::getInterpretationType() {
|
||||
template<class T, bool notnull>
|
||||
Type* EnumInterpreterAsInteger<T, notnull>::getInterpretationType() {
|
||||
typedef typename std::underlying_type<T>::type EnumUT;
|
||||
return ObjectWrapperByUnderlyingType<EnumUT>::ObjectWrapper::Class::getType();
|
||||
}
|
||||
@ -375,12 +441,12 @@ namespace __class {
|
||||
: AbstractPolymorphicDispatcher(Interpreter::notNull)
|
||||
{}
|
||||
|
||||
type::Void toInterpretation(const type::Void& enumValue) const override {
|
||||
return Interpreter::toInterpretation(enumValue);
|
||||
type::Void toInterpretation(const type::Void& enumValue, EnumInterpreterError& error) const override {
|
||||
return Interpreter::toInterpretation(enumValue, error);
|
||||
}
|
||||
|
||||
type::Void fromInterpretation(const type::Void& interValue) const override {
|
||||
return Interpreter::fromInterpretation(interValue);
|
||||
type::Void fromInterpretation(const type::Void& interValue, EnumInterpreterError& error) const override {
|
||||
return Interpreter::fromInterpretation(interValue, error);
|
||||
}
|
||||
|
||||
type::Type* getInterpretationType() const override {
|
||||
|
@ -258,10 +258,32 @@ oatpp::Void Deserializer::deserializeAny(Deserializer* deserializer, parser::Car
|
||||
}
|
||||
|
||||
oatpp::Void Deserializer::deserializeEnum(Deserializer* deserializer, parser::Caret& caret, const Type* const type) {
|
||||
|
||||
auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::AbstractPolymorphicDispatcher*>(
|
||||
type->polymorphicDispatcher
|
||||
);
|
||||
return polymorphicDispatcher->fromInterpretation(deserializer->deserialize(caret, polymorphicDispatcher->getInterpretationType()));
|
||||
|
||||
data::mapping::type::EnumInterpreterError e = data::mapping::type::EnumInterpreterError::OK;
|
||||
const auto& value = deserializer->deserialize(caret, polymorphicDispatcher->getInterpretationType());
|
||||
if(caret.hasError()) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto& result = polymorphicDispatcher->fromInterpretation(value, e);
|
||||
|
||||
if(e == data::mapping::type::EnumInterpreterError::OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
switch(e) {
|
||||
case data::mapping::type::EnumInterpreterError::CONSTRAINT_NOT_NULL:
|
||||
caret.setError("[oatpp::parser::json::mapping::Deserializer::deserializeEnum()]: Error. Enum constraint violated - 'NotNull'.");
|
||||
break;
|
||||
default:
|
||||
caret.setError("[oatpp::parser::json::mapping::Deserializer::deserializeEnum()]: Error. Can't deserialize Enum.");
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
||||
}
|
||||
|
||||
oatpp::Void Deserializer::deserializeObject(Deserializer* deserializer, parser::Caret& caret, const Type* const type) {
|
||||
|
@ -121,7 +121,21 @@ void Serializer::serializeEnum(Serializer* serializer,
|
||||
auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::AbstractPolymorphicDispatcher*>(
|
||||
polymorph.valueType->polymorphicDispatcher
|
||||
);
|
||||
serializer->serialize(stream, polymorphicDispatcher->toInterpretation(polymorph));
|
||||
|
||||
data::mapping::type::EnumInterpreterError e = data::mapping::type::EnumInterpreterError::OK;
|
||||
serializer->serialize(stream, polymorphicDispatcher->toInterpretation(polymorph, e));
|
||||
|
||||
if(e == data::mapping::type::EnumInterpreterError::OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch(e) {
|
||||
case data::mapping::type::EnumInterpreterError::CONSTRAINT_NOT_NULL:
|
||||
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serializeEnum()]: Error. Enum constraint violated - 'NotNull'.");
|
||||
default:
|
||||
throw std::runtime_error("[oatpp::parser::json::mapping::Serializer::serializeEnum()]: Error. Can't serialize Enum.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Serializer::serializeObject(Serializer* serializer,
|
||||
|
@ -61,6 +61,8 @@ add_executable(oatppAllTests
|
||||
oatpp/parser/json/mapping/DTOMapperTest.hpp
|
||||
oatpp/parser/json/mapping/DeserializerTest.cpp
|
||||
oatpp/parser/json/mapping/DeserializerTest.hpp
|
||||
oatpp/parser/json/mapping/EnumTest.cpp
|
||||
oatpp/parser/json/mapping/EnumTest.hpp
|
||||
oatpp/web/protocol/http/encoding/ChunkedTest.cpp
|
||||
oatpp/web/protocol/http/encoding/ChunkedTest.hpp
|
||||
oatpp/web/mime/multipart/StatefulParserTest.cpp
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "oatpp/parser/json/mapping/DeserializerTest.hpp"
|
||||
#include "oatpp/parser/json/mapping/DTOMapperPerfTest.hpp"
|
||||
#include "oatpp/parser/json/mapping/DTOMapperTest.hpp"
|
||||
#include "oatpp/parser/json/mapping/EnumTest.hpp"
|
||||
|
||||
#include "oatpp/encoding/UnicodeTest.hpp"
|
||||
#include "oatpp/encoding/Base64Test.hpp"
|
||||
@ -73,7 +74,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::CommandLineArgumentsTest);
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest);
|
||||
@ -99,16 +100,16 @@ void runTests() {
|
||||
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::PairListTest);
|
||||
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::UnorderedMapTest);
|
||||
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::AnyTest);
|
||||
*/
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::EnumTest);
|
||||
/*
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::async::LockTest);
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::parser::CaretTest);
|
||||
|
||||
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::EnumTest);
|
||||
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);
|
||||
@ -188,7 +189,7 @@ void runTests() {
|
||||
test_port.run();
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -79,14 +79,18 @@ void EnumTest::onRun() {
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Test Interpreter AsString...");
|
||||
auto inter = oatpp::Enum<Enum1>::AsString::Interpreter::toInterpretation(oatpp::Enum<Enum1>::AsString(Enum1::NAME_1));
|
||||
oatpp::data::mapping::type::EnumInterpreterError e = oatpp::data::mapping::type::EnumInterpreterError::OK;
|
||||
auto inter = oatpp::Enum<Enum1>::AsString::Interpreter::toInterpretation(oatpp::Enum<Enum1>::AsString(Enum1::NAME_1), e);
|
||||
OATPP_ASSERT(inter.valueType == oatpp::String::Class::getType());
|
||||
OATPP_ASSERT(e == oatpp::data::mapping::type::EnumInterpreterError::OK);
|
||||
|
||||
auto interValue = inter.staticCast<oatpp::String>();
|
||||
OATPP_ASSERT(interValue == "name-1");
|
||||
|
||||
oatpp::Void voidValue = oatpp::Enum<Enum1>::AsString::Interpreter::fromInterpretation(interValue);
|
||||
oatpp::Void voidValue = oatpp::Enum<Enum1>::AsString::Interpreter::fromInterpretation(interValue, e);
|
||||
OATPP_ASSERT(voidValue.valueType == oatpp::Enum<Enum1>::AsString::Class::getType());
|
||||
OATPP_ASSERT(e == oatpp::data::mapping::type::EnumInterpreterError::OK);
|
||||
|
||||
auto value = voidValue.staticCast<oatpp::Enum<Enum1>::AsString>();
|
||||
OATPP_ASSERT(value == Enum1::NAME_1);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
@ -94,14 +98,18 @@ void EnumTest::onRun() {
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Test Interpreter AsInteger...");
|
||||
auto inter = oatpp::Enum<Enum1>::AsInteger::Interpreter::toInterpretation(oatpp::Enum<Enum1>::AsInteger(Enum1::NAME_1));
|
||||
oatpp::data::mapping::type::EnumInterpreterError e = oatpp::data::mapping::type::EnumInterpreterError::OK;
|
||||
auto inter = oatpp::Enum<Enum1>::AsInteger::Interpreter::toInterpretation(oatpp::Enum<Enum1>::AsInteger(Enum1::NAME_1), e);
|
||||
OATPP_ASSERT(inter.valueType == oatpp::Int32::Class::getType());
|
||||
OATPP_ASSERT(e == oatpp::data::mapping::type::EnumInterpreterError::OK);
|
||||
|
||||
auto interValue = inter.staticCast<oatpp::Int32>();
|
||||
OATPP_ASSERT(interValue == static_cast<v_int32>(Enum1::NAME_1));
|
||||
|
||||
oatpp::Void voidValue = oatpp::Enum<Enum1>::AsInteger::Interpreter::fromInterpretation(interValue);
|
||||
oatpp::Void voidValue = oatpp::Enum<Enum1>::AsInteger::Interpreter::fromInterpretation(interValue, e);
|
||||
OATPP_ASSERT(voidValue.valueType == oatpp::Enum<Enum1>::AsInteger::Class::getType());
|
||||
OATPP_ASSERT(e == oatpp::data::mapping::type::EnumInterpreterError::OK);
|
||||
|
||||
auto value = voidValue.staticCast<oatpp::Enum<Enum1>::AsInteger>();
|
||||
OATPP_ASSERT(value == Enum1::NAME_1);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
|
187
test/oatpp/parser/json/mapping/EnumTest.cpp
Normal file
187
test/oatpp/parser/json/mapping/EnumTest.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 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 "EnumTest.hpp"
|
||||
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
namespace oatpp { namespace test { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
namespace {
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
ENUM(Enum0, v_int32);
|
||||
|
||||
ENUM(Enum1, v_int32,
|
||||
VALUE(V1, 10, "enum1-v1"),
|
||||
VALUE(V2, 20, "enum1-v2"),
|
||||
VALUE(V3, 30, "enum1-v3")
|
||||
);
|
||||
|
||||
class DTO1 : public oatpp::Object {
|
||||
|
||||
DTO_INIT(DTO1, Object)
|
||||
|
||||
DTO_FIELD(Enum<Enum1>::AsString, enum1);
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
}
|
||||
|
||||
void EnumTest::onRun() {
|
||||
|
||||
oatpp::parser::json::mapping::ObjectMapper mapper;
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as string...");
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsString> map = {{"enum", Enum1::V1}};
|
||||
auto json = mapper.writeToString(map);
|
||||
OATPP_LOGD(TAG, "json='%s'", json->c_str());
|
||||
OATPP_ASSERT(json == "{\"enum\":\"enum1-v1\"}");
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as string null...");
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsString> map = {{"enum", nullptr}};
|
||||
auto json = mapper.writeToString(map);
|
||||
OATPP_LOGD(TAG, "json='%s'", json->c_str());
|
||||
OATPP_ASSERT(json == "{\"enum\":null}");
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as string error on null...");
|
||||
bool error = false;
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsString::NotNull> map = {{"enum", nullptr}};
|
||||
try {
|
||||
auto json = mapper.writeToString(map);
|
||||
} catch (const std::runtime_error& e) {
|
||||
OATPP_LOGD(TAG, "error - %s", e.what());
|
||||
error = true;
|
||||
}
|
||||
OATPP_ASSERT(error == true);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as int...");
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsInteger> map = {{"enum", Enum1::V1}};
|
||||
auto json = mapper.writeToString(map);
|
||||
OATPP_LOGD(TAG, "json='%s'", json->c_str());
|
||||
OATPP_ASSERT(json == "{\"enum\":10}");
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as int null...");
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsInteger> map = {{"enum", nullptr}};
|
||||
auto json = mapper.writeToString(map);
|
||||
OATPP_LOGD(TAG, "json='%s'", json->c_str());
|
||||
OATPP_ASSERT(json == "{\"enum\":null}");
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Serializer as int error on null...");
|
||||
bool error = false;
|
||||
oatpp::Fields<oatpp::Enum<Enum1>::AsInteger::NotNull> map = {{"enum", nullptr}};
|
||||
try {
|
||||
auto json = mapper.writeToString(map);
|
||||
} catch (const std::runtime_error& e) {
|
||||
OATPP_LOGD(TAG, "error - %s", e.what());
|
||||
error = true;
|
||||
}
|
||||
OATPP_ASSERT(error == true);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as string...");
|
||||
oatpp::String json = "{\"enum\":\"enum1-v2\"}";
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsString>>(json);
|
||||
OATPP_ASSERT(map["enum"] == Enum1::V2);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as string null...");
|
||||
oatpp::String json = "{\"enum\":null}";
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsString>>(json);
|
||||
OATPP_ASSERT(map["enum"] == nullptr);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as string error on null...");
|
||||
bool error = false;
|
||||
oatpp::String json = "{\"enum\":null}";
|
||||
try {
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsString::NotNull>>(json);
|
||||
} catch (const oatpp::parser::ParsingError& e) {
|
||||
OATPP_LOGD(TAG, "error - %s", e.what());
|
||||
error = true;
|
||||
}
|
||||
OATPP_ASSERT(error == true);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as int...");
|
||||
oatpp::String json = "{\"enum\":20}";
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsInteger>>(json);
|
||||
OATPP_ASSERT(map["enum"] == Enum1::V2);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as int null...");
|
||||
oatpp::String json = "{\"enum\":null}";
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsInteger>>(json);
|
||||
OATPP_ASSERT(map["enum"] == nullptr);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "Deserializer as int error on null...");
|
||||
bool error = false;
|
||||
oatpp::String json = "{\"enum\":null}";
|
||||
try {
|
||||
auto map = mapper.readFromString<oatpp::Fields<oatpp::Enum<Enum1>::AsInteger::NotNull>>(json);
|
||||
} catch (const oatpp::parser::ParsingError& e) {
|
||||
OATPP_LOGD(TAG, "error - %s", e.what());
|
||||
error = true;
|
||||
}
|
||||
OATPP_ASSERT(error == true);
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}}}}}
|
42
test/oatpp/parser/json/mapping/EnumTest.hpp
Normal file
42
test/oatpp/parser/json/mapping/EnumTest.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* 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_test_parser_json_mapping_EnumTest_hpp
|
||||
#define oatpp_test_parser_json_mapping_EnumTest_hpp
|
||||
|
||||
#include "oatpp-test/UnitTest.hpp"
|
||||
|
||||
namespace oatpp { namespace test { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
class EnumTest : public UnitTest{
|
||||
public:
|
||||
|
||||
EnumTest():UnitTest("TEST[parser::json::mapping::EnumTest]"){}
|
||||
void onRun() override;
|
||||
|
||||
};
|
||||
|
||||
}}}}}
|
||||
|
||||
#endif /* oatpp_test_parser_json_mapping_EnumTest_hpp */
|
Loading…
Reference in New Issue
Block a user