Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

add strict enum de/serialization macro#4612

Open
hnampally wants to merge 13 commits intonlohmann:developfrom
hnampally:issue-3992
Open

add strict enum de/serialization macro#4612
hnampally wants to merge 13 commits intonlohmann:developfrom
hnampally:issue-3992

Conversation

@hnampally
Copy link
Contributor

@hnampallyhnampally commentedJan 20, 2025
edited
Loading

#3992

  • The changes are described in detail, both the what and why.
  • If applicable, an#3992 is referenced.
  • TheCode coverage remained at 100%. A test case for every new line of code.
  • If applicable, thedocumentation is updated.
  • The source code is amalgamated by runningmake amalgamate.

Read theContribution Guidelines for detailed information.

Signed-off-by: Harinath Nampally <harinath922@gmail.com>
@github-actions
Copy link

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated.@hnampally
Please read and follow theContribution Guidelines.

@coveralls
Copy link

coveralls commentedJan 20, 2025
edited
Loading

Coverage Status

coverage: 99.186%. remained the same
when pulling6747555 on hnampally:issue-3992
into606b634 on nlohmann:develop.

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>
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
@github-actions
Copy link

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated.@hnampally
Please read and follow theContribution Guidelines.

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When definingJSON_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)

hnampally reacted with thumbs up emoji
Copy link
ContributorAuthor

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)); \
Copy link
Owner

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

hnampally reacted with thumbs up emoji
Copy link
ContributorAuthor

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)); \
Copy link
Owner

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 '...'

hnampally reacted with thumbs up emoji
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,
Copy link
Contributor

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 beingBasicJsonType instead ofstd::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
Copy link
Owner

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"

hnampally reacted with thumbs up emoji
Expected output:

```
[json.exception.type_error.302] deserialization failed: invalid JSON value "yellow"
Copy link
Owner

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"

hnampally reacted with thumbs up emoji

int main()
{

Copy link
Owner

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

hnampally reacted with thumbs up emoji
@gregmarr
Copy link
Contributor

It looks like you have a space in your filename here:
docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum _strict.md

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?

hnampally reacted with thumbs up emoji

Signed-off-by: Harinath Nampally <harinath922@gmail.com>
@jmonticelli
Copy link

jmonticelli commentedApr 1, 2025
edited
Loading

This looks particularly useful, I was surprised by the behavior of the default enum serialization macro.
Thanks for putting this PR up btw@hnampally

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nlohmannnlohmannAwaiting requested review from nlohmannnlohmann is a code owner

1 more reviewer

@gregmarrgregmarrgregmarr left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@hnampally@coveralls@gregmarr@jmonticelli@nlohmann

[8]ページ先頭

©2009-2026 Movatter.jp