All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
nlohmann::json_pointer< BasicJsonType > Class Template Reference

JSON Pointer. More...

#include <json.hpp>

Public Member Functions

 json_pointer (const std::string &s="")
 create JSON pointer More...
 
std::string to_string () const
 return a string representation of the JSON pointer More...
 
 operator std::string () const
 return a string representation of the JSON pointer More...
 
json_pointeroperator/= (const json_pointer &ptr)
 append another JSON pointer at the end of this JSON pointer More...
 
json_pointeroperator/= (std::string token)
 append an unescaped reference token at the end of this JSON pointer More...
 
json_pointeroperator/= (std::size_t array_index)
 append an array index at the end of this JSON pointer More...
 
json_pointer parent_pointer () const
 returns the parent of this JSON pointer More...
 
void pop_back ()
 remove last reference token More...
 
const std::string & back () const
 return last reference token More...
 
void push_back (const std::string &token)
 append an unescaped token at the end of the reference pointer More...
 
void push_back (std::string &&token)
 append an unescaped token at the end of the reference pointer More...
 
bool empty () const noexcept
 return whether pointer points to the root document More...
 

Private Member Functions

json_pointer top () const
 
BasicJsonType & get_and_create (BasicJsonType &j) const
 create and return a reference to the pointed to value More...
 
BasicJsonType & get_unchecked (BasicJsonType *ptr) const
 return a reference to the pointed to value More...
 
BasicJsonType & get_checked (BasicJsonType *ptr) const
 
const BasicJsonType & get_unchecked (const BasicJsonType *ptr) const
 return a const reference to the pointed to value More...
 
const BasicJsonType & get_checked (const BasicJsonType *ptr) const
 
bool contains (const BasicJsonType *ptr) const
 

Static Private Member Functions

static int array_index (const std::string &s)
 
static std::vector< std::string > split (const std::string &reference_string)
 split the string input to reference tokens More...
 
static void replace_substring (std::string &s, const std::string &f, const std::string &t)
 replace all occurrences of a substring by another string More...
 
static std::string escape (std::string s)
 escape "~" to "~0" and "/" to "~1" More...
 
static void unescape (std::string &s)
 unescape "~1" to tilde and "~0" to slash (order is important!) More...
 
static void flatten (const std::string &reference_string, const BasicJsonType &value, BasicJsonType &result)
 
static BasicJsonType unflatten (const BasicJsonType &value)
 

Private Attributes

std::vector< std::string > reference_tokens
 the reference tokens More...
 

Friends

class basic_json
 
json_pointer operator/ (const json_pointer &lhs, const json_pointer &rhs)
 create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer More...
 
json_pointer operator/ (const json_pointer &ptr, std::string token)
 create a new JSON pointer by appending the unescaped token at the end of the JSON pointer More...
 
json_pointer operator/ (const json_pointer &ptr, std::size_t array_index)
 create a new JSON pointer by appending the array-index-token at the end of the JSON pointer More...
 
bool operator== (json_pointer const &lhs, json_pointer const &rhs) noexcept
 compares two JSON pointers for equality More...
 
bool operator!= (json_pointer const &lhs, json_pointer const &rhs) noexcept
 compares two JSON pointers for inequality More...
 

Detailed Description

template<typename BasicJsonType>
class nlohmann::json_pointer< BasicJsonType >

JSON Pointer.

A JSON pointer defines a string syntax for identifying a specific value within a JSON document. It can be used with functions at and operator[]. Furthermore, JSON pointers are the base for JSON patches.

See Also
RFC 6901
Since
version 2.0.0

Definition at line 2435 of file json.hpp.

Constructor & Destructor Documentation

template<typename BasicJsonType >
nlohmann::json_pointer< BasicJsonType >::json_pointer ( const std::string &  s = "")
inlineexplicit

create JSON pointer

Create a JSON pointer according to the syntax described in Section 3 of RFC6901.

Parameters
[in]sstring representing the JSON pointer; if omitted, the empty string is assumed which references the whole JSON value
Exceptions
parse_error.107if the given JSON pointer s is nonempty and does not begin with a slash (/); see example below
parse_error.108if a tilde (~) in the given JSON pointer s is not followed by 0 (representing ~) or 1 (representing /); see example below

{The example shows the construction several valid JSON pointers as well as the exceptional behavior.,json_pointer}

