Unverified Commit 47c2004f authored by Niels Lohmann's avatar Niels Lohmann

Merge branch 'develop' of https://github.com/nlohmann/json into develop

parents fc4040ce 260be043
...@@ -231,6 +231,15 @@ Please file issues [here](https://github.com/build2-packaging/nlohmann-json) if ...@@ -231,6 +231,15 @@ Please file issues [here](https://github.com/build2-packaging/nlohmann-json) if
If you are using [`wsjcpp`](https://wsjcpp.org), you can use the command `wsjcpp install "https://github.com/nlohmann/json:develop"` to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch. If you are using [`wsjcpp`](https://wsjcpp.org), you can use the command `wsjcpp install "https://github.com/nlohmann/json:develop"` to get the latest version. Note you can change the branch ":develop" to an existing tag or another branch.
If you are using [`CPM.cmake`](https://github.com/TheLartians/CPM.cmake), you can check this [`example`](https://github.com/TheLartians/CPM.cmake/tree/master/examples/json). After [adding CPM script](https://github.com/TheLartians/CPM.cmake#adding-cpm) to your project, implement the following snippet to your CMake:
```cmake
CPMAddPackage(
NAME nlohmann_json
GITHUB_REPOSITORY nlohmann/json
VERSION 3.9.1)
```
### Pkg-config ### Pkg-config
If you are using bare Makefiles, you can use `pkg-config` to generate the include flags that point to where the library is installed: If you are using bare Makefiles, you can use `pkg-config` to generate the include flags that point to where the library is installed:
......
...@@ -39,15 +39,15 @@ class byte_container_with_subtype : public BinaryType ...@@ -39,15 +39,15 @@ class byte_container_with_subtype : public BinaryType
: container_type(std::move(b)) : container_type(std::move(b))
{} {}
byte_container_with_subtype(const container_type& b, std::uint8_t subtype) noexcept(noexcept(container_type(b))) byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b)))
: container_type(b) : container_type(b)
, m_subtype(subtype) , m_subtype(subtype_)
, m_has_subtype(true) , m_has_subtype(true)
{} {}
byte_container_with_subtype(container_type&& b, std::uint8_t subtype) noexcept(noexcept(container_type(std::move(b)))) byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b))))
: container_type(std::move(b)) : container_type(std::move(b))
, m_subtype(subtype) , m_subtype(subtype_)
, m_has_subtype(true) , m_has_subtype(true)
{} {}
...@@ -80,9 +80,9 @@ class byte_container_with_subtype : public BinaryType ...@@ -80,9 +80,9 @@ class byte_container_with_subtype : public BinaryType
@since version 3.8.0 @since version 3.8.0
*/ */
void set_subtype(std::uint8_t subtype) noexcept void set_subtype(std::uint8_t subtype_) noexcept
{ {
m_subtype = subtype; m_subtype = subtype_;
m_has_subtype = true; m_has_subtype = true;
} }
......
...@@ -17,19 +17,14 @@ class json_ref ...@@ -17,19 +17,14 @@ class json_ref
json_ref(value_type&& value) json_ref(value_type&& value)
: owned_value(std::move(value)) : owned_value(std::move(value))
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
json_ref(const value_type& value) json_ref(const value_type& value)
: value_ref(const_cast<value_type*>(&value)) : value_ref(&value)
, is_rvalue(false)
{} {}
json_ref(std::initializer_list<json_ref> init) json_ref(std::initializer_list<json_ref> init)
: owned_value(init) : owned_value(init)
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
template < template <
...@@ -37,8 +32,6 @@ class json_ref ...@@ -37,8 +32,6 @@ class json_ref
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 > enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args && ... args) json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...) : owned_value(std::forward<Args>(args)...)
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
// class should be movable only // class should be movable only
...@@ -50,27 +43,26 @@ class json_ref ...@@ -50,27 +43,26 @@ class json_ref
value_type moved_or_copied() const value_type moved_or_copied() const
{ {
if (is_rvalue) if (value_ref == nullptr)
{ {
return std::move(*value_ref); return std::move(owned_value);
} }
return *value_ref; return *value_ref;
} }
value_type const& operator*() const value_type const& operator*() const
{ {
return *static_cast<value_type const*>(value_ref); return value_ref ? *value_ref : owned_value;
} }
value_type const* operator->() const value_type const* operator->() const
{ {
return static_cast<value_type const*>(value_ref); return &**this;
} }
private: private:
mutable value_type owned_value = nullptr; mutable value_type owned_value = nullptr;
value_type* value_ref = nullptr; value_type const* value_ref = nullptr;
const bool is_rvalue = true;
}; };
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
...@@ -881,6 +881,7 @@ class serializer ...@@ -881,6 +881,7 @@ class serializer
} }
}; };
JSON_ASSERT(byte < utf8d.size());
const std::uint8_t type = utf8d[byte]; const std::uint8_t type = utf8d[byte];
codep = (state != UTF8_ACCEPT) codep = (state != UTF8_ACCEPT)
......
...@@ -168,6 +168,19 @@ template <class Key, class T, class IgnoredLess = std::less<Key>, ...@@ -168,6 +168,19 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
Container::push_back(value); Container::push_back(value);
return {--this->end(), true}; return {--this->end(), true};
} }
template<typename InputIt>
using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
std::input_iterator_tag>::value>::type;
template<typename InputIt, typename = require_input_iter<InputIt>>
void insert(InputIt first, InputIt last)
{
for (auto it = first; it != last; ++it)
{
insert(*it);
}
}
}; };
} // namespace nlohmann } // namespace nlohmann
...@@ -12605,19 +12605,14 @@ class json_ref ...@@ -12605,19 +12605,14 @@ class json_ref
json_ref(value_type&& value) json_ref(value_type&& value)
: owned_value(std::move(value)) : owned_value(std::move(value))
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
json_ref(const value_type& value) json_ref(const value_type& value)
: value_ref(const_cast<value_type*>(&value)) : value_ref(&value)
, is_rvalue(false)
{} {}
json_ref(std::initializer_list<json_ref> init) json_ref(std::initializer_list<json_ref> init)
: owned_value(init) : owned_value(init)
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
template < template <
...@@ -12625,8 +12620,6 @@ class json_ref ...@@ -12625,8 +12620,6 @@ class json_ref
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 > enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args && ... args) json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...) : owned_value(std::forward<Args>(args)...)
, value_ref(&owned_value)
, is_rvalue(true)
{} {}
// class should be movable only // class should be movable only
...@@ -12638,27 +12631,26 @@ class json_ref ...@@ -12638,27 +12631,26 @@ class json_ref
value_type moved_or_copied() const value_type moved_or_copied() const
{ {
if (is_rvalue) if (value_ref == nullptr)
{ {
return std::move(*value_ref); return std::move(owned_value);
} }
return *value_ref; return *value_ref;
} }
value_type const& operator*() const value_type const& operator*() const
{ {
return *static_cast<value_type const*>(value_ref); return value_ref ? *value_ref : owned_value;
} }
value_type const* operator->() const value_type const* operator->() const
{ {
return static_cast<value_type const*>(value_ref); return &**this;
} }
private: private:
mutable value_type owned_value = nullptr; mutable value_type owned_value = nullptr;
value_type* value_ref = nullptr; value_type const* value_ref = nullptr;
const bool is_rvalue = true;
}; };
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
...@@ -16392,6 +16384,7 @@ class serializer ...@@ -16392,6 +16384,7 @@ class serializer
} }
}; };
JSON_ASSERT(byte < utf8d.size());
const std::uint8_t type = utf8d[byte]; const std::uint8_t type = utf8d[byte];
codep = (state != UTF8_ACCEPT) codep = (state != UTF8_ACCEPT)
...@@ -16634,6 +16627,19 @@ template <class Key, class T, class IgnoredLess = std::less<Key>, ...@@ -16634,6 +16627,19 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
Container::push_back(value); Container::push_back(value);
return {--this->end(), true}; return {--this->end(), true};
} }
template<typename InputIt>
using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
std::input_iterator_tag>::value>::type;
template<typename InputIt, typename = require_input_iter<InputIt>>
void insert(InputIt first, InputIt last)
{
for (auto it = first; it != last; ++it)
{
insert(*it);
}
}
}; };
} // namespace nlohmann } // namespace nlohmann
......
...@@ -76,4 +76,18 @@ TEST_CASE("ordered_json") ...@@ -76,4 +76,18 @@ TEST_CASE("ordered_json")
CHECK(multi_ordered.dump() == "{\"z\":1,\"m\":2,\"y\":4}"); CHECK(multi_ordered.dump() == "{\"z\":1,\"m\":2,\"y\":4}");
CHECK(multi_ordered.erase("m") == 1); CHECK(multi_ordered.erase("m") == 1);
CHECK(multi_ordered.dump() == "{\"z\":1,\"y\":4}"); CHECK(multi_ordered.dump() == "{\"z\":1,\"y\":4}");
// Ranged insert test.
// It seems that values shouldn't be overwritten. Only new values are added
json j1 {{"c", 1}, {"b", 2}, {"a", 3}};
const json j2 {{"c", 77}, {"d", 42}, {"a", 4}};
j1.insert( j2.cbegin(), j2.cend() );
CHECK(j1.size() == 4);
CHECK(j1.dump() == "{\"a\":3,\"b\":2,\"c\":1,\"d\":42}");
ordered_json oj1 {{"c", 1}, {"b", 2}, {"a", 3}};
const ordered_json oj2 {{"c", 77}, {"d", 42}, {"a", 4}};
oj1.insert( oj2.cbegin(), oj2.cend() );
CHECK(oj1.size() == 4);
CHECK(oj1.dump() == "{\"c\":1,\"b\":2,\"a\":3,\"d\":42}");
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment