Skip to content

Commit 677f5d9

Browse files
committed
formatting and correcting the helper
Signed-off-by: Ash-Jose <[email protected]>
1 parent 5944257 commit 677f5d9

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

include/nlohmann/detail/macro_scope.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@
259259
@def NLOHMANN_JSON_SERIALIZE_ENUM_STRICT
260260
@since version 3.12.0
261261
*/
262+
263+
/* helper for strict enum error reporting */
264+
template<typename BasicJsonType>
265+
inline void throw_enum_error(const BasicJsonType& j, const char* enum_type)
266+
{
267+
JSON_THROW(::nlohmann::detail::type_error::create(
268+
302,
269+
std::string("invalid value for ") + enum_type + ": " + j.dump(),
270+
&j));
271+
}
272+
262273
#define NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(ENUM_TYPE, ...) \
263274
template<typename BasicJsonType> \
264275
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
@@ -274,15 +285,7 @@
274285
}); \
275286
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
276287
} \
277-
/* helper for strict enum error reporting */ \
278288
template<typename BasicJsonType> \
279-
inline void throw_enum_error(const BasicJsonType& j, const char* enum_type) \
280-
{ \
281-
JSON_THROW(::nlohmann::detail::type_error::create( \
282-
302, \
283-
std::string("invalid value for ") + enum_type + ": " + j.dump(), \
284-
&j)); \
285-
} \
286289
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
287290
{ \
288291
/* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
@@ -294,11 +297,12 @@
294297
{ \
295298
return ej_pair.second == j; \
296299
}); \
297-
if (it == std::end(m)) \
300+
if (it != std::end(m)) \
298301
{ \
299-
throw_enum_error(j, #ENUM_TYPE); \
302+
e = it->first; \
303+
return; \
300304
} \
301-
e = it->first; \
305+
throw_enum_error(j, #ENUM_TYPE); \
302306
}
303307

304308
// Ugly macros to avoid uglier copy-paste when specializing basic_json. They

single_include/nlohmann/json.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,17 @@ JSON_HEDLEY_DIAGNOSTIC_POP
26232623
@def NLOHMANN_JSON_SERIALIZE_ENUM_STRICT
26242624
@since version 3.12.0
26252625
*/
2626+
2627+
/* helper for strict enum error reporting */
2628+
template<typename BasicJsonType>
2629+
inline void throw_enum_error(const BasicJsonType& j, const char* enum_type)
2630+
{
2631+
JSON_THROW(::nlohmann::detail::type_error::create(
2632+
302,
2633+
std::string("invalid value for ") + enum_type + ": " + j.dump(),
2634+
&j));
2635+
}
2636+
26262637
#define NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(ENUM_TYPE, ...) \
26272638
template<typename BasicJsonType> \
26282639
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
@@ -2638,15 +2649,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP
26382649
}); \
26392650
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
26402651
} \
2641-
/* helper for strict enum error reporting */ \
26422652
template<typename BasicJsonType> \
2643-
inline void throw_enum_error(const BasicJsonType& j, const char* enum_type) \
2644-
{ \
2645-
JSON_THROW(::nlohmann::detail::type_error::create( \
2646-
302, \
2647-
std::string("invalid value for ") + enum_type + ": " + j.dump(), \
2648-
&j)); \
2649-
} \
26502653
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
26512654
{ \
26522655
/* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
@@ -2658,11 +2661,12 @@ JSON_HEDLEY_DIAGNOSTIC_POP
26582661
{ \
26592662
return ej_pair.second == j; \
26602663
}); \
2661-
if (it == std::end(m)) \
2664+
if (it != std::end(m)) \
26622665
{ \
2663-
throw_enum_error(j, #ENUM_TYPE); \
2666+
e = it->first; \
2667+
return; \
26642668
} \
2665-
e = it->first; \
2669+
throw_enum_error(j, #ENUM_TYPE); \
26662670
}
26672671

26682672
// Ugly macros to avoid uglier copy-paste when specializing basic_json. They

0 commit comments

Comments
 (0)