Since
version 2.0.0

Definition at line 10120 of file json.hpp.

10122  {}
static std::vector< std::string > split(const std::string &reference_string)
split the string input to reference tokens
Definition: json.hpp:10850
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083

Member Function Documentation

template<typename BasicJsonType >
static int nlohmann::json_pointer< BasicJsonType >::array_index ( const std::string &  s)
inlinestaticprivate
Parameters
[in]sreference token to be converted into an array index
Returns
integer representation of s
Exceptions
out_of_range.404if string s could not be converted to an integer

Definition at line 10404 of file json.hpp.

10405  {
10406  std::size_t processed_chars = 0;
10407  const int res = std::stoi(s, &processed_chars);
10408 
10409  // check if the string was completely read
10410  if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size()))
10411  {
10412  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'"));
10413  }
10414 
10415  return res;
10416  }
#define JSON_THROW(exception)
Definition: json.hpp:1754
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
template<typename BasicJsonType >
const std::string& nlohmann::json_pointer< BasicJsonType >::back ( ) const
inline

return last reference token

Precondition
not empty()
Returns
last reference token

{The example shows the usage of back.,json_pointer__back}

Constant.

Exceptions
out_of_range.405if JSON pointer has no parent
Since
version 3.6.0

Definition at line 10344 of file json.hpp.

10345  {
10346  if (JSON_HEDLEY_UNLIKELY(empty()))
10347  {
10348  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10349  }
10350 
10351  return reference_tokens.back();
10352  }
#define JSON_THROW(exception)
Definition: json.hpp:1754
bool empty() const noexcept
return whether pointer points to the root document
Definition: json.hpp:10391
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
template<typename BasicJsonType >
bool nlohmann::json_pointer< BasicJsonType >::contains ( const BasicJsonType *  ptr) const
inlineprivate
Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number

Definition at line 10774 of file json.hpp.

10775  {
10776  using size_type = typename BasicJsonType::size_type;
10777  for (const auto& reference_token : reference_tokens)
10778  {
10779  switch (ptr->type())
10780  {
10782  {
10783  if (not ptr->contains(reference_token))
10784  {
10785  // we did not find the key in the object
10786  return false;
10787  }
10788 
10789  ptr = &ptr->operator[](reference_token);
10790  break;
10791  }
10792 
10794  {
10795  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
10796  {
10797  // "-" always fails the range check
10798  return false;
10799  }
10800 
10801  // error condition (cf. RFC 6901, Sect. 4)
10802  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10803  {
10805  "array index '" + reference_token +
10806  "' must not begin with '0'"));
10807  }
10808 
10809  JSON_TRY
10810  {
10811  const auto idx = static_cast<size_type>(array_index(reference_token));
10812  if (idx >= ptr->size())
10813  {
10814  // index out of range
10815  return false;
10816  }
10817 
10818  ptr = &ptr->operator[](idx);
10819  break;
10820  }
10821  JSON_CATCH(std::invalid_argument&)
10822  {
10823  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10824  }
10825  break;
10826  }
10827 
10828  default:
10829  {
10830  // we do not expect primitive values if there is still a
10831  // reference token to process
10832  return false;
10833  }
10834  }
10835  }
10836 
10837  // no reference token left means we found a primitive value
10838  return true;
10839  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
bool nlohmann::json_pointer< BasicJsonType >::empty ( ) const
inlinenoexcept

return whether pointer points to the root document

Returns
true iff the JSON pointer points to the root document

Constant.

No-throw guarantee: this function never throws exceptions.

{The example shows the result of empty for different JSON Pointers.,json_pointer__empty}

Since
version 3.6.0

Definition at line 10391 of file json.hpp.

10392  {
10393  return reference_tokens.empty();
10394  }
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
template<typename BasicJsonType >
static std::string nlohmann::json_pointer< BasicJsonType >::escape ( std::string  s)
inlinestaticprivate

escape "~" to "~0" and "/" to "~1"

Definition at line 10937 of file json.hpp.

10938  {
10939  replace_substring(s, "~", "~0");
10940  replace_substring(s, "/", "~1");
10941  return s;
10942  }
static void replace_substring(std::string &s, const std::string &f, const std::string &t)
replace all occurrences of a substring by another string
Definition: json.hpp:10925
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
template<typename BasicJsonType >
static void nlohmann::json_pointer< BasicJsonType >::flatten ( const std::string &  reference_string,
const BasicJsonType &  value,
BasicJsonType &  result 
)
inlinestaticprivate
Parameters
[in]reference_stringthe reference string to the current value
[in]valuethe value to consider
[in,out]resultthe result object to insert values to
Note
Empty objects or arrays are flattened to null.

Definition at line 10958 of file json.hpp.

10961  {
10962  switch (value.type())
10963  {
10965  {
10966  if (value.m_value.array->empty())
10967  {
10968  // flatten empty array as null
10969  result[reference_string] = nullptr;
10970  }
10971  else
10972  {
10973  // iterate array and use index as reference string
10974  for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
10975  {
10976  flatten(reference_string + "/" + std::to_string(i),
10977  value.m_value.array->operator[](i), result);
10978  }
10979  }
10980  break;
10981  }
10982 
10984  {
10985  if (value.m_value.object->empty())
10986  {
10987  // flatten empty object as null
10988  result[reference_string] = nullptr;
10989  }
10990  else
10991  {
10992  // iterate object and use keys as reference string
10993  for (const auto& element : *value.m_value.object)
10994  {
10995  flatten(reference_string + "/" + escape(element.first), element.second, result);
10996  }
10997  }
10998  break;
10999  }
11000 
11001  default:
11002  {
11003  // add primitive value with its reference string
11004  result[reference_string] = value;
11005  break;
11006  }
11007  }
11008  }
array (ordered collection of values)
static std::string escape(std::string s)
escape &quot;~&quot; to &quot;~0&quot; and &quot;/&quot; to &quot;~1&quot;
Definition: json.hpp:10937
object (unordered set of name/value pairs)
static void flatten(const std::string &reference_string, const BasicJsonType &value, BasicJsonType &result)
Definition: json.hpp:10958
std::string to_string(WindowPattern const &pattern)
temporary value
template<typename BasicJsonType >
BasicJsonType& nlohmann::json_pointer< BasicJsonType >::get_and_create ( BasicJsonType &  j) const
inlineprivate

create and return a reference to the pointed to value

Linear in the number of reference tokens.

Exceptions
parse_error.109if array index is not a number
type_error.313if value cannot be unflattened

Definition at line 10438 of file json.hpp.

10439  {
10440  using size_type = typename BasicJsonType::size_type;
10441  auto result = &j;
10442 
10443  // in case no reference tokens exist, return a reference to the JSON value
10444  // j which will be overwritten by a primitive value
10445  for (const auto& reference_token : reference_tokens)
10446  {
10447  switch (result->type())
10448  {
10449  case detail::value_t::null:
10450  {
10451  if (reference_token == "0")
10452  {
10453  // start a new array if reference token is 0
10454  result = &result->operator[](0);
10455  }
10456  else
10457  {
10458  // start a new object otherwise
10459  result = &result->operator[](reference_token);
10460  }
10461  break;
10462  }
10463 
10465  {
10466  // create an entry in the object
10467  result = &result->operator[](reference_token);
10468  break;
10469  }
10470 
10472  {
10473  // create an entry in the array
10474  JSON_TRY
10475  {
10476  result = &result->operator[](static_cast<size_type>(array_index(reference_token)));
10477  }
10478  JSON_CATCH(std::invalid_argument&)
10479  {
10480  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10481  }
10482  break;
10483  }
10484 
10485  /*
10486  The following code is only reached if there exists a reference
10487  token _and_ the current value is primitive. In this case, we have
10488  an error situation, because primitive values may only occur as
10489  single value; that is, with an empty list of reference tokens.
10490  */
10491  default:
10492  JSON_THROW(detail::type_error::create(313, "invalid value to unflatten"));
10493  }
10494  }
10495 
10496  return *result;
10497  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
BasicJsonType& nlohmann::json_pointer< BasicJsonType >::get_checked ( BasicJsonType *  ptr) const
inlineprivate
Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.402if the array index '-' is used
out_of_range.404if the JSON pointer can not be resolved

Definition at line 10594 of file json.hpp.

10595  {
10596  using size_type = typename BasicJsonType::size_type;
10597  for (const auto& reference_token : reference_tokens)
10598  {
10599  switch (ptr->type())
10600  {
10602  {
10603  // note: at performs range check
10604  ptr = &ptr->at(reference_token);
10605  break;
10606  }
10607 
10609  {
10610  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
10611  {
10612  // "-" always fails the range check
10614  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
10615  ") is out of range"));
10616  }
10617 
10618  // error condition (cf. RFC 6901, Sect. 4)
10619  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10620  {
10622  "array index '" + reference_token +
10623  "' must not begin with '0'"));
10624  }
10625 
10626  // note: at performs range check
10627  JSON_TRY
10628  {
10629  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
10630  }
10631  JSON_CATCH(std::invalid_argument&)
10632  {
10633  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10634  }
10635  break;
10636  }
10637 
10638  default:
10639  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10640  }
10641  }
10642 
10643  return *ptr;
10644  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
std::string to_string(WindowPattern const &pattern)
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
const BasicJsonType& nlohmann::json_pointer< BasicJsonType >::get_checked ( const BasicJsonType *  ptr) const
inlineprivate
Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.402if the array index '-' is used
out_of_range.404if the JSON pointer can not be resolved

