-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add strict enum de/serialization macro #4612
base: develop
Are you sure you want to change the base?
Conversation
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @hnampally |
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md
Outdated
Show resolved
Hide resolved
docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp
Outdated
Show resolved
Hide resolved
docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp
Outdated
Show resolved
Hide resolved
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @hnampally |
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp
Outdated
Show resolved
Hide resolved
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
template<typename T> | ||
[[noreturn]] inline void json_throw_from_serialize_macro(T&& exception) | ||
{ | ||
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When defining JSON_THROW
in the same file, we use the following code to detect exceptions. Please use the same here to avoid issues.
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh okay, thanks!
}); \ | ||
if (it == std::end(m)) { \ | ||
auto value = static_cast<typename std::underlying_type<ENUM_TYPE>::type>(e); \ | ||
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the exception message. What about
serialization failed: enum value ... is out of range
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that sounds better, thanks!
return ej_pair.second == j; \ | ||
}); \ | ||
if (it == std::end(m)) \ | ||
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
deserialization failed: invalid JSON value '...'
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md
Outdated
Show resolved
Hide resolved
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md
Outdated
Show resolved
Hide resolved
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md
Outdated
Show resolved
Hide resolved
|
||
- If an enum value appears more than once in the mapping, only the first occurrence will be used for serialization, | ||
subsequent mappings for the same enum value will be ignored. | ||
- If a JSON value appears more than once in the mapping, only the first occurrence will be used for deserialization, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"If a string value" since it's not an arbitrary json value but a string....
Oh, OH, it doesn't require that it be a string, does it?
😮
There's nothing in the code that requires that this be a string.
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color, {
{ Red, "red" },
{ Green, nlohmann::json(1) },
{ Blue, nlohmann::json({{{"purple", "people eater"}}})}
})
int main()
{
nlohmann::json j;
j = { Red, Green, Blue };
std::cout << j.dump() << "\n";
auto j2 = nlohmann::json::parse(j.dump());
std::vector<Color> vec = j2;
std::cout << vec[0] << vec[1] << vec[2];
}
["red",1,[{"purple":"people eater"}]]
012
@hnampally Is this intended behavior, or did this just accidentally fall out of the second parameter being BasicJsonType
instead of std::string
?
If this is intended behavior, it should definitely be documented before someone trips upon it accidentally, or someone relies on it and then someone else "fixes" it.
If it's not intended behavior, then it should be fixed.
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
Expected output: | ||
|
||
``` | ||
[json.exception.type_error.302] serialization failed: enum value 3 is out of range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also use an include here:
--8<-- "examples/nlohmann_json_serialize_enum_strict.output"
Expected output: | ||
|
||
``` | ||
[json.exception.type_error.302] deserialization failed: invalid JSON value "yellow" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also use an include here:
--8<-- "examples/nlohmann_json_deserialize_enum_strict.output"
|
||
int main() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the empty line.
Also: move line
json j_yellow = "yellow";
above the comment
// deserialization
It looks like you have a space in your filename here: Also, the discussion about strings vs arbitrary json for the value has been marked outdated because there was a change in the file. @nlohmann did you see that discussion? |
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
This looks particularly useful, I was surprised by the behavior of the default enum serialization macro. |
#3992
make amalgamate
.Read the Contribution Guidelines for detailed information.