60c48755e Add missing macros in README and link to docs for more info (#4582) a6255cc41 add a ci step for Json_Diagnostic_Positions (#4579) f038ac4c9 Enable modernize-use-integer-sign-comparison check (#4581) 4f64d8d0b Modernize integer comparison (#4577) 2134cb947 Fix NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT with an empty JSON instance (#4508) 6057b31df Overwork astyle call (#4573) 0fa326a8e Use Clang image to run iwyu (#4574) 8314ac2f7 Use ubuntu-latest image to run Valgrind (#4575) 69c8758d5 fix the abi tests for diagnostics positions feature (#4572) 5ff90d9e0 fix diagnostic positions related compilation errors (#4570) 6be4e8560 Bump cppcheck from 1.4.6 to 1.4.7 in /cmake/requirements (#4567) 663058e7d Fix typo in nlohmann_define_derived_type.md (#4565) af4ad7915 Add ONLY_SERIALIZE for NLOHMANN_DEFINE_DERIVED_TYPE_* macros (#4562) 58f5f2596 json start/end position implementation (#4517) 733c59588 Bump actions/upload-artifact from 4.4.3 to 4.5.0 (#4557) 6cb099e30 Suppress modernize-use-integer-sign-comparison (#4558) 094bd2651 Set parents after insert call (#4537) 30cd44df9 Clean up CI (#4553) 5362012fd [StepSecurity] ci: Harden GitHub Actions (#4551) 861ec9c3c Fix token permissions warnings (#4550) f4910b20c Bump mkdocs-material from 9.5.48 to 9.5.49 in /docs/mkdocs (#4548) f2a4ac067 Add step to build the documentation (#4549) 8e1b80001 Move reuse dependency to requirements.txt file (#4547) 677e19e39 Clean up (#4546) 9e23b5d61 ⬆️ Bump watchdog from 2.1.7 to 6.0.0 in /tools/serve_header (#4542) eb142e33c ⬆️ Bump ossf/scorecard-action from 2.3.3 to 2.4.0 (#4545) 1c8e26dcb ⬆️ Bump lukka/get-cmake from 3.31.0 to 3.31.2 (#4544) b6b8378c8 ⬆️ Bump github/codeql-action from 2.27.9 to 3.27.9 (#4543) 58949c0ad ⬆️ Bump actions/github-script from 6.4.0 to 7.0.1 (#4540) 7cfbe321b ⬆️ Bump pyyaml from 6.0 to 6.0.2 in /tools/serve_header (#4541) 4003f8da0 [StepSecurity] Apply security best practices (#4539) 6a2ae22a5 📝 add OpenSSF Scorecard badge 620034ece ♻️ allow patch and diff to be used with arbitrary string types (#4536) e6cafa573 📝 fix CPM.cmake example e0a17f847 📝 add more package managers (#4533) 20ae363a7 Replace EOF with char_traits (#4532) 589641b17 Add support of multi-dim C-style array member of struct. (#4262) f385fe28f 💸 update sponsors 981a4c39f 📝 trigger documentation build dd81f878f 🚀 fix permissions 549c79ba7 Overwork documentation (#4516) 9f60e8555 Fix return value of get_ptr for unsigned integers (#4525) a006a7a48 Add more GCC warning flags (#4524) 1b9a9d1f2 Update licenses (#4521) 935c6eeb5 Optimize binary `get_number` implementation by reading multiple bytes at once (#4391) e41905fcb 💸 thanks @phrrngtn d333f0895 CMake: generate a pkg-config file that follow pkg-config conventions (#4456) 3d6c664de Allow comparing default initialized iterators (#4512) ee32bfc1c Make SAX output locale-independent (#4505) a97041a98 Skip enum tests when JSON_DisableEnumSerialization=ON (#4504) f9f8c0779 fix: integer parsed as float when EINTR set in errno (#4506) 378e09179 Fix weak-vtables warning (#4500) 1f218e107 Possible fix for #4485 (#4487) 64f68dc56 Suppress warnings in NLOHMANN_JSON_SERIALIZE_ENUM (#4497) 1c5923e5d Add comment for #4494 (#4496) e3ac5a64b Add test for libstdc++ (#4495) e509007df Remove alwayslink=True Bazel flag (#4396) 060414037 feat: Rebase `feature/optional` to `develop` (#4036) fd20975a9 Add CPack support (#4459) fde9a86c5 Make iterator_proxy_value a forward_iterator (#4371) (#4372) 1825117e6 Another desperate try to fix the CI (#4489) 4a602df34 Add lgtm explanation (#4362) 18ff442e6 Allow overriding the CMake target name (#4483) aff5a31d3 Add NLOHMANN_DEFINE_DERIVED_TYPE_* macros (#4033) ac8b22180 Update CONTRIBUTING.md (#4486) 632583977 Update is_structured.md (#4472) b36f4c477 Update natvis to reflect 3.11.3 and the current structure of basic_json (#4451) 960b763ec Docs: fix typos of 'whether' in `operator_{gt,le,lt}.md` (#4412) 8c391e04f Docs: Fix wrong code usage in the Value access section of `json_pointer.md` (#4255) 01da82eae Fix gdb pretty printer (#4343) 97f0bdaf9 chore: fix some typos in comments (#4345) c883fb0f1 Fix for incorrect function name in documentation example (#4342) 377c767aa Updated exception handling to catch const reference in out_of_range (#4331) 16b3d841d Fixed an error in the `Custom data source` example. (#4335) 199dea11b #4307 Updated docx to 3.10.5 from 3.10.4 (#4310) 0457de21c Align astyle flags in Makefile with CI (#4277) c35d260c2 Suppress Clang-Tidy warnings (#4276) 6a064e026 Remove broken link from CONTRIBUTING.md (#4274) 7efe87549 Update json_has_static_rtti.md (#4269) a259ecc51 Fix `to_json` for enums when the enum has an unsigned underlying type. (#4237) 3780b41dd 💰 update sponsors git-subtree-dir: external/json git-subtree-split: 60c48755e3f717eace7830d7bbc0d8f1a5e0cc8a
360 lines
11 KiB
C++
360 lines
11 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++
|
|
// | | |__ | | | | | | version 3.11.3
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013 - 2024 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
#include <functional> // equal_to, less
|
|
#include <initializer_list> // initializer_list
|
|
#include <iterator> // input_iterator_tag, iterator_traits
|
|
#include <memory> // allocator
|
|
#include <stdexcept> // for out_of_range
|
|
#include <type_traits> // enable_if, is_convertible
|
|
#include <utility> // pair
|
|
#include <vector> // vector
|
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
|
|
/// ordered_map: a minimal map-like container that preserves insertion order
|
|
/// for use within nlohmann::basic_json<ordered_map>
|
|
template <class Key, class T, class IgnoredLess = std::less<Key>,
|
|
class Allocator = std::allocator<std::pair<const Key, T>>>
|
|
struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
|
|
{
|
|
using key_type = Key;
|
|
using mapped_type = T;
|
|
using Container = std::vector<std::pair<const Key, T>, Allocator>;
|
|
using iterator = typename Container::iterator;
|
|
using const_iterator = typename Container::const_iterator;
|
|
using size_type = typename Container::size_type;
|
|
using value_type = typename Container::value_type;
|
|
#ifdef JSON_HAS_CPP_14
|
|
using key_compare = std::equal_to<>;
|
|
#else
|
|
using key_compare = std::equal_to<Key>;
|
|
#endif
|
|
|
|
// Explicit constructors instead of `using Container::Container`
|
|
// otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
|
|
ordered_map() noexcept(noexcept(Container())) : Container{} {}
|
|
explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
|
|
template <class It>
|
|
ordered_map(It first, It last, const Allocator& alloc = Allocator())
|
|
: Container{first, last, alloc} {}
|
|
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
|
|
: Container{init, alloc} {}
|
|
|
|
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return {it, false};
|
|
}
|
|
}
|
|
Container::emplace_back(key, std::forward<T>(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)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return {it, false};
|
|
}
|
|
}
|
|
Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
|
|
return {std::prev(this->end()), true};
|
|
}
|
|
|
|
T& operator[](const key_type& key)
|
|
{
|
|
return emplace(key, T{}).first->second;
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
T & operator[](KeyType && key)
|
|
{
|
|
return emplace(std::forward<KeyType>(key), T{}).first->second;
|
|
}
|
|
|
|
const T& operator[](const key_type& key) const
|
|
{
|
|
return at(key);
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
const T & operator[](KeyType && key) const
|
|
{
|
|
return at(std::forward<KeyType>(key));
|
|
}
|
|
|
|
T& at(const key_type& key)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it->second;
|
|
}
|
|
}
|
|
|
|
JSON_THROW(std::out_of_range("key not found"));
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it->second;
|
|
}
|
|
}
|
|
|
|
JSON_THROW(std::out_of_range("key not found"));
|
|
}
|
|
|
|
const T& at(const key_type& key) const
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it->second;
|
|
}
|
|
}
|
|
|
|
JSON_THROW(std::out_of_range("key not found"));
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it->second;
|
|
}
|
|
}
|
|
|
|
JSON_THROW(std::out_of_range("key not found"));
|
|
}
|
|
|
|
size_type erase(const key_type& key)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
// Since we cannot move const Keys, re-construct them in place
|
|
for (auto next = it; ++next != this->end(); ++it)
|
|
{
|
|
it->~value_type(); // Destroy but keep allocation
|
|
new (&*it) value_type{std::move(*next)};
|
|
}
|
|
Container::pop_back();
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
// Since we cannot move const Keys, re-construct them in place
|
|
for (auto next = it; ++next != this->end(); ++it)
|
|
{
|
|
it->~value_type(); // Destroy but keep allocation
|
|
new (&*it) value_type{std::move(*next)};
|
|
}
|
|
Container::pop_back();
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
iterator erase(iterator pos)
|
|
{
|
|
return erase(pos, std::next(pos));
|
|
}
|
|
|
|
iterator erase(iterator first, iterator last)
|
|
{
|
|
if (first == last)
|
|
{
|
|
return first;
|
|
}
|
|
|
|
const auto elements_affected = std::distance(first, last);
|
|
const auto offset = std::distance(Container::begin(), first);
|
|
|
|
// This is the start situation. We need to delete elements_affected
|
|
// elements (3 in this example: e, f, g), and need to return an
|
|
// iterator past the last deleted element (h in this example).
|
|
// Note that offset is the distance from the start of the vector
|
|
// to first. We will need this later.
|
|
|
|
// [ a, b, c, d, e, f, g, h, i, j ]
|
|
// ^ ^
|
|
// first last
|
|
|
|
// Since we cannot move const Keys, we re-construct them in place.
|
|
// We start at first and re-construct (viz. copy) the elements from
|
|
// the back of the vector. Example for first iteration:
|
|
|
|
// ,--------.
|
|
// v | destroy e and re-construct with h
|
|
// [ a, b, c, d, e, f, g, h, i, j ]
|
|
// ^ ^
|
|
// it it + elements_affected
|
|
|
|
for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
|
|
{
|
|
it->~value_type(); // destroy but keep allocation
|
|
new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
|
|
}
|
|
|
|
// [ a, b, c, d, h, i, j, h, i, j ]
|
|
// ^ ^
|
|
// first last
|
|
|
|
// remove the unneeded elements at the end of the vector
|
|
Container::resize(this->size() - static_cast<size_type>(elements_affected));
|
|
|
|
// [ a, b, c, d, h, i, j ]
|
|
// ^ ^
|
|
// first last
|
|
|
|
// first is now pointing past the last deleted element, but we cannot
|
|
// use this iterator, because it may have been invalidated by the
|
|
// resize call. Instead, we can return begin() + offset.
|
|
return Container::begin() + offset;
|
|
}
|
|
|
|
size_type count(const key_type& key) const
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
iterator find(const key_type& key)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it;
|
|
}
|
|
}
|
|
return Container::end();
|
|
}
|
|
|
|
template<class KeyType, detail::enable_if_t<
|
|
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
|
iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it;
|
|
}
|
|
}
|
|
return Container::end();
|
|
}
|
|
|
|
const_iterator find(const key_type& key) const
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, key))
|
|
{
|
|
return it;
|
|
}
|
|
}
|
|
return Container::end();
|
|
}
|
|
|
|
std::pair<iterator, bool> insert( value_type&& value )
|
|
{
|
|
return emplace(value.first, std::move(value.second));
|
|
}
|
|
|
|
std::pair<iterator, bool> insert( const value_type& value )
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (m_compare(it->first, value.first))
|
|
{
|
|
return {it, false};
|
|
}
|
|
}
|
|
Container::push_back(value);
|
|
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);
|
|
}
|
|
}
|
|
|
|
private:
|
|
JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
|
|
};
|
|
|
|
NLOHMANN_JSON_NAMESPACE_END
|