Definition at line 10718 of file json.hpp.

10719  {
10720  using size_type = typename BasicJsonType::size_type;
10721  for (const auto& reference_token : reference_tokens)
10722  {
10723  switch (ptr->type())
10724  {
10726  {
10727  // note: at performs range check
10728  ptr = &ptr->at(reference_token);
10729  break;
10730  }
10731 
10733  {
10734  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
10735  {
10736  // "-" always fails the range check
10738  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
10739  ") is out of range"));
10740  }
10741 
10742  // error condition (cf. RFC 6901, Sect. 4)
10743  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10744  {
10746  "array index '" + reference_token +
10747  "' must not begin with '0'"));
10748  }
10749 
10750  // note: at performs range check
10751  JSON_TRY
10752  {
10753  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
10754  }
10755  JSON_CATCH(std::invalid_argument&)
10756  {
10757  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10758  }
10759  break;
10760  }
10761 
10762  default:
10763  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10764  }
10765  }
10766 
10767  return *ptr;
10768  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
std::string to_string(WindowPattern const &pattern)
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
BasicJsonType& nlohmann::json_pointer< BasicJsonType >::get_unchecked ( BasicJsonType *  ptr) const
inlineprivate

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.
Parameters
[in]ptra JSON value
Returns
reference to the JSON value pointed to by the JSON pointer

Linear in the length of the JSON pointer.

Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.404if the JSON pointer can not be resolved

Definition at line 10518 of file json.hpp.

10519  {
10520  using size_type = typename BasicJsonType::size_type;
10521  for (const auto& reference_token : reference_tokens)
10522  {
10523  // convert null values to arrays or objects before continuing
10524  if (ptr->is_null())
10525  {
10526  // check if reference token is a number
10527  const bool nums =
10528  std::all_of(reference_token.begin(), reference_token.end(),
10529  [](const unsigned char x)
10530  {
10531  return std::isdigit(x);
10532  });
10533 
10534  // change value to array for numbers or "-" or to object otherwise
10535  *ptr = (nums or reference_token == "-")
10538  }
10539 
10540  switch (ptr->type())
10541  {
10543  {
10544  // use unchecked object access
10545  ptr = &ptr->operator[](reference_token);
10546  break;
10547  }
10548 
10550  {
10551  // error condition (cf. RFC 6901, Sect. 4)
10552  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10553  {
10555  "array index '" + reference_token +
10556  "' must not begin with '0'"));
10557  }
10558 
10559  if (reference_token == "-")
10560  {
10561  // explicitly treat "-" as index beyond the end
10562  ptr = &ptr->operator[](ptr->m_value.array->size());
10563  }
10564  else
10565  {
10566  // convert array index to number; unchecked access
10567  JSON_TRY
10568  {
10569  ptr = &ptr->operator[](
10570  static_cast<size_type>(array_index(reference_token)));
10571  }
10572  JSON_CATCH(std::invalid_argument&)
10573  {
10574  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10575  }
10576  }
10577  break;
10578  }
10579 
10580  default:
10581  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10582  }
10583  }
10584 
10585  return *ptr;
10586  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
process_name opflash particleana ie x
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
const BasicJsonType& nlohmann::json_pointer< BasicJsonType >::get_unchecked ( const BasicJsonType *  ptr) const
inlineprivate

return a const reference to the pointed to value

Parameters
[in]ptra JSON value
Returns
const reference to the JSON value pointed to by the JSON pointer
Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.402if the array index '-' is used
out_of_range.404if the JSON pointer can not be resolved

Definition at line 10659 of file json.hpp.

10660  {
10661  using size_type = typename BasicJsonType::size_type;
10662  for (const auto& reference_token : reference_tokens)
10663  {
10664  switch (ptr->type())
10665  {
10667  {
10668  // use unchecked object access
10669  ptr = &ptr->operator[](reference_token);
10670  break;
10671  }
10672 
10674  {
10675  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
10676  {
10677  // "-" cannot be used for const access
10679  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
10680  ") is out of range"));
10681  }
10682 
10683  // error condition (cf. RFC 6901, Sect. 4)
10684  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10685  {
10687  "array index '" + reference_token +
10688  "' must not begin with '0'"));
10689  }
10690 
10691  // use unchecked array access
10692  JSON_TRY
10693  {
10694  ptr = &ptr->operator[](
10695  static_cast<size_type>(array_index(reference_token)));
10696  }
10697  JSON_CATCH(std::invalid_argument&)
10698  {
10699  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10700  }
10701  break;
10702  }
10703 
10704  default:
10705  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10706  }
10707  }
10708 
10709  return *ptr;
10710  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
std::string to_string(WindowPattern const &pattern)
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
nlohmann::json_pointer< BasicJsonType >::operator std::string ( ) const
inline

return a string representation of the JSON pointer

Invariant
For each JSON pointer ptr, it holds:
ptr == json_pointer(ptr.to_string());
Returns
a string representation of the JSON pointer

{The example shows the result of to_string.,json_pointer__to_string}

Since
version 2.0.0

Definition at line 10149 of file json.hpp.

10150  {
10151  return to_string();
10152  }
std::string to_string() const
return a string representation of the JSON pointer
Definition: json.hpp:10138
template<typename BasicJsonType >
json_pointer& nlohmann::json_pointer< BasicJsonType >::operator/= ( const json_pointer< BasicJsonType > &  ptr)
inline

append another JSON pointer at the end of this JSON pointer

Parameters
[in]ptrJSON pointer to append
Returns
JSON pointer with ptr appended

{The example shows the usage of operator/=.,json_pointer__operator_add}

Linear in the length of ptr.

See Also
operator/=(std::string) to append a reference token
operator/=(std::size_t) to append an array index
operator/(const json_pointer&, const json_pointer&) for a binary operator
Since
version 3.6.0

Definition at line 10170 of file json.hpp.

10171  {
10172  reference_tokens.insert(reference_tokens.end(),
10173  ptr.reference_tokens.begin(),
10174  ptr.reference_tokens.end());
10175  return *this;
10176  }
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
template<typename BasicJsonType >
json_pointer& nlohmann::json_pointer< BasicJsonType >::operator/= ( std::string  token)
inline

append an unescaped reference token at the end of this JSON pointer

Parameters
[in]tokenreference token to append
Returns
JSON pointer with token appended without escaping token

{The example shows the usage of operator/=.,json_pointer__operator_add}

Amortized constant.

See Also
operator/=(const json_pointer&) to append a JSON pointer
operator/=(std::size_t) to append an array index
operator/(const json_pointer&, std::size_t) for a binary operator
Since
version 3.6.0

Definition at line 10194 of file json.hpp.

10195  {
10196  push_back(std::move(token));
10197  return *this;
10198  }
void push_back(const std::string &token)
append an unescaped token at the end of the reference pointer
Definition: json.hpp:10366
template<typename BasicJsonType >
json_pointer& nlohmann::json_pointer< BasicJsonType >::operator/= ( std::size_t  array_index)
inline

append an array index at the end of this JSON pointer

Parameters
[in]array_indexarray index to append
Returns
JSON pointer with array_index appended

{The example shows the usage of operator/=.,json_pointer__operator_add}

Amortized constant.

See Also
operator/=(const json_pointer&) to append a JSON pointer
operator/=(std::string) to append a reference token
operator/(const json_pointer&, std::string) for a binary operator
Since
version 3.6.0

Definition at line 10216 of file json.hpp.

