mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 19:50:34 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
286294739a | ||
|
|
1daad8efe0 | ||
|
|
989b8ed841 |
@@ -272,15 +272,6 @@ basic_json(basic_json&& other) noexcept;
|
||||
When used without parentheses around an empty initializer list, `basic_json()` is called instead of this
|
||||
function, yielding the JSON `#!json null` value.
|
||||
|
||||
- Overload 4:
|
||||
|
||||
!!! info "Implicit conversion"
|
||||
|
||||
The conversion is implicit unless [`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md)
|
||||
is defined to `0` and `BasicJsonType::string_t` differs from `string_t`. In that case, the constructor is
|
||||
`explicit`, so a JSON value with a different string type is no longer silently converted, for example when it is
|
||||
passed to a function taking `#!cpp const json&`. Write `#!cpp json(other)` or `#!cpp other.get<json>()` instead.
|
||||
|
||||
- Overload 7:
|
||||
|
||||
!!! info "Preconditions"
|
||||
@@ -429,8 +420,7 @@ basic_json(basic_json&& other) noexcept;
|
||||
1. Since version 1.0.0.
|
||||
2. Since version 1.0.0.
|
||||
3. Since version 2.1.0.
|
||||
4. Since version 3.2.0. Explicit for different string types if `JSON_USE_IMPLICIT_CONVERSIONS` is `0` since
|
||||
version 3.13.0.
|
||||
4. Since version 3.2.0.
|
||||
5. Since version 1.0.0.
|
||||
6. Since version 1.0.0.
|
||||
7. Since version 1.0.0. Fixed in version 3.13.0 to also check the iterator range for binary values; before, a range
|
||||
|
||||
@@ -69,3 +69,5 @@ Logarithmic in the size of the container, O(log(`size()`)).
|
||||
## Version history
|
||||
|
||||
- Since version 2.0.8.
|
||||
- Fixed in version 3.13.0: for [`ordered_json`](../ordered_json.md), the value could previously only be passed as an
|
||||
rvalue; it can now also be passed as an lvalue or a `#!cpp const` lvalue, matching the behavior of `json`.
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
```
|
||||
|
||||
When defined to `0`, implicit conversions are switched off. By default, implicit conversions are switched on. The
|
||||
value directly affects [`operator ValueType`](../basic_json/operator_ValueType.md) and the
|
||||
[converting constructor](../basic_json/basic_json.md) from a `basic_json` specialization with a different string
|
||||
type (overload 4).
|
||||
value directly affects [`operator ValueType`](../basic_json/operator_ValueType.md).
|
||||
|
||||
## Default definition
|
||||
|
||||
@@ -59,25 +57,6 @@ By default, implicit conversions are enabled.
|
||||
auto s = j.get<std::string>();
|
||||
```
|
||||
|
||||
??? example "Conversion between `basic_json` specializations"
|
||||
|
||||
A `basic_json` specialization with a different string type is also no longer converted implicitly when
|
||||
`JSON_USE_IMPLICIT_CONVERSIONS` is defined to `0`:
|
||||
|
||||
```cpp
|
||||
using wjson = nlohmann::basic_json<std::map, std::vector, std::wstring>;
|
||||
|
||||
void load(const nlohmann::json& j);
|
||||
|
||||
wjson wj = /* ... */;
|
||||
load(wj); // error: no implicit conversion
|
||||
load(nlohmann::json(wj)); // OK: explicit conversion
|
||||
load(wj.get<nlohmann::json>()); // OK: explicit conversion
|
||||
```
|
||||
|
||||
Specializations that share the same string type, such as `json` and `ordered_json`, remain implicitly
|
||||
convertible.
|
||||
|
||||
## See also
|
||||
|
||||
- [**operator ValueType**](../basic_json/operator_ValueType.md) - get a value (implicit)
|
||||
@@ -87,4 +66,3 @@ By default, implicit conversions are enabled.
|
||||
## Version history
|
||||
|
||||
- Added in version 3.9.0.
|
||||
- Also affects the conversion between `basic_json` specializations with different string types since version 3.13.0.
|
||||
|
||||
@@ -291,9 +291,7 @@ void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept(std::is_not
|
||||
{
|
||||
if (opt.has_value())
|
||||
{
|
||||
// explicit construction, as the conversion from a basic_json with a different
|
||||
// string type is explicit if JSON_USE_IMPLICIT_CONVERSIONS is 0 (#2649)
|
||||
j = BasicJsonType(*opt);
|
||||
j = *opt;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1606,42 +1606,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
private:
|
||||
/// whether a basic_json specialization can be converted implicitly into this one;
|
||||
/// with JSON_USE_IMPLICIT_CONVERSIONS set to 0, this is only the case if both share
|
||||
/// the same string type (see https://github.com/nlohmann/json/issues/2649)
|
||||
template<typename BasicJsonType>
|
||||
using is_implicitly_convertible_basic_json = std::integral_constant < bool,
|
||||
(JSON_USE_IMPLICIT_CONVERSIONS != 0)
|
||||
|| std::is_same<typename BasicJsonType::string_t, string_t>::value >;
|
||||
|
||||
/// tag to select the constructor that performs the conversion from another basic_json specialization
|
||||
struct convert_basic_json_tag {};
|
||||
|
||||
public:
|
||||
/// @brief create a JSON value from an existing one
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
template < typename BasicJsonType,
|
||||
detail::enable_if_t <
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value
|
||||
&& is_implicitly_convertible_basic_json<BasicJsonType>::value, int > = 0 >
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
|
||||
basic_json(const BasicJsonType& val)
|
||||
: basic_json(val, convert_basic_json_tag{})
|
||||
{}
|
||||
|
||||
/// @brief create a JSON value from an existing one
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
template < typename BasicJsonType,
|
||||
detail::enable_if_t <
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value
|
||||
&& !is_implicitly_convertible_basic_json<BasicJsonType>::value, int > = 0 >
|
||||
explicit basic_json(const BasicJsonType& val)
|
||||
: basic_json(val, convert_basic_json_tag{})
|
||||
{}
|
||||
|
||||
private:
|
||||
template<typename BasicJsonType>
|
||||
basic_json(const BasicJsonType& val, convert_basic_json_tag /*unused*/)
|
||||
#if JSON_DIAGNOSTIC_POSITIONS
|
||||
: start_position(val.start_pos()),
|
||||
end_position(val.end_pos())
|
||||
@@ -1697,7 +1667,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
public:
|
||||
/// @brief create a container (array or object) from an initializer list
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
basic_json(initializer_list_t init,
|
||||
@@ -2464,7 +2433,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
int > = 0 >
|
||||
BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
|
||||
{
|
||||
return BasicJsonType(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -2603,7 +2572,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
int> = 0>
|
||||
ValueType & get_to(ValueType& v) const
|
||||
{
|
||||
v = ValueType(*this);
|
||||
v = *this;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,9 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
|
||||
template<class V, detail::enable_if_t<
|
||||
detail::is_constructible<T, V>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(const key_type& key, V && t)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
{
|
||||
@@ -81,13 +83,14 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return {it, false};
|
||||
}
|
||||
}
|
||||
append(key, std::forward<T>(t));
|
||||
append(key, std::forward<V>(t));
|
||||
return {std::prev(this->end()), true};
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(KeyType && key, T && t)
|
||||
template<class KeyType, class V, detail::enable_if_t<
|
||||
detail::conjunction<detail::is_usable_as_key_type<key_compare, key_type, KeyType>,
|
||||
detail::is_constructible<T, V>>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(KeyType && key, V && t)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
{
|
||||
@@ -96,7 +99,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return {it, false};
|
||||
}
|
||||
}
|
||||
append(std::forward<KeyType>(key), std::forward<T>(t));
|
||||
append(std::forward<KeyType>(key), std::forward<V>(t));
|
||||
return {std::prev(this->end()), true};
|
||||
}
|
||||
|
||||
|
||||
@@ -6822,9 +6822,7 @@ void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept(std::is_not
|
||||
{
|
||||
if (opt.has_value())
|
||||
{
|
||||
// explicit construction, as the conversion from a basic_json with a different
|
||||
// string type is explicit if JSON_USE_IMPLICIT_CONVERSIONS is 0 (#2649)
|
||||
j = BasicJsonType(*opt);
|
||||
j = *opt;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -26624,7 +26622,9 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
|
||||
template<class V, detail::enable_if_t<
|
||||
detail::is_constructible<T, V>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(const key_type& key, V && t)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
{
|
||||
@@ -26633,13 +26633,14 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return {it, false};
|
||||
}
|
||||
}
|
||||
append(key, std::forward<T>(t));
|
||||
append(key, std::forward<V>(t));
|
||||
return {std::prev(this->end()), true};
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(KeyType && key, T && t)
|
||||
template<class KeyType, class V, detail::enable_if_t<
|
||||
detail::conjunction<detail::is_usable_as_key_type<key_compare, key_type, KeyType>,
|
||||
detail::is_constructible<T, V>>::value, int> = 0>
|
||||
std::pair<iterator, bool> emplace(KeyType && key, V && t)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
{
|
||||
@@ -26648,7 +26649,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return {it, false};
|
||||
}
|
||||
}
|
||||
append(std::forward<KeyType>(key), std::forward<T>(t));
|
||||
append(std::forward<KeyType>(key), std::forward<V>(t));
|
||||
return {std::prev(this->end()), true};
|
||||
}
|
||||
|
||||
@@ -28535,42 +28536,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
private:
|
||||
/// whether a basic_json specialization can be converted implicitly into this one;
|
||||
/// with JSON_USE_IMPLICIT_CONVERSIONS set to 0, this is only the case if both share
|
||||
/// the same string type (see https://github.com/nlohmann/json/issues/2649)
|
||||
template<typename BasicJsonType>
|
||||
using is_implicitly_convertible_basic_json = std::integral_constant < bool,
|
||||
(JSON_USE_IMPLICIT_CONVERSIONS != 0)
|
||||
|| std::is_same<typename BasicJsonType::string_t, string_t>::value >;
|
||||
|
||||
/// tag to select the constructor that performs the conversion from another basic_json specialization
|
||||
struct convert_basic_json_tag {};
|
||||
|
||||
public:
|
||||
/// @brief create a JSON value from an existing one
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
template < typename BasicJsonType,
|
||||
detail::enable_if_t <
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value
|
||||
&& is_implicitly_convertible_basic_json<BasicJsonType>::value, int > = 0 >
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
|
||||
basic_json(const BasicJsonType& val)
|
||||
: basic_json(val, convert_basic_json_tag{})
|
||||
{}
|
||||
|
||||
/// @brief create a JSON value from an existing one
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
template < typename BasicJsonType,
|
||||
detail::enable_if_t <
|
||||
detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value
|
||||
&& !is_implicitly_convertible_basic_json<BasicJsonType>::value, int > = 0 >
|
||||
explicit basic_json(const BasicJsonType& val)
|
||||
: basic_json(val, convert_basic_json_tag{})
|
||||
{}
|
||||
|
||||
private:
|
||||
template<typename BasicJsonType>
|
||||
basic_json(const BasicJsonType& val, convert_basic_json_tag /*unused*/)
|
||||
#if JSON_DIAGNOSTIC_POSITIONS
|
||||
: start_position(val.start_pos()),
|
||||
end_position(val.end_pos())
|
||||
@@ -28626,7 +28597,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
public:
|
||||
/// @brief create a container (array or object) from an initializer list
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
basic_json(initializer_list_t init,
|
||||
@@ -29393,7 +29363,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
int > = 0 >
|
||||
BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
|
||||
{
|
||||
return BasicJsonType(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -29532,7 +29502,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
int> = 0>
|
||||
ValueType & get_to(ValueType& v) const
|
||||
{
|
||||
v = ValueType(*this);
|
||||
v = *this;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -420,41 +419,6 @@ TEST_CASE("alternative string type")
|
||||
CHECK(j2.dump() == R"({"/foo/0":"bar","/foo/1":"baz"})");
|
||||
}
|
||||
|
||||
SECTION("conversion between basic_json specializations (#2649)")
|
||||
{
|
||||
// explicit conversions are always possible
|
||||
CHECK(std::is_constructible<nlohmann::json, alt_json>::value);
|
||||
CHECK(std::is_constructible<alt_json, nlohmann::json>::value);
|
||||
CHECK(std::is_constructible<nlohmann::json, nlohmann::ordered_json>::value);
|
||||
CHECK(std::is_constructible<nlohmann::ordered_json, nlohmann::json>::value);
|
||||
|
||||
// specializations with the same string type are implicitly convertible
|
||||
CHECK(std::is_convertible<nlohmann::ordered_json, nlohmann::json>::value);
|
||||
CHECK(std::is_convertible<nlohmann::json, nlohmann::ordered_json>::value);
|
||||
|
||||
// specializations with different string types are only implicitly convertible
|
||||
// if implicit conversions are enabled
|
||||
#if JSON_USE_IMPLICIT_CONVERSIONS
|
||||
CHECK(std::is_convertible<alt_json, nlohmann::json>::value);
|
||||
CHECK(std::is_convertible<nlohmann::json, alt_json>::value);
|
||||
#else
|
||||
CHECK_FALSE(std::is_convertible<alt_json, nlohmann::json>::value);
|
||||
CHECK_FALSE(std::is_convertible<nlohmann::json, alt_json>::value);
|
||||
#endif
|
||||
|
||||
// get<BasicJsonType>() works in either case
|
||||
const nlohmann::json j = {{"foo", 1}, {"bar", true}};
|
||||
CHECK(j.get<nlohmann::ordered_json>() == nlohmann::ordered_json(j));
|
||||
// (only a number is converted here, as objects and strings are affected by #3425)
|
||||
CHECK(nlohmann::json(42).get<alt_json>() == 42);
|
||||
CHECK(alt_json(nlohmann::json(42)) == 42);
|
||||
|
||||
// get_to() also works in either case
|
||||
alt_json a;
|
||||
nlohmann::json(42).get_to(a);
|
||||
CHECK(a == 42);
|
||||
}
|
||||
|
||||
SECTION("strict enum")
|
||||
{
|
||||
// regression test for #5667: NLOHMANN_JSON_SERIALIZE_ENUM_STRICT's from_json
|
||||
|
||||
@@ -196,3 +196,44 @@ TEST_CASE("regression test - diff() must account for ordered_json member order")
|
||||
CHECK(a.patch(p) == b);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("regression test for issue #5673 - ordered_json::emplace with a non-rvalue value")
|
||||
{
|
||||
SECTION("lvalue value")
|
||||
{
|
||||
ordered_json oj = ordered_json::object();
|
||||
ordered_json value = 1;
|
||||
auto res = oj.emplace("a", value);
|
||||
CHECK(res.second == true);
|
||||
CHECK(oj.dump() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
SECTION("const lvalue value")
|
||||
{
|
||||
ordered_json oj = ordered_json::object();
|
||||
const ordered_json value = 1;
|
||||
auto res = oj.emplace("a", value);
|
||||
CHECK(res.second == true);
|
||||
CHECK(oj.dump() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
SECTION("rvalue value")
|
||||
{
|
||||
ordered_json oj = ordered_json::object();
|
||||
auto res = oj.emplace("a", ordered_json(1));
|
||||
CHECK(res.second == true);
|
||||
CHECK(oj.dump() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
SECTION("existing key is not overwritten (std::map-compatible semantics)")
|
||||
{
|
||||
ordered_json oj = ordered_json::object();
|
||||
ordered_json value = 1;
|
||||
oj.emplace("a", value);
|
||||
|
||||
ordered_json other_value = 2;
|
||||
auto res = oj.emplace("a", other_value);
|
||||
CHECK(res.second == false);
|
||||
CHECK(oj.dump() == "{\"a\":1}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,6 +403,79 @@ TEST_CASE("ordered_map")
|
||||
CHECK(om.size() == 4);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("emplace")
|
||||
{
|
||||
// regression test for issue #5673: the mapped-value parameter must
|
||||
// accept lvalues and const lvalues, not just rvalues
|
||||
ordered_map<std::string, std::string> om;
|
||||
om["eins"] = "one";
|
||||
om["zwei"] = "two";
|
||||
om["drei"] = "three";
|
||||
|
||||
SECTION("with T&& (rvalue)")
|
||||
{
|
||||
auto res1 = om.emplace("eins", std::string("1"));
|
||||
CHECK(res1.first == om.begin());
|
||||
CHECK(res1.second == false);
|
||||
CHECK(om.size() == 3);
|
||||
CHECK(om.at("eins") == "one"); // existing key is not overwritten
|
||||
|
||||
auto res4 = om.emplace("vier", std::string("four"));
|
||||
CHECK(res4.first == om.begin() + 3);
|
||||
CHECK(res4.second == true);
|
||||
CHECK(om.size() == 4);
|
||||
CHECK(om.at("vier") == "four");
|
||||
}
|
||||
|
||||
SECTION("with T& (lvalue)")
|
||||
{
|
||||
std::string one = "1";
|
||||
std::string four = "four";
|
||||
|
||||
auto res1 = om.emplace("eins", one);
|
||||
CHECK(res1.first == om.begin());
|
||||
CHECK(res1.second == false);
|
||||
CHECK(om.size() == 3);
|
||||
CHECK(om.at("eins") == "one"); // existing key is not overwritten
|
||||
|
||||
auto res4 = om.emplace("vier", four);
|
||||
CHECK(res4.first == om.begin() + 3);
|
||||
CHECK(res4.second == true);
|
||||
CHECK(om.size() == 4);
|
||||
CHECK(om.at("vier") == "four");
|
||||
CHECK(four == "four"); // source was copied, not moved from
|
||||
}
|
||||
|
||||
SECTION("with const T&")
|
||||
{
|
||||
const std::string one = "1";
|
||||
const std::string four = "four";
|
||||
|
||||
auto res1 = om.emplace("eins", one);
|
||||
CHECK(res1.first == om.begin());
|
||||
CHECK(res1.second == false);
|
||||
CHECK(om.size() == 3);
|
||||
|
||||
auto res4 = om.emplace("vier", four);
|
||||
CHECK(res4.first == om.begin() + 3);
|
||||
CHECK(res4.second == true);
|
||||
CHECK(om.size() == 4);
|
||||
CHECK(om.at("vier") == "four");
|
||||
}
|
||||
|
||||
SECTION("with key of key_type (non-template overload)")
|
||||
{
|
||||
const std::string key_vier{"vier"};
|
||||
std::string four = "four";
|
||||
|
||||
auto res4 = om.emplace(key_vier, four);
|
||||
CHECK(res4.first == om.begin() + 3);
|
||||
CHECK(res4.second == true);
|
||||
CHECK(om.size() == 4);
|
||||
CHECK(om.at("vier") == "four");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ordered_map growth")
|
||||
|
||||
Reference in New Issue
Block a user