Skip to content

Conversation

@Ash-Jose
Copy link

@Ash-Jose Ash-Jose commented Nov 4, 2025

Summary

This PR fixes #3992 by introducing a new macro
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT, which throws a json.exception.type_error.302
when deserializing a JSON string that does not match any enumerator.

The existing NLOHMANN_JSON_SERIALIZE_ENUM macro remains unchanged to preserve backward compatibility.


Changes

  • Added new macro NLOHMANN_JSON_SERIALIZE_ENUM_STRICT in macro_scope.hpp that throws type_error.302 for unmapped enum strings.
  • Added new test file unit-serialize_enum_strict.cpp validating the strict throwing behavior.
  • Regenerated single-header files using make amalgamate.
  • Verified all tests using ctest --output-on-failure.

Expected test impact

All existing tests for NLOHMANN_JSON_SERIALIZE_ENUM continue to pass unchanged.
New tests for NLOHMANN_JSON_SERIALIZE_ENUM_STRICT confirm that unknown JSON values now trigger exceptions.

Other unrelated floating-point mismatches (e.g., test-regression1_cpp11) are not affected by this change.


Documentation

A new documentation entry will be added at
docs/api/macros/nlohmann_json_serialize_enum_strict.md describing the stricter deserialization behavior.


Checklist

  • Implementation complete
  • New strict macro added (NLOHMANN_JSON_SERIALIZE_ENUM_STRICT)
  • New test added and passing
  • Single-header regenerated (make amalgamate)
  • Backward compatibility preserved
  • DCO signed
  • Documentation pending maintainer confirmation

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

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

Please do not change the existing macros. This would break existing applications. Instead, add a macro NLOHMANN_JSON_SERIALIZE_ENUM_STRICT with the new behavior.

@Ash-Jose Ash-Jose force-pushed the fix/enum-deser-throw-on-unknown branch from 95a9a52 to 28866e1 Compare November 4, 2025 22:35
@Ash-Jose Ash-Jose changed the title Throw type_error.302 on unknown JSON value in NLOHMANN_JSON_SERIALIZE_ENUM (Fixes #3992) Add NLOHMANN_JSON_SERIALIZE_ENUM_STRICT macro that throws type_error.302 on unknown JSON value (Fixes #3992) Nov 4, 2025
@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 4, 2025

i have made changes accordingly pls review

@github-actions
Copy link

github-actions bot commented Nov 4, 2025

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @Ash-Jose
Please read and follow the Contribution Guidelines.

@coveralls
Copy link

coveralls commented Nov 4, 2025

Coverage Status

coverage: 99.192% (+0.001%) from 99.191%
when pulling 1231904 on Ash-Jose:fix/enum-deser-throw-on-unknown
into 49026f7 on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

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

  • Please add a new line at the end of unit-serialize_enum_strict.cpp.
  • Please run make amalgamation to properly indent the code.

@Ash-Jose Ash-Jose requested a review from nlohmann November 5, 2025 09:23
@github-actions
Copy link

github-actions bot commented Nov 5, 2025

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @Ash-Jose
Please read and follow the Contribution Guidelines.

@Ash-Jose Ash-Jose force-pushed the fix/enum-deser-throw-on-unknown branch from 69b82a1 to a18ff7c Compare November 5, 2025 09:57
@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

is this because of using concat
amalgamation check seems to have passed this time around

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2025

is this because of using concat amalgamation check seems to have passed this time around

You need to include #include <nlohmann/detail/string_concat.hpp>.

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

included the header

@github-actions
Copy link

github-actions bot commented Nov 5, 2025

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @Ash-Jose
Please read and follow the Contribution Guidelines.

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

why are tests still failing??

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2025

Maybe some issue with the includes. Please try:

  • remove the string_concat.hpp include
  • revert the code to using std::string()... like you did before

Sorry for the confusion.

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

i have reverted it

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

should i redo in a different branch??
i dont understand why checks are failing

}); \
if (it == std::end(m)) \
{ \
throw ::nlohmann::detail::type_error::create( \
Copy link
Owner

Choose a reason for hiding this comment

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

This should need the exceptions.hpp header which again includes macro_scope.hpp. I think this is the core of the issue.

Copy link
Author

Choose a reason for hiding this comment

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

ill try including the exceptions header

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, this needs to use JSON_THROW.

Copy link
Contributor

Choose a reason for hiding this comment

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

This still needs to be changed, or this macro can't be used by people that disable exceptions.

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

i dont understand why they still fail

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2025

Because there is more to forward-declare. I'm sorry, but it seems to be a more complicated issue to use an exception in the macro header.

@gregmarr
Copy link
Contributor

gregmarr commented Nov 5, 2025

The forward declarations aren't required for the macro definition, they're required where the things are used.

I think the issue is that it's using the detail namespace. I don't think we do that for any of the other user space macros.
Nope, simple type issue.

throw ::nlohmann::detail::type_error::create( \
302, \
std::string("invalid value for ") + #ENUM_TYPE + ": " + j.dump(), \
j); \
Copy link
Contributor

Choose a reason for hiding this comment

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

This is your problem, it takes a pointer, it should be &j.

{
class type_error;
} // namespace detail
} // namespace nlohmann
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed.

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

i have made changes accordingly
this was the first version of my change
although back then i wasnt able to make amalgamate work

@Ash-Jose
Copy link
Author

Ash-Jose commented Nov 5, 2025

its still failing a few tests
is that concerning??

test.json Outdated
@@ -0,0 +1,7 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should not be in the root of the repo. Is this just from local testing and got committed accidentally?

Copy link
Author

Choose a reason for hiding this comment

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

my bad ill take it out

Ash-Jose and others added 16 commits November 14, 2025 01:58
…_JSON_SERIALIZE_ENUM_STRICT

- Added missing include for <nlohmann/detail/string_concat.hpp>
- Replaced string concatenation with concat() call as suggested by maintainer
- Re-ran `make amalgamate` to update single-header files

Signed-off-by: Ash-Jose <[email protected]>
included string header
Signed-off-by: Ash-Jose <[email protected]>
Signed-off-by: Ash Jose <[email protected]>
@Ash-Jose Ash-Jose closed this Nov 13, 2025
@Ash-Jose Ash-Jose deleted the fix/enum-deser-throw-on-unknown branch November 13, 2025 21:21
@Ash-Jose Ash-Jose restored the fix/enum-deser-throw-on-unknown branch November 13, 2025 21:25
@Ash-Jose Ash-Jose deleted the fix/enum-deser-throw-on-unknown branch November 13, 2025 21:34
@Ash-Jose Ash-Jose reopened this Nov 13, 2025
@Ash-Jose Ash-Jose force-pushed the fix/enum-deser-throw-on-unknown branch from 9cadcca to d191b5d Compare November 13, 2025 22:46
@Ash-Jose Ash-Jose force-pushed the fix/enum-deser-throw-on-unknown branch from d191b5d to 1231904 Compare November 13, 2025 22:48
@Ash-Jose
Copy link
Author

are my changes sufficient this time around?
i will make the signoff change to avoid the DCO error
also this pr has become quite messy with all my unecessary commits
if you want i will submit another pr with only the necessary commits to keep things clean

@gregmarr
Copy link
Contributor

@nlohmann Looks like the workflows here are awaiting approval. Not sure why since they ran before.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NLOHMANN_JSON_SERIALIZE_ENUM defaulting to the first enum value is surprising behavior

4 participants