-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Open
Labels
kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationa fix for the issue has been proposed and waits for confirmation
Description
Description
I want to get enum value or use default one if it has error, but I can't
sample code:
#include <nlohmann/json.hpp>
#include <iostream>
#include <string>
enum class UserRequestType {
GetUser,
AddUser,
Unknown
};
// clang-format off
NLOHMANN_JSON_SERIALIZE_ENUM(UserRequestType, {
{UserRequestType::GetUser, "get"},
{UserRequestType::AddUser, "add"},
{UserRequestType::Unknown, "unknown"}
})
// clang-format on
int main(int argc, char const *argv[]) {
std::string jtext = "{\"type\": \"invalidtype\"}";
nlohmann::json j = nlohmann::json::parse(jtext);
UserRequestType t = j.value("type", UserRequestType::Unknown);
std::cout << static_cast<int>(t) << '\n'; // got 0 (UserRequestType::GetUser)
return 0;
}I know I can use j["type"].get<UserRequestType>() and reorder the enum to use first one as default, but json.value(string, UserRequestType has a bug
Reproduction steps
compile and run the code
Expected vs. actual results
expected j["type"].get<UserRequestType>() throw exception and j.value("type", UserRequestType::Unknown) return default one
Minimal code example
Error messages
Compiler and operating system
Ubuntu 24.04, GCC 13.3.0
Library version
3.11.3-1
Validation
- The bug also occurs if the latest version from the
developbranch is used. - I can successfully compile and run the unit tests.
Metadata
Metadata
Assignees
Labels
kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationa fix for the issue has been proposed and waits for confirmation