Repository navigation
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>
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>
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.
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)| }); \ | ||
| 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.
I don't like the exception message. What about
serialization failed: enum value ... is out of range
There was a problem hiding this comment.
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.
Same as above:
deserialization failed: invalid JSON value '...'
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
|
|
||
| - 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.
"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.
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.
Also use an include here:
--8<-- "examples/nlohmann_json_deserialize_enum_strict.output"
|
|
||
| int main() | ||
| { | ||
|
|
There was a problem hiding this comment.
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. |
|
This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions! |
|
@hnampally I know it's been a while, but would you be willing to pick this up again? |
@nlohmann Sure, let me give this PR a quick re-read and hopefully we can get it merged/closed. |
|
#5151 solves the same issue and ready to go, therefore cloing this PR |
|
@hnampally Sorry for the confusion! |
#3992
make amalgamate.Read the Contribution Guidelines for detailed information.