mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 03:30:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f644908e5 |
@@ -7,15 +7,15 @@ void clear() noexcept;
|
||||
Clears the content of a JSON value and resets it to the default value as if [`basic_json(value_t)`](basic_json.md) would
|
||||
have been called with the current value type from [`type()`](type.md):
|
||||
|
||||
| Value type | initial value |
|
||||
|------------|----------------------|
|
||||
| null | `null` |
|
||||
| boolean | `false` |
|
||||
| string | `""` |
|
||||
| number | `0` |
|
||||
| binary | An empty byte vector |
|
||||
| object | `{}` |
|
||||
| array | `[]` |
|
||||
| Value type | initial value |
|
||||
|------------|-----------------------------------------|
|
||||
| null | `null` |
|
||||
| boolean | `false` |
|
||||
| string | `""` |
|
||||
| number | `0` |
|
||||
| binary | An empty byte vector with no subtype |
|
||||
| object | `{}` |
|
||||
| array | `[]` |
|
||||
|
||||
Has the same effect as calling
|
||||
|
||||
@@ -56,3 +56,4 @@ All iterators, pointers, and references related to this container are invalidate
|
||||
|
||||
- Added in version 1.0.0.
|
||||
- Added support for binary types in version 3.8.0.
|
||||
- Fixed in version 3.13.0 to also clear the subtype of a binary value; before, the subtype was left unchanged.
|
||||
|
||||
@@ -167,13 +167,9 @@ template<typename BasicJsonType, typename EnumType,
|
||||
enable_if_t<std::is_enum<EnumType>::value, int> = 0>
|
||||
inline void from_json(const BasicJsonType& j, EnumType& e)
|
||||
{
|
||||
using underlying_type = typename std::underlying_type<EnumType>::type;
|
||||
// get_arithmetic_value() does not accept boolean_t; read the number that to_json() wrote instead
|
||||
using value_type = typename std::conditional<std::is_same<underlying_type, typename BasicJsonType::boolean_t>::value,
|
||||
typename BasicJsonType::number_unsigned_t, underlying_type>::type;
|
||||
value_type val;
|
||||
typename std::underlying_type<EnumType>::type val;
|
||||
get_arithmetic_value(j, val);
|
||||
e = static_cast<EnumType>(static_cast<underlying_type>(val));
|
||||
e = static_cast<EnumType>(val);
|
||||
}
|
||||
#endif // JSON_DISABLE_ENUM_SERIALIZATION
|
||||
|
||||
|
||||
@@ -3833,6 +3833,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
case value_t::binary:
|
||||
{
|
||||
m_data.m_value.binary->clear();
|
||||
m_data.m_value.binary->clear_subtype();
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -5664,13 +5664,9 @@ template<typename BasicJsonType, typename EnumType,
|
||||
enable_if_t<std::is_enum<EnumType>::value, int> = 0>
|
||||
inline void from_json(const BasicJsonType& j, EnumType& e)
|
||||
{
|
||||
using underlying_type = typename std::underlying_type<EnumType>::type;
|
||||
// get_arithmetic_value() does not accept boolean_t; read the number that to_json() wrote instead
|
||||
using value_type = typename std::conditional<std::is_same<underlying_type, typename BasicJsonType::boolean_t>::value,
|
||||
typename BasicJsonType::number_unsigned_t, underlying_type>::type;
|
||||
value_type val;
|
||||
typename std::underlying_type<EnumType>::type val;
|
||||
get_arithmetic_value(j, val);
|
||||
e = static_cast<EnumType>(static_cast<underlying_type>(val));
|
||||
e = static_cast<EnumType>(val);
|
||||
}
|
||||
#endif // JSON_DISABLE_ENUM_SERIALIZATION
|
||||
|
||||
@@ -29918,6 +29914,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
case value_t::binary:
|
||||
{
|
||||
m_data.m_value.binary->clear();
|
||||
m_data.m_value.binary->clear_subtype();
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1358,14 +1358,6 @@ TEST_CASE("value conversion")
|
||||
CHECK(json(value_1).get<c_enum>() == value_1);
|
||||
CHECK(json(cpp_enum::value_1).get<cpp_enum>() == cpp_enum::value_1);
|
||||
}
|
||||
|
||||
SECTION("get an enum with underlying type bool (#5671)")
|
||||
{
|
||||
enum class bool_enum : bool { off, on };
|
||||
|
||||
CHECK(json(bool_enum::off).get<bool_enum>() == bool_enum::off);
|
||||
CHECK(json(bool_enum::on).get<bool_enum>() == bool_enum::on);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("more involved conversions")
|
||||
|
||||
@@ -155,6 +155,18 @@ TEST_CASE("modifiers")
|
||||
CHECK(j == json(json::value_t::binary));
|
||||
CHECK(j == json(k.type()));
|
||||
}
|
||||
|
||||
SECTION("filled binary with subtype")
|
||||
{
|
||||
json j = json::binary({1, 2, 3, 4, 5}, 42);
|
||||
json const k = j;
|
||||
|
||||
j.clear();
|
||||
CHECK(!j.empty());
|
||||
CHECK(!j.get_binary().has_subtype());
|
||||
CHECK(j == json(json::value_t::binary));
|
||||
CHECK(j == json(k.type()));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("number (integer)")
|
||||
|
||||
Reference in New Issue
Block a user