10217  {
10218  return *this /= std::to_string(array_index);
10219  }
static int array_index(const std::string &s)
Definition: json.hpp:10404
std::string to_string(WindowPattern const &pattern)
template<typename BasicJsonType >
json_pointer nlohmann::json_pointer< BasicJsonType >::parent_pointer ( ) const
inline

returns the parent of this JSON pointer

Returns
parent of this JSON pointer; in case this JSON pointer is the root, the root itself is returned

Linear in the length of the JSON pointer.

{The example shows the result of parent_pointer for different JSON Pointers.,json_pointer__parent_pointer}

Since
version 3.6.0

Definition at line 10295 of file json.hpp.

10296  {
10297  if (empty())
10298  {
10299  return *this;
10300  }
10301 
10302  json_pointer res = *this;
10303  res.pop_back();
10304  return res;
10305  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
bool empty() const noexcept
return whether pointer points to the root document
Definition: json.hpp:10391
template<typename BasicJsonType >
void nlohmann::json_pointer< BasicJsonType >::pop_back ( )
inline

remove last reference token

Precondition
not empty()

{The example shows the usage of pop_back.,json_pointer__pop_back}

Constant.

Exceptions
out_of_range.405if JSON pointer has no parent
Since
version 3.6.0

Definition at line 10320 of file json.hpp.

10321  {
10322  if (JSON_HEDLEY_UNLIKELY(empty()))
10323  {
10324  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10325  }
10326 
10327  reference_tokens.pop_back();
10328  }
#define JSON_THROW(exception)
Definition: json.hpp:1754
bool empty() const noexcept
return whether pointer points to the root document
Definition: json.hpp:10391
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
template<typename BasicJsonType >
void nlohmann::json_pointer< BasicJsonType >::push_back ( const std::string &  token)
inline

append an unescaped token at the end of the reference pointer

Parameters
[in]tokentoken to add

Amortized constant.

{The example shows the result of push_back for different JSON Pointers.,json_pointer__push_back}

Since
version 3.6.0

Definition at line 10366 of file json.hpp.

10367  {
10368  reference_tokens.push_back(token);
10369  }
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
template<typename BasicJsonType >
void nlohmann::json_pointer< BasicJsonType >::push_back ( std::string &&  token)
inline

append an unescaped token at the end of the reference pointer

Parameters
[in]tokentoken to add

Amortized constant.

{The example shows the result of push_back for different JSON Pointers.,json_pointer__push_back}

Since
version 3.6.0

Definition at line 10372 of file json.hpp.

10373  {
10374  reference_tokens.push_back(std::move(token));
10375  }
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
template<typename BasicJsonType >
static void nlohmann::json_pointer< BasicJsonType >::replace_substring ( std::string &  s,
const std::string &  f,
const std::string &  t 
)
inlinestaticprivate

replace all occurrences of a substring by another string

Parameters
[in,out]sthe string to manipulate; changed so that all occurrences of f are replaced with t
[in]fthe substring to replace with t
[in]tthe string to replace f
Precondition
The search string f must not be empty. This precondition is enforced with an assertion.
Since
version 2.0.0

Definition at line 10925 of file json.hpp.

10927  {
10928  assert(not f.empty());
10929  for (auto pos = s.find(f); // find first occurrence of f
10930  pos != std::string::npos; // make sure f was found
10931  s.replace(pos, f.size(), t), // replace with t, and
10932  pos = s.find(f, pos + t.size())) // find next occurrence of f
10933  {}
10934  }
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
template<typename BasicJsonType >
static std::vector<std::string> nlohmann::json_pointer< BasicJsonType >::split ( const std::string &  reference_string)
inlinestaticprivate

split the string input to reference tokens

Note
This function is only called by the json_pointer constructor. All exceptions below are documented there.
Exceptions
parse_error.107if the pointer is not empty or begins with '/'
parse_error.108if character '~' is not followed by '0' or '1'

Definition at line 10850 of file json.hpp.

10851  {
10852  std::vector<std::string> result;
10853 
10854  // special case: empty reference string -> no reference tokens
10855  if (reference_string.empty())
10856  {
10857  return result;
10858  }
10859 
10860  // check if nonempty reference string begins with slash
10861  if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
10862  {
10864  "JSON pointer must be empty or begin with '/' - was: '" +
10865  reference_string + "'"));
10866  }
10867 
10868  // extract the reference tokens:
10869  // - slash: position of the last read slash (or end of string)
10870  // - start: position after the previous slash
10871  for (
10872  // search for the first slash after the first character
10873  std::size_t slash = reference_string.find_first_of('/', 1),
10874  // set the beginning of the first reference token
10875  start = 1;
10876  // we can stop if start == 0 (if slash == std::string::npos)
10877  start != 0;
10878  // set the beginning of the next reference token
10879  // (will eventually be 0 if slash == std::string::npos)
10880  start = (slash == std::string::npos) ? 0 : slash + 1,
10881  // find next slash
10882  slash = reference_string.find_first_of('/', start))
10883  {
10884  // use the text between the beginning of the reference token
10885  // (start) and the last slash (slash).
10886  auto reference_token = reference_string.substr(start, slash - start);
10887 
10888  // check reference tokens are properly escaped
10889  for (std::size_t pos = reference_token.find_first_of('~');
10890  pos != std::string::npos;
10891  pos = reference_token.find_first_of('~', pos + 1))
10892  {
10893  assert(reference_token[pos] == '~');
10894 
10895  // ~ must be followed by 0 or 1
10896  if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 or
10897  (reference_token[pos + 1] != '0' and
10898  reference_token[pos + 1] != '1')))
10899  {
10900  JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'"));
10901  }
10902  }
10903 
10904  // finally, store the reference token
10905  unescape(reference_token);
10906  result.push_back(reference_token);
10907  }
10908 
10909  return result;
10910  }
static void unescape(std::string &s)
unescape &quot;~1&quot; to tilde and &quot;~0&quot; to slash (order is important!)
Definition: json.hpp:10945
#define JSON_THROW(exception)
Definition: json.hpp:1754
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<typename BasicJsonType >
std::string nlohmann::json_pointer< BasicJsonType >::to_string ( ) const
inline

