【发布时间】:2021-08-31 20:20:13
【问题描述】:
是否可以在 nlohmann::json(https://github.com/nlohmann/json) 中使用更好的枚举(https://github.com/aantron/better-enums)?
我正在尝试将整数/短字段迁移到 better_enum 生成的类。我确实尝试在编译时添加 to_json 和 from_json 方法。
错误:static_assert 由于要求而失败 'std::is_default_constructiblestudents::GRADE::value' "类型必须是 与 get() 一起使用时 DefaultConstructible"
这是我尝试使用的代码示例
学生.hpp
namespace students {
BETTER_ENUM(GRADE, short,
GRADE_1,
GRADE_2)
struct Student {
std::string name;
GRADE grade;
};
inline nlohmann::json get_untyped(const nlohmann::json &j, const char *property) {
if (j.find(property) != j.end()) {
return j.at(property).get<nlohmann::json>();
}
return {};
}
inline nlohmann::json get_untyped(const nlohmann::json &j, const std::string &property) {
return get_untyped(j, property.data());
}
void from_json(const nlohmann::json &j, students::GRADE &x);
void to_json(nlohmann::json &j, const students::GRADE &x);
void from_json(const nlohmann::json &j, students::Student &x);
void to_json(nlohmann::json &j, const students::Student &x);
inline void students::from_json(const nlohmann::json &j, GRADE &x) {
x = GRADE::_from_integral(j.at("grade").get<short>());
}
inline void students::to_json(nlohmann::json &j, const GRADE &x) {
j["grade"] = x._to_integral();
}
inline void from_json(const nlohmann::json &j, students::Student &x) {
x.name = j.at("name").get<std::string>();
x.grade = j.at("grade").get<GRADE>();
}
inline void to_json(nlohmann::json &j, const students::Student &x) {
j = nlohmann::json::object();
j["name"] = x.name;
j["grade"] = x.grade;
}
}
文件存储代码:
students::Student student1 = {"vs", students::GRADE::GRADE_1};
nlohmann::json jsonObject = student1;
std::ofstream studentFile("/Users/vs/Projects/test/data/students.json");
studentFile << std::setw(4) << jsonObject << std::endl;
【问题讨论】:
标签: c++ json enums nlohmann-json