Skip to content

Commit ff66dad

Browse files
committed
modified NLOHMANN_JSON_SERIALIZE_STRICT to throw
Signed-off-by: Caillin Nugent <caillinn@student.unimelb.edu.au>
1 parent 16e2669 commit ff66dad

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

include/nlohmann/detail/macro_scope.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@
272272
{ \
273273
return ej_pair.first == e; \
274274
}); \
275-
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
275+
if (it != std::end(m)) j = it->second; \
276+
else throw nlohmann::detail::out_of_range::create(403, "enum value out of range", nullptr);\
276277
} \
277278
template<typename BasicJsonType> \
278279
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
@@ -286,7 +287,8 @@
286287
{ \
287288
return ej_pair.second == j; \
288289
}); \
289-
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
290+
if (it != std::end(m)) e = it->first; \
291+
else throw nlohmann::detail::out_of_range::create(403, "enum value out of range", nullptr);\
290292
}
291293

292294

tests/src/unit-conversions.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ TEST_CASE("JSON to enum mapping")
16601660
enum class strict_cards {kreuz, pik, herz, karo};
16611661

16621662
// NOLINTNEXTLINE(misc-use-internal-linkage,misc-const-correctness,cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - false positive
1663-
NLOHMANN_JSON_SERIALIZE_ENUM(strict_cards,
1663+
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(strict_cards,
16641664
{
16651665
{strict_cards::kreuz, "kreuz"},
16661666
{strict_cards::pik, "pik"},
@@ -1678,7 +1678,7 @@ enum StrictTaskState // NOLINT(cert-int09-c,readability-enum-initial-value,cppco
16781678
};
16791679

16801680
// NOLINTNEXTLINE(misc-const-correctness,misc-use-internal-linkage,cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - false positive
1681-
NLOHMANN_JSON_SERIALIZE_ENUM(StrictTaskState,
1681+
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(StrictTaskState,
16821682
{
16831683
{STRICT_TS_INVALID, nullptr},
16841684
{STRICT_TS_STOPPED, "stopped"},
@@ -1703,9 +1703,8 @@ TEST_CASE("Strict JSON to enum mapping")
17031703
CHECK(strict_cards::karo == json("karo"));
17041704

17051705
// invalid json -> exception thrown
1706-
CHECK(strict_cards::kreuz == json("what?").get<strict_cards>());
17071706
json _;
1708-
CHECK_THROWS_WITH_AS(_ = json("what?").get<strict_cards>(), "[json.exception.out_of_range.403] enum value 'what?' out of range", json::out_of_range&);
1707+
CHECK_THROWS_WITH_AS(_ = json("what?").get<strict_cards>(), "[json.exception.out_of_range.403] enum value out of range", json::out_of_range&);
17091708
}
17101709

17111710
SECTION("traditional enum")
@@ -1724,7 +1723,7 @@ TEST_CASE("Strict JSON to enum mapping")
17241723

17251724
// invalid json -> exception thrown
17261725
json _;
1727-
CHECK_THROWS_WITH_AS(_ = json("what?").get<StrictTaskState>(), "[json.exception.out_of_range.403] enum value 'what?' out of range", json::out_of_range&);
1726+
CHECK_THROWS_WITH_AS(_ = json("what?").get<StrictTaskState>(), "[json.exception.out_of_range.403] enum value out of range", json::out_of_range&);
17281727
}
17291728
}
17301729

0 commit comments

Comments
 (0)