Код: Выделить всё
#pragma once
#include
#include
enum class AddressType {
Residential,
Commercial,
Industrial,
Agricultural
};
class AddressTypeConvertor {
public:
static std::unordered_map enumToStringMap;
static std::unordered_map stringToEnumMap;
static void initialize();
static std::string to_string(AddressType method);
static AddressType to_enum(const std::string& method);
};
< /code>
DTO Использование: < /p>
DTO_FIELD(Enum, addressType);
< /code>
интеграция MySQL: < /p>
[*] Создать /post: < /p>
AddressTypeConvertor::to_string(addressDto->addressType),
< /code>
< /li>
get: < /p>
addressDto->addressType = AddressTypeConvertor::to_enum(row[1].get());
< /code>
< /li>
< /ul>
Ошибка при выполнении: < /p>
server=oatpp/1.3.0
code=500
description=Internal Server Error
message=[oatpp::parser::json::mapping::Deserializer::deserializeEnum()]: Error. Can't deserialize Enum.
< /code>
< /li>
Альтернативная попытка с использованием обертки Oatpp Enum < /p>
Модифицированный DTO: < /p>
class BankAccountDto : public oatpp::DTO {
DTO_INIT(BankAccountDto, DTO)
DTO_FIELD(String, id);
DTO_FIELD(String, BankName);
DTO_FIELD(oatpp::Enum::AsString, AccountNumber);
DTO_FIELD(oatpp::Enum::AsString, AccountType);
};
< /code>
enum: < /p>
ENUM(BankAccountType, v_int32,
VALUE(Current, 0, "Current"),
VALUE(Savings, 1, "Savings")
)
< /code>
Использование: < /p>
create: < /p>
bankAccountDto->AccountType.get();
< /code>
< /li>
Получить от db: < /p>
bankAccountDto->AccountNumber = row[2].get();
< /code>
< /li>
< /ul>
Ошибка времени компиляции: < /p>
.set("Type", advertisementDto->Type.get())
see reference to function template instantiation 'mysqlx::abi2::r0::Value::Value(const C *)' being compiled
with [ T=AdvertisementType, C=AdvertisementType ]
see declaration of 'mysqlx::abi2::r0::string::traits'
with [ C=AdvertisementType ]
see reference to function template instantiation 'mysqlx::abi2::r0::string::string(const C *)' being compiled
with [ C=AdvertisementType ]
< /code>
< /li>
< /ol>
Ни один подход не работает: < /p>
Первый подход не выполняет во время выполнения с проблемами десериализации. /> Мои вопросы: < /p>
Какой правильный способ сериализации /deserialize enums в Oat ++ при использовании соединителя JSON и MySQL? Используйте Oatpp's enum () Какой шаблон вы используете в производстве, чтобы избежать этих ошибок?>
Подробнее здесь: https://stackoverflow.com/questions/795 ... ime-errors