return a string representation of the JSON pointer

Invariant
For each JSON pointer ptr, it holds:
ptr == json_pointer(ptr.to_string());
Returns
a string representation of the JSON pointer

{The example shows the result of to_string.,json_pointer__to_string}

Since
version 2.0.0

Definition at line 10138 of file json.hpp.

10139  {
10140  return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
10141  std::string{},
10142  [](const std::string & a, const std::string & b)
10143  {
10144  return a + "/" + escape(b);
10145  });
10146  }
static std::string escape(std::string s)
escape &quot;~&quot; to &quot;~0&quot; and &quot;/&quot; to &quot;~1&quot;
Definition: json.hpp:10937
process_name gaushit a
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
template<typename BasicJsonType >
json_pointer nlohmann::json_pointer< BasicJsonType >::top ( ) const
inlineprivate

Definition at line 10418 of file json.hpp.

10419  {
10420  if (JSON_HEDLEY_UNLIKELY(empty()))
10421  {
10422  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10423  }
10424 
10425  json_pointer result = *this;
10426  result.reference_tokens = {reference_tokens[0]};
10427  return result;
10428  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
#define JSON_THROW(exception)
Definition: json.hpp:1754
bool empty() const noexcept
return whether pointer points to the root document
Definition: json.hpp:10391
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:11083
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
template<typename BasicJsonType >
static void nlohmann::json_pointer< BasicJsonType >::unescape ( std::string &  s)
inlinestaticprivate

unescape "~1" to tilde and "~0" to slash (order is important!)

Definition at line 10945 of file json.hpp.

10946  {
10947  replace_substring(s, "~1", "/");
10948  replace_substring(s, "~0", "~");
10949  }
static void replace_substring(std::string &s, const std::string &f, const std::string &t)
replace all occurrences of a substring by another string
Definition: json.hpp:10925
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
template<typename BasicJsonType >
static BasicJsonType nlohmann::json_pointer< BasicJsonType >::unflatten ( const BasicJsonType &  value)
inlinestaticprivate
Parameters
[in]valueflattened JSON
Returns
unflattened JSON
Exceptions
parse_error.109if array index is not a number
type_error.314if value is not an object
type_error.315if object values are not primitive
type_error.313if value cannot be unflattened

Definition at line 11021 of file json.hpp.

11022  {
11023  if (JSON_HEDLEY_UNLIKELY(not value.is_object()))
11024  {
11025  JSON_THROW(detail::type_error::create(314, "only objects can be unflattened"));
11026  }
11027 
11028  BasicJsonType result;
11029 
11030  // iterate the JSON object values
11031  for (const auto& element : *value.m_value.object)
11032  {
11033  if (JSON_HEDLEY_UNLIKELY(not element.second.is_primitive()))
11034  {
11035  JSON_THROW(detail::type_error::create(315, "values in object must be primitive"));
11036  }
11037 
11038  // assign value to reference pointed to by JSON pointer; Note that if
11039  // the JSON pointer is "" (i.e., points to the whole value), function
11040  // get_and_create returns a reference to result itself. An assignment
11041  // will then create a primitive value.
11042  json_pointer(element.first).get_and_create(result) = element.second;
11043  }
11044 
11045  return result;
11046  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
#define JSON_THROW(exception)
Definition: json.hpp:1754
temporary value
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078

Friends And Related Function Documentation

template<typename BasicJsonType >
friend class basic_json
friend

Definition at line 10096 of file json.hpp.

template<typename BasicJsonType >
bool operator!= ( json_pointer< BasicJsonType > const &  lhs,
json_pointer< BasicJsonType > const &  rhs 
)
friend

compares two JSON pointers for inequality

Parameters
[in]lhsJSON pointer to compare
[in]rhsJSON pointer to compare
Returns
whether lhs is not equal rhs

Linear in the length of the JSON pointer

No-throw guarantee: this function never throws exceptions.

Definition at line 11076 of file json.hpp.

11078  {
11079  return not (lhs == rhs);
11080  }
template<typename BasicJsonType >
json_pointer operator/ ( const json_pointer< BasicJsonType > &  lhs,
const json_pointer< BasicJsonType > &  rhs 
)
friend

create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer

Parameters
[in]lhsJSON pointer
[in]rhsJSON pointer
Returns
a new JSON pointer with rhs appended to lhs

{The example shows the usage of operator/.,json_pointer__operator_add_binary}

Linear in the length of lhs and rhs.

See Also
operator/=(const json_pointer&) to append a JSON pointer
Since
version 3.6.0

Definition at line 10236 of file json.hpp.

10238  {
10239  return json_pointer(lhs) /= rhs;
10240  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
template<typename BasicJsonType >
json_pointer operator/ ( const json_pointer< BasicJsonType > &  ptr,
std::string  token 
)
friend

create a new JSON pointer by appending the unescaped token at the end of the JSON pointer

Parameters
[in]ptrJSON pointer
[in]tokenreference token
Returns
a new JSON pointer with unescaped token appended to ptr

{The example shows the usage of operator/.,json_pointer__operator_add_binary}

Linear in the length of ptr.

See Also
operator/=(std::string) to append a reference token
Since
version 3.6.0

Definition at line 10257 of file json.hpp.

10258  {
10259  return json_pointer(ptr) /= std::move(token);
10260  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
template<typename BasicJsonType >
json_pointer operator/ ( const json_pointer< BasicJsonType > &  ptr,
std::size_t  array_index 
)
friend

create a new JSON pointer by appending the array-index-token at the end of the JSON pointer

Parameters
[in]ptrJSON pointer
[in]array_indexarray index
Returns
a new JSON pointer with array_index appended to ptr

{The example shows the usage of operator/.,json_pointer__operator_add_binary}

Linear in the length of ptr.

See Also
operator/=(std::size_t) to append an array index
Since
version 3.6.0

Definition at line 10277 of file json.hpp.

10278  {
10279  return json_pointer(ptr) /= array_index;
10280  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:10120
static int array_index(const std::string &s)
Definition: json.hpp:10404
template<typename BasicJsonType >
bool operator== ( json_pointer< BasicJsonType > const &  lhs,
json_pointer< BasicJsonType > const &  rhs 
)
friend

compares two JSON pointers for equality

Parameters
[in]lhsJSON pointer to compare
[in]rhsJSON pointer to compare
Returns
whether lhs is equal to rhs

Linear in the length of the JSON pointer

No-throw guarantee: this function never throws exceptions.

Definition at line 11059 of file json.hpp.

11061  {
11062  return lhs.reference_tokens == rhs.reference_tokens;
11063  }

Member Data Documentation

template<typename BasicJsonType >
std::vector<std::string> nlohmann::json_pointer< BasicJsonType >::reference_tokens
private

the reference tokens

Definition at line 11083 of file json.hpp.


The documentation for this class was generated from the following file: