2018-01-28 14:36:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-13 14:31:22 +00:00
|
|
|
#include <algorithm> // all_of
|
2019-06-30 08:03:08 +00:00
|
|
|
#include <cctype> // isdigit
|
2020-06-20 13:36:28 +00:00
|
|
|
#include <limits> // max
|
2018-01-28 14:36:56 +00:00
|
|
|
#include <numeric> // accumulate
|
|
|
|
#include <string> // string
|
2019-03-15 13:55:13 +00:00
|
|
|
#include <utility> // move
|
2018-01-28 14:36:56 +00:00
|
|
|
#include <vector> // vector
|
|
|
|
|
2018-01-29 10:21:11 +00:00
|
|
|
#include <nlohmann/detail/exceptions.hpp>
|
2019-03-17 11:01:49 +00:00
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
2021-01-16 14:33:05 +00:00
|
|
|
#include <nlohmann/detail/string_escape.hpp>
|
2018-01-29 10:21:11 +00:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2018-01-28 14:36:56 +00:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
class json_pointer
|
|
|
|
{
|
|
|
|
// allow basic_json to access private members
|
|
|
|
NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
|
|
|
friend class basic_json;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
@brief create JSON pointer
|
|
|
|
|
|
|
|
Create a JSON pointer according to the syntax described in
|
|
|
|
[Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3).
|
|
|
|
|
|
|
|
@param[in] s string representing the JSON pointer; if omitted, the empty
|
|
|
|
string is assumed which references the whole JSON value
|
|
|
|
|
|
|
|
@throw parse_error.107 if the given JSON pointer @a s is nonempty and does
|
|
|
|
not begin with a slash (`/`); see example below
|
|
|
|
|
|
|
|
@throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is
|
|
|
|
not followed by `0` (representing `~`) or `1` (representing `/`); see
|
|
|
|
example below
|
|
|
|
|
|
|
|
@liveexample{The example shows the construction several valid JSON pointers
|
|
|
|
as well as the exceptional behavior.,json_pointer}
|
|
|
|
|
|
|
|
@since version 2.0.0
|
|
|
|
*/
|
|
|
|
explicit json_pointer(const std::string& s = "")
|
|
|
|
: reference_tokens(split(s))
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief return a string representation of the JSON pointer
|
|
|
|
|
|
|
|
@invariant For each JSON pointer `ptr`, it holds:
|
|
|
|
@code {.cpp}
|
|
|
|
ptr == json_pointer(ptr.to_string());
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
@return a string representation of the JSON pointer
|
|
|
|
|
2019-03-19 09:06:35 +00:00
|
|
|
@liveexample{The example shows the result of `to_string`.,json_pointer__to_string}
|
2018-01-28 14:36:56 +00:00
|
|
|
|
|
|
|
@since version 2.0.0
|
|
|
|
*/
|
2018-10-28 08:15:41 +00:00
|
|
|
std::string to_string() const
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
|
|
|
|
std::string{},
|
|
|
|
[](const std::string & a, const std::string & b)
|
|
|
|
{
|
2021-01-16 14:33:05 +00:00
|
|
|
return a + "/" + detail::escape(b);
|
2018-01-28 14:36:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @copydoc to_string()
|
|
|
|
operator std::string() const
|
|
|
|
{
|
|
|
|
return to_string();
|
|
|
|
}
|
|
|
|
|
2019-01-31 19:15:36 +00:00
|
|
|
/*!
|
|
|
|
@brief append another JSON pointer at the end of this JSON pointer
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@param[in] ptr JSON pointer to append
|
|
|
|
@return JSON pointer with @a ptr appended
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
|
|
|
|
|
|
|
|
@complexity Linear in the length of @a ptr.
|
|
|
|
|
|
|
|
@sa @ref operator/=(std::string) to append a reference token
|
|
|
|
@sa @ref operator/=(std::size_t) to append an array index
|
|
|
|
@sa @ref operator/(const json_pointer&, const json_pointer&) for a binary operator
|
|
|
|
|
|
|
|
@since version 3.6.0
|
2019-01-31 19:15:36 +00:00
|
|
|
*/
|
|
|
|
json_pointer& operator/=(const json_pointer& ptr)
|
|
|
|
{
|
2019-03-17 21:25:18 +00:00
|
|
|
reference_tokens.insert(reference_tokens.end(),
|
|
|
|
ptr.reference_tokens.begin(),
|
|
|
|
ptr.reference_tokens.end());
|
2019-01-31 19:15:36 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:06:35 +00:00
|
|
|
/*!
|
|
|
|
@brief append an unescaped reference token at the end of this JSON pointer
|
|
|
|
|
|
|
|
@param[in] token reference token to append
|
|
|
|
@return JSON pointer with @a token appended without escaping @a token
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
|
|
|
|
|
|
|
|
@complexity Amortized constant.
|
|
|
|
|
|
|
|
@sa @ref operator/=(const json_pointer&) to append a JSON pointer
|
|
|
|
@sa @ref operator/=(std::size_t) to append an array index
|
|
|
|
@sa @ref operator/(const json_pointer&, std::size_t) for a binary operator
|
|
|
|
|
|
|
|
@since version 3.6.0
|
|
|
|
*/
|
2019-01-31 19:15:36 +00:00
|
|
|
json_pointer& operator/=(std::string token)
|
|
|
|
{
|
|
|
|
push_back(std::move(token));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:06:35 +00:00
|
|
|
/*!
|
|
|
|
@brief append an array index at the end of this JSON pointer
|
|
|
|
|
2020-03-19 10:52:40 +00:00
|
|
|
@param[in] array_idx array index to append
|
|
|
|
@return JSON pointer with @a array_idx appended
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
|
|
|
|
|
|
|
|
@complexity Amortized constant.
|
|
|
|
|
|
|
|
@sa @ref operator/=(const json_pointer&) to append a JSON pointer
|
|
|
|
@sa @ref operator/=(std::string) to append a reference token
|
|
|
|
@sa @ref operator/(const json_pointer&, std::string) for a binary operator
|
|
|
|
|
|
|
|
@since version 3.6.0
|
|
|
|
*/
|
2020-03-19 10:52:40 +00:00
|
|
|
json_pointer& operator/=(std::size_t array_idx)
|
2019-01-31 19:15:36 +00:00
|
|
|
{
|
2020-03-19 10:52:40 +00:00
|
|
|
return *this /= std::to_string(array_idx);
|
2019-01-31 19:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@param[in] lhs JSON pointer
|
|
|
|
@param[in] rhs JSON pointer
|
|
|
|
@return a new JSON pointer with @a rhs appended to @a lhs
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
|
|
|
|
|
|
|
|
@complexity Linear in the length of @a lhs and @a rhs.
|
|
|
|
|
|
|
|
@sa @ref operator/=(const json_pointer&) to append a JSON pointer
|
|
|
|
|
|
|
|
@since version 3.6.0
|
2019-01-31 19:15:36 +00:00
|
|
|
*/
|
2019-03-19 09:06:35 +00:00
|
|
|
friend json_pointer operator/(const json_pointer& lhs,
|
|
|
|
const json_pointer& rhs)
|
2019-01-31 19:15:36 +00:00
|
|
|
{
|
2019-03-19 09:06:35 +00:00
|
|
|
return json_pointer(lhs) /= rhs;
|
2019-01-31 19:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@param[in] ptr JSON pointer
|
|
|
|
@param[in] token reference token
|
|
|
|
@return a new JSON pointer with unescaped @a token appended to @a ptr
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
|
|
|
|
|
|
|
|
@complexity Linear in the length of @a ptr.
|
|
|
|
|
|
|
|
@sa @ref operator/=(std::string) to append a reference token
|
|
|
|
|
|
|
|
@since version 3.6.0
|
2019-01-31 19:15:36 +00:00
|
|
|
*/
|
|
|
|
friend json_pointer operator/(const json_pointer& ptr, std::string token)
|
|
|
|
{
|
|
|
|
return json_pointer(ptr) /= std::move(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@param[in] ptr JSON pointer
|
2020-03-19 10:52:40 +00:00
|
|
|
@param[in] array_idx array index
|
|
|
|
@return a new JSON pointer with @a array_idx appended to @a ptr
|
2019-03-19 09:06:35 +00:00
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
|
|
|
|
|
|
|
|
@complexity Linear in the length of @a ptr.
|
|
|
|
|
|
|
|
@sa @ref operator/=(std::size_t) to append an array index
|
|
|
|
|
|
|
|
@since version 3.6.0
|
2019-01-31 19:15:36 +00:00
|
|
|
*/
|
2020-03-19 10:52:40 +00:00
|
|
|
friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx)
|
2019-01-31 19:15:36 +00:00
|
|
|
{
|
2020-03-19 10:52:40 +00:00
|
|
|
return json_pointer(ptr) /= array_idx;
|
2019-01-31 19:15:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:10:45 +00:00
|
|
|
/*!
|
2019-03-17 21:25:18 +00:00
|
|
|
@brief returns the parent of this JSON pointer
|
|
|
|
|
|
|
|
@return parent of this JSON pointer; in case this JSON pointer is the root,
|
|
|
|
the root itself is returned
|
|
|
|
|
2019-03-19 14:40:23 +00:00
|
|
|
@complexity Linear in the length of the JSON pointer.
|
2019-03-17 21:25:18 +00:00
|
|
|
|
|
|
|
@liveexample{The example shows the result of `parent_pointer` for different
|
|
|
|
JSON Pointers.,json_pointer__parent_pointer}
|
|
|
|
|
|
|
|
@since version 3.6.0
|
2019-02-25 09:10:45 +00:00
|
|
|
*/
|
|
|
|
json_pointer parent_pointer() const
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_pointer res = *this;
|
|
|
|
res.pop_back();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:36:56 +00:00
|
|
|
/*!
|
2019-03-19 14:40:23 +00:00
|
|
|
@brief remove last reference token
|
|
|
|
|
|
|
|
@pre not `empty()`
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `pop_back`.,json_pointer__pop_back}
|
|
|
|
|
|
|
|
@complexity Constant.
|
|
|
|
|
2018-01-28 14:36:56 +00:00
|
|
|
@throw out_of_range.405 if JSON pointer has no parent
|
2019-03-19 14:40:23 +00:00
|
|
|
|
|
|
|
@since version 3.6.0
|
2018-01-28 14:36:56 +00:00
|
|
|
*/
|
2019-03-19 14:40:23 +00:00
|
|
|
void pop_back()
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(empty()))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference_tokens.pop_back();
|
2019-03-19 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief return last reference token
|
|
|
|
|
|
|
|
@pre not `empty()`
|
|
|
|
@return last reference token
|
|
|
|
|
|
|
|
@liveexample{The example shows the usage of `back`.,json_pointer__back}
|
|
|
|
|
|
|
|
@complexity Constant.
|
|
|
|
|
|
|
|
@throw out_of_range.405 if JSON pointer has no parent
|
|
|
|
|
|
|
|
@since version 3.6.0
|
|
|
|
*/
|
2019-09-30 22:57:27 +00:00
|
|
|
const std::string& back() const
|
2019-03-19 14:40:23 +00:00
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(empty()))
|
2019-03-19 14:40:23 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
|
2019-03-19 14:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reference_tokens.back();
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:39:06 +00:00
|
|
|
/*!
|
2019-01-31 19:15:36 +00:00
|
|
|
@brief append an unescaped token at the end of the reference pointer
|
2019-03-17 21:25:18 +00:00
|
|
|
|
|
|
|
@param[in] token token to add
|
|
|
|
|
|
|
|
@complexity Amortized constant.
|
|
|
|
|
|
|
|
@liveexample{The example shows the result of `push_back` for different
|
|
|
|
JSON Pointers.,json_pointer__push_back}
|
|
|
|
|
2019-03-19 14:40:23 +00:00
|
|
|
@since version 3.6.0
|
2019-01-24 15:46:51 +00:00
|
|
|
*/
|
2019-01-31 19:15:36 +00:00
|
|
|
void push_back(const std::string& token)
|
2019-01-24 15:46:51 +00:00
|
|
|
{
|
2019-01-31 19:15:36 +00:00
|
|
|
reference_tokens.push_back(token);
|
2019-01-24 15:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 19:15:36 +00:00
|
|
|
/// @copydoc push_back(const std::string&)
|
|
|
|
void push_back(std::string&& token)
|
2019-01-24 15:46:51 +00:00
|
|
|
{
|
2019-01-31 19:15:36 +00:00
|
|
|
reference_tokens.push_back(std::move(token));
|
2019-01-24 15:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 21:25:18 +00:00
|
|
|
/*!
|
|
|
|
@brief return whether pointer points to the root document
|
|
|
|
|
|
|
|
@return true iff the JSON pointer points to the root document
|
|
|
|
|
|
|
|
@complexity Constant.
|
|
|
|
|
|
|
|
@exceptionsafety No-throw guarantee: this function never throws exceptions.
|
|
|
|
|
|
|
|
@liveexample{The example shows the result of `empty` for different JSON
|
|
|
|
Pointers.,json_pointer__empty}
|
|
|
|
|
|
|
|
@since version 3.6.0
|
|
|
|
*/
|
2019-02-25 08:57:12 +00:00
|
|
|
bool empty() const noexcept
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
return reference_tokens.empty();
|
|
|
|
}
|
|
|
|
|
2019-02-25 08:57:12 +00:00
|
|
|
private:
|
2019-03-17 21:25:18 +00:00
|
|
|
/*!
|
|
|
|
@param[in] s reference token to be converted into an array index
|
|
|
|
|
|
|
|
@return integer representation of @a s
|
|
|
|
|
2020-06-20 13:25:54 +00:00
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index begins not with a digit
|
2019-03-17 21:25:18 +00:00
|
|
|
@throw out_of_range.404 if string @a s could not be converted to an integer
|
2020-06-20 13:36:28 +00:00
|
|
|
@throw out_of_range.410 if an array index exceeds size_type
|
2019-03-17 21:25:18 +00:00
|
|
|
*/
|
2020-06-20 13:36:28 +00:00
|
|
|
static typename BasicJsonType::size_type array_index(const std::string& s)
|
2019-03-17 21:25:18 +00:00
|
|
|
{
|
2020-06-20 13:36:28 +00:00
|
|
|
using size_type = typename BasicJsonType::size_type;
|
|
|
|
|
2020-03-17 12:49:28 +00:00
|
|
|
// error condition (cf. RFC 6901, Sect. 4)
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
|
2020-03-17 12:49:28 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType()));
|
2020-03-17 12:49:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 02:00:21 +00:00
|
|
|
// error condition (cf. RFC 6901, Sect. 4)
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
|
2020-03-17 12:49:28 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType()));
|
2020-03-17 12:49:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 21:25:18 +00:00
|
|
|
std::size_t processed_chars = 0;
|
2020-06-20 13:36:28 +00:00
|
|
|
unsigned long long res = 0;
|
2020-03-23 09:24:47 +00:00
|
|
|
JSON_TRY
|
|
|
|
{
|
2020-06-20 13:36:28 +00:00
|
|
|
res = std::stoull(s, &processed_chars);
|
2020-03-23 09:24:47 +00:00
|
|
|
}
|
2020-03-25 07:56:45 +00:00
|
|
|
JSON_CATCH(std::out_of_range&)
|
2020-03-23 09:24:47 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
|
2020-03-23 09:24:47 +00:00
|
|
|
}
|
2019-03-17 21:25:18 +00:00
|
|
|
|
|
|
|
// check if the string was completely read
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size()))
|
2019-03-17 21:25:18 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
|
2019-03-17 21:25:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:36:28 +00:00
|
|
|
// only triggered on special platforms (like 32bit), see also
|
|
|
|
// https://github.com/nlohmann/json/pull/2203
|
|
|
|
if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)()))
|
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE
|
2020-06-20 13:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<size_type>(res);
|
2019-03-17 21:25:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 11:30:06 +00:00
|
|
|
JSON_PRIVATE_UNLESS_TESTED:
|
2018-01-28 14:36:56 +00:00
|
|
|
json_pointer top() const
|
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(empty()))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_pointer result = *this;
|
|
|
|
result.reference_tokens = {reference_tokens[0]};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-12 11:30:06 +00:00
|
|
|
private:
|
2018-01-28 14:36:56 +00:00
|
|
|
/*!
|
|
|
|
@brief create and return a reference to the pointed to value
|
|
|
|
|
|
|
|
@complexity Linear in the number of reference tokens.
|
|
|
|
|
|
|
|
@throw parse_error.109 if array index is not a number
|
|
|
|
@throw type_error.313 if value cannot be unflattened
|
|
|
|
*/
|
|
|
|
BasicJsonType& get_and_create(BasicJsonType& j) const
|
|
|
|
{
|
|
|
|
auto result = &j;
|
|
|
|
|
|
|
|
// in case no reference tokens exist, return a reference to the JSON value
|
|
|
|
// j which will be overwritten by a primitive value
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (result->type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::null:
|
|
|
|
{
|
|
|
|
if (reference_token == "0")
|
|
|
|
{
|
|
|
|
// start a new array if reference token is 0
|
|
|
|
result = &result->operator[](0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// start a new object otherwise
|
|
|
|
result = &result->operator[](reference_token);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
// create an entry in the object
|
|
|
|
result = &result->operator[](reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
|
|
|
// create an entry in the array
|
2020-06-20 13:36:28 +00:00
|
|
|
result = &result->operator[](array_index(reference_token));
|
2018-01-28 14:36:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following code is only reached if there exists a reference
|
|
|
|
token _and_ the current value is primitive. In this case, we have
|
|
|
|
an error situation, because primitive values may only occur as
|
|
|
|
single value; that is, with an empty list of reference tokens.
|
|
|
|
*/
|
|
|
|
default:
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief return a reference to the pointed to value
|
|
|
|
|
|
|
|
@note This version does not throw if a value is not present, but tries to
|
|
|
|
create nested values instead. For instance, calling this function
|
|
|
|
with pointer `"/this/that"` on a null value is equivalent to calling
|
|
|
|
`operator[]("this").operator[]("that")` on that value, effectively
|
|
|
|
changing the null value to an object.
|
|
|
|
|
|
|
|
@param[in] ptr a JSON value
|
|
|
|
|
|
|
|
@return reference to the JSON value pointed to by the JSON pointer
|
|
|
|
|
|
|
|
@complexity Linear in the length of the JSON pointer.
|
|
|
|
|
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index was not a number
|
|
|
|
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
|
|
*/
|
|
|
|
BasicJsonType& get_unchecked(BasicJsonType* ptr) const
|
|
|
|
{
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
|
|
|
// convert null values to arrays or objects before continuing
|
2019-06-30 08:03:08 +00:00
|
|
|
if (ptr->is_null())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
// check if reference token is a number
|
|
|
|
const bool nums =
|
|
|
|
std::all_of(reference_token.begin(), reference_token.end(),
|
2019-06-30 08:03:08 +00:00
|
|
|
[](const unsigned char x)
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
return std::isdigit(x);
|
2018-01-28 14:36:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// change value to array for numbers or "-" or to object otherwise
|
2020-06-03 12:20:36 +00:00
|
|
|
*ptr = (nums || reference_token == "-")
|
2018-01-28 14:36:56 +00:00
|
|
|
? detail::value_t::array
|
|
|
|
: detail::value_t::object;
|
|
|
|
}
|
|
|
|
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (ptr->type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
// use unchecked object access
|
|
|
|
ptr = &ptr->operator[](reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
|
|
|
if (reference_token == "-")
|
|
|
|
{
|
|
|
|
// explicitly treat "-" as index beyond the end
|
|
|
|
ptr = &ptr->operator[](ptr->m_value.array->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert array index to number; unchecked access
|
2020-06-20 13:36:28 +00:00
|
|
|
ptr = &ptr->operator[](array_index(reference_token));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index was not a number
|
|
|
|
@throw out_of_range.402 if the array index '-' is used
|
|
|
|
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
|
|
*/
|
|
|
|
BasicJsonType& get_checked(BasicJsonType* ptr) const
|
|
|
|
{
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (ptr->type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
// note: at performs range check
|
|
|
|
ptr = &ptr->at(reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
// "-" always fails the range check
|
|
|
|
JSON_THROW(detail::out_of_range::create(402,
|
|
|
|
"array index '-' (" + std::to_string(ptr->m_value.array->size()) +
|
2021-01-25 12:47:50 +00:00
|
|
|
") is out of range", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// note: at performs range check
|
2020-06-20 13:36:28 +00:00
|
|
|
ptr = &ptr->at(array_index(reference_token));
|
2018-01-28 14:36:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief return a const reference to the pointed to value
|
|
|
|
|
|
|
|
@param[in] ptr a JSON value
|
|
|
|
|
|
|
|
@return const reference to the JSON value pointed to by the JSON
|
|
|
|
pointer
|
|
|
|
|
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index was not a number
|
|
|
|
@throw out_of_range.402 if the array index '-' is used
|
|
|
|
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
|
|
*/
|
|
|
|
const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
|
|
|
|
{
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (ptr->type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
// use unchecked object access
|
|
|
|
ptr = &ptr->operator[](reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
// "-" cannot be used for const access
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use unchecked array access
|
2020-06-20 13:36:28 +00:00
|
|
|
ptr = &ptr->operator[](array_index(reference_token));
|
2018-01-28 14:36:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index was not a number
|
|
|
|
@throw out_of_range.402 if the array index '-' is used
|
|
|
|
@throw out_of_range.404 if the JSON pointer can not be resolved
|
|
|
|
*/
|
|
|
|
const BasicJsonType& get_checked(const BasicJsonType* ptr) const
|
|
|
|
{
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (ptr->type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
// note: at performs range check
|
|
|
|
ptr = &ptr->at(reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
// "-" always fails the range check
|
|
|
|
JSON_THROW(detail::out_of_range::create(402,
|
|
|
|
"array index '-' (" + std::to_string(ptr->m_value.array->size()) +
|
2021-01-25 12:47:50 +00:00
|
|
|
") is out of range", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// note: at performs range check
|
2020-06-20 13:36:28 +00:00
|
|
|
ptr = &ptr->at(array_index(reference_token));
|
2018-01-28 14:36:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2019-06-30 08:03:08 +00:00
|
|
|
/*!
|
|
|
|
@throw parse_error.106 if an array index begins with '0'
|
|
|
|
@throw parse_error.109 if an array index was not a number
|
|
|
|
*/
|
|
|
|
bool contains(const BasicJsonType* ptr) const
|
|
|
|
{
|
|
|
|
for (const auto& reference_token : reference_tokens)
|
|
|
|
{
|
|
|
|
switch (ptr->type())
|
|
|
|
{
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
if (!ptr->contains(reference_token))
|
2019-06-30 08:03:08 +00:00
|
|
|
{
|
|
|
|
// we did not find the key in the object
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = &ptr->operator[](reference_token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
2019-07-14 19:19:55 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
|
2019-06-30 08:03:08 +00:00
|
|
|
{
|
|
|
|
// "-" always fails the range check
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
|
2020-04-13 11:41:13 +00:00
|
|
|
{
|
|
|
|
// invalid char
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
|
2020-04-13 11:41:13 +00:00
|
|
|
{
|
|
|
|
// first char should be between '1' and '9'
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (std::size_t i = 1; i < reference_token.size(); i++)
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
|
2020-04-13 11:41:13 +00:00
|
|
|
{
|
|
|
|
// other char should be between '0' and '9'
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 08:03:08 +00:00
|
|
|
|
2020-06-20 13:36:28 +00:00
|
|
|
const auto idx = array_index(reference_token);
|
2020-03-23 09:24:47 +00:00
|
|
|
if (idx >= ptr->size())
|
2019-06-30 08:03:08 +00:00
|
|
|
{
|
2020-03-23 09:24:47 +00:00
|
|
|
// index out of range
|
|
|
|
return false;
|
2019-06-30 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 09:24:47 +00:00
|
|
|
ptr = &ptr->operator[](idx);
|
2019-06-30 08:03:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// we do not expect primitive values if there is still a
|
|
|
|
// reference token to process
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no reference token left means we found a primitive value
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:36:56 +00:00
|
|
|
/*!
|
|
|
|
@brief split the string input to reference tokens
|
|
|
|
|
|
|
|
@note This function is only called by the json_pointer constructor.
|
|
|
|
All exceptions below are documented there.
|
|
|
|
|
|
|
|
@throw parse_error.107 if the pointer is not empty or begins with '/'
|
|
|
|
@throw parse_error.108 if character '~' is not followed by '0' or '1'
|
|
|
|
*/
|
|
|
|
static std::vector<std::string> split(const std::string& reference_string)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
// special case: empty reference string -> no reference tokens
|
|
|
|
if (reference_string.empty())
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if nonempty reference string begins with slash
|
2019-07-01 20:37:30 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType()));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract the reference tokens:
|
|
|
|
// - slash: position of the last read slash (or end of string)
|
|
|
|
// - start: position after the previous slash
|
|
|
|
for (
|
|
|
|
// search for the first slash after the first character
|
|
|
|
std::size_t slash = reference_string.find_first_of('/', 1),
|
|
|
|
// set the beginning of the first reference token
|
|
|
|
start = 1;
|
2018-10-06 11:49:02 +00:00
|
|
|
// we can stop if start == 0 (if slash == std::string::npos)
|
2018-01-28 14:36:56 +00:00
|
|
|
start != 0;
|
|
|
|
// set the beginning of the next reference token
|
|
|
|
// (will eventually be 0 if slash == std::string::npos)
|
2018-10-06 11:49:02 +00:00
|
|
|
start = (slash == std::string::npos) ? 0 : slash + 1,
|
2018-01-28 14:36:56 +00:00
|
|
|
// find next slash
|
|
|
|
slash = reference_string.find_first_of('/', start))
|
|
|
|
{
|
|
|
|
// use the text between the beginning of the reference token
|
|
|
|
// (start) and the last slash (slash).
|
|
|
|
auto reference_token = reference_string.substr(start, slash - start);
|
|
|
|
|
|
|
|
// check reference tokens are properly escaped
|
|
|
|
for (std::size_t pos = reference_token.find_first_of('~');
|
|
|
|
pos != std::string::npos;
|
|
|
|
pos = reference_token.find_first_of('~', pos + 1))
|
|
|
|
{
|
2020-07-06 10:22:31 +00:00
|
|
|
JSON_ASSERT(reference_token[pos] == '~');
|
2018-01-28 14:36:56 +00:00
|
|
|
|
|
|
|
// ~ must be followed by 0 or 1
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
|
|
|
|
(reference_token[pos + 1] != '0' &&
|
2019-07-01 20:37:30 +00:00
|
|
|
reference_token[pos + 1] != '1')))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType()));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, store the reference token
|
2021-01-16 14:33:05 +00:00
|
|
|
detail::unescape(reference_token);
|
2018-01-28 14:36:56 +00:00
|
|
|
result.push_back(reference_token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-12 11:30:06 +00:00
|
|
|
private:
|
2018-01-28 14:36:56 +00:00
|
|
|
/*!
|
|
|
|
@param[in] reference_string the reference string to the current value
|
|
|
|
@param[in] value the value to consider
|
|
|
|
@param[in,out] result the result object to insert values to
|
|
|
|
|
|
|
|
@note Empty objects or arrays are flattened to `null`.
|
|
|
|
*/
|
|
|
|
static void flatten(const std::string& reference_string,
|
|
|
|
const BasicJsonType& value,
|
|
|
|
BasicJsonType& result)
|
|
|
|
{
|
2019-06-30 08:03:08 +00:00
|
|
|
switch (value.type())
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
|
|
|
case detail::value_t::array:
|
|
|
|
{
|
|
|
|
if (value.m_value.array->empty())
|
|
|
|
{
|
|
|
|
// flatten empty array as null
|
|
|
|
result[reference_string] = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// iterate array and use index as reference string
|
|
|
|
for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
|
|
|
|
{
|
|
|
|
flatten(reference_string + "/" + std::to_string(i),
|
|
|
|
value.m_value.array->operator[](i), result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case detail::value_t::object:
|
|
|
|
{
|
|
|
|
if (value.m_value.object->empty())
|
|
|
|
{
|
|
|
|
// flatten empty object as null
|
|
|
|
result[reference_string] = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// iterate object and use keys as reference string
|
|
|
|
for (const auto& element : *value.m_value.object)
|
|
|
|
{
|
2021-01-16 14:33:05 +00:00
|
|
|
flatten(reference_string + "/" + detail::escape(element.first), element.second, result);
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// add primitive value with its reference string
|
|
|
|
result[reference_string] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@param[in] value flattened JSON
|
|
|
|
|
|
|
|
@return unflattened JSON
|
|
|
|
|
|
|
|
@throw parse_error.109 if array index is not a number
|
|
|
|
@throw type_error.314 if value is not an object
|
|
|
|
@throw type_error.315 if object values are not primitive
|
|
|
|
@throw type_error.313 if value cannot be unflattened
|
|
|
|
*/
|
|
|
|
static BasicJsonType
|
|
|
|
unflatten(const BasicJsonType& value)
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BasicJsonType result;
|
|
|
|
|
|
|
|
// iterate the JSON object values
|
|
|
|
for (const auto& element : *value.m_value.object)
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
|
2018-01-28 14:36:56 +00:00
|
|
|
{
|
2021-01-25 12:47:50 +00:00
|
|
|
JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second));
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assign value to reference pointed to by JSON pointer; Note that if
|
|
|
|
// the JSON pointer is "" (i.e., points to the whole value), function
|
|
|
|
// get_and_create returns a reference to result itself. An assignment
|
|
|
|
// will then create a primitive value.
|
|
|
|
json_pointer(element.first).get_and_create(result) = element.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:06:35 +00:00
|
|
|
/*!
|
|
|
|
@brief compares two JSON pointers for equality
|
|
|
|
|
|
|
|
@param[in] lhs JSON pointer to compare
|
|
|
|
@param[in] rhs JSON pointer to compare
|
|
|
|
@return whether @a lhs is equal to @a rhs
|
|
|
|
|
|
|
|
@complexity Linear in the length of the JSON pointer
|
|
|
|
|
|
|
|
@exceptionsafety No-throw guarantee: this function never throws exceptions.
|
|
|
|
*/
|
2018-01-28 14:36:56 +00:00
|
|
|
friend bool operator==(json_pointer const& lhs,
|
|
|
|
json_pointer const& rhs) noexcept
|
|
|
|
{
|
2019-03-18 12:53:48 +00:00
|
|
|
return lhs.reference_tokens == rhs.reference_tokens;
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 09:06:35 +00:00
|
|
|
/*!
|
|
|
|
@brief compares two JSON pointers for inequality
|
|
|
|
|
|
|
|
@param[in] lhs JSON pointer to compare
|
|
|
|
@param[in] rhs JSON pointer to compare
|
|
|
|
@return whether @a lhs is not equal @a rhs
|
|
|
|
|
|
|
|
@complexity Linear in the length of the JSON pointer
|
|
|
|
|
|
|
|
@exceptionsafety No-throw guarantee: this function never throws exceptions.
|
|
|
|
*/
|
2018-01-28 14:36:56 +00:00
|
|
|
friend bool operator!=(json_pointer const& lhs,
|
|
|
|
json_pointer const& rhs) noexcept
|
|
|
|
{
|
2020-06-03 12:20:36 +00:00
|
|
|
return !(lhs == rhs);
|
2018-01-28 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// the reference tokens
|
|
|
|
std::vector<std::string> reference_tokens;
|
|
|
|
};
|
2018-10-07 16:39:18 +00:00
|
|
|
} // namespace nlohmann
|