Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
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...
 

Static Public Member Functions

static int array_index (const std::string &s)
 

Private Member Functions

std::string pop_back ()
 remove and return last reference pointer More...
 
bool is_root () const noexcept
 return whether pointer points to the root document More...
 
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
 

Static Private Member Functions

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

template<template< typename, typename, typename... > class ObjectType, template< typename, typename... > class ArrayType, class StringType , class BooleanType , class NumberIntegerType , class NumberUnsignedType , class NumberFloatType , template< typename > class AllocatorType, template< typename, typename=void > class JSONSerializer>
class basic_json
 
bool operator== (json_pointer const &lhs, json_pointer const &rhs) noexcept
 
bool operator!= (json_pointer const &lhs, json_pointer const &rhs) noexcept
 

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 100 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 11623 of file json.hpp.

11624  : reference_tokens(split(s))
11625  {}
static std::vector< std::string > split(const std::string &reference_string)
split the string input to reference tokens
Definition: json.hpp:12063
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274

Member Function Documentation

template<typename BasicJsonType >
static int nlohmann::json_pointer< BasicJsonType >::array_index ( const std::string &  s)
inlinestatic
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 11665 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, and JSON_UNLIKELY.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::patch().

11666  {
11667  std::size_t processed_chars = 0;
11668  const int res = std::stoi(s, &processed_chars);
11669 
11670  // check if the string was completely read
11671  if (JSON_UNLIKELY(processed_chars != s.size()))
11672  {
11673  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'"));
11674  }
11675 
11676  return res;
11677  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
template<typename BasicJsonType >
static std::string nlohmann::json_pointer< BasicJsonType >::escape ( std::string  s)
inlinestaticprivate

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

Definition at line 12150 of file json.hpp.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::diff().

12151  {
12152  replace_substring(s, "~", "~0");
12153  replace_substring(s, "/", "~1");
12154  return s;
12155  }
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:12138
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 12171 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::object, and Catch::Generators::value().

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::flatten().

12174  {
12175  switch (value.m_type)
12176  {
12178  {
12179  if (value.m_value.array->empty())
12180  {
12181  // flatten empty array as null
12182  result[reference_string] = nullptr;
12183  }
12184  else
12185  {
12186  // iterate array and use index as reference string
12187  for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
12188  {
12189  flatten(reference_string + "/" + std::to_string(i),
12190  value.m_value.array->operator[](i), result);
12191  }
12192  }
12193  break;
12194  }
12195 
12197  {
12198  if (value.m_value.object->empty())
12199  {
12200  // flatten empty object as null
12201  result[reference_string] = nullptr;
12202  }
12203  else
12204  {
12205  // iterate object and use keys as reference string
12206  for (const auto& element : *value.m_value.object)
12207  {
12208  flatten(reference_string + "/" + escape(element.first), element.second, result);
12209  }
12210  }
12211  break;
12212  }
12213 
12214  default:
12215  {
12216  // add primitive value with its reference string
12217  result[reference_string] = value;
12218  break;
12219  }
12220  }
12221  }
array (ordered collection of values)
static std::string escape(std::string s)
escape "~" to "~0" and "/" to "~1"
Definition: json.hpp:12150
object (unordered set of name/value pairs)
static void flatten(const std::string &reference_string, const BasicJsonType &value, BasicJsonType &result)
Definition: json.hpp:12171
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
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 11722 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::parse_error::create(), nlohmann::detail::type_error::create(), JSON_CATCH, JSON_THROW, JSON_TRY, nlohmann::detail::null, and nlohmann::detail::object.

Referenced by nlohmann::json_pointer< BasicJsonType >::unflatten().

11723  {
11724  using size_type = typename BasicJsonType::size_type;
11725  auto result = &j;
11726 
11727  // in case no reference tokens exist, return a reference to the JSON value
11728  // j which will be overwritten by a primitive value
11729  for (const auto& reference_token : reference_tokens)
11730  {
11731  switch (result->m_type)
11732  {
11733  case detail::value_t::null:
11734  {
11735  if (reference_token == "0")
11736  {
11737  // start a new array if reference token is 0
11738  result = &result->operator[](0);
11739  }
11740  else
11741  {
11742  // start a new object otherwise
11743  result = &result->operator[](reference_token);
11744  }
11745  break;
11746  }
11747 
11749  {
11750  // create an entry in the object
11751  result = &result->operator[](reference_token);
11752  break;
11753  }
11754 
11756  {
11757  // create an entry in the array
11758  JSON_TRY
11759  {
11760  result = &result->operator[](static_cast<size_type>(array_index(reference_token)));
11761  }
11762  JSON_CATCH(std::invalid_argument&)
11763  {
11764  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11765  }
11766  break;
11767  }
11768 
11769  /*
11770  The following code is only reached if there exists a reference
11771  token _and_ the current value is primitive. In this case, we have
11772  an error situation, because primitive values may only occur as
11773  single value; that is, with an empty list of reference tokens.
11774  */
11775  default:
11776  JSON_THROW(detail::type_error::create(313, "invalid value to unflatten"));
11777  }
11778  }
11779 
11780  return *result;
11781  }
#define JSON_CATCH(exception)
Definition: json.hpp:164
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static int array_index(const std::string &s)
Definition: json.hpp:11665
#define JSON_TRY
Definition: json.hpp:163
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:1024
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
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 11878 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::parse_error::create(), nlohmann::detail::out_of_range::create(), JSON_CATCH, JSON_THROW, JSON_TRY, JSON_UNLIKELY, and nlohmann::detail::object.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at(), and nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value().

11879  {
11880  using size_type = typename BasicJsonType::size_type;
11881  for (const auto& reference_token : reference_tokens)
11882  {
11883  switch (ptr->m_type)
11884  {
11886  {
11887  // note: at performs range check
11888  ptr = &ptr->at(reference_token);
11889  break;
11890  }
11891 
11893  {
11894  if (JSON_UNLIKELY(reference_token == "-"))
11895  {
11896  // "-" always fails the range check
11898  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
11899  ") is out of range"));
11900  }
11901 
11902  // error condition (cf. RFC 6901, Sect. 4)
11903  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11904  {
11906  "array index '" + reference_token +
11907  "' must not begin with '0'"));
11908  }
11909 
11910  // note: at performs range check
11911  JSON_TRY
11912  {
11913  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
11914  }
11915  JSON_CATCH(std::invalid_argument&)
11916  {
11917  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11918  }
11919  break;
11920  }
11921 
11922  default:
11923  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11924  }
11925  }
11926 
11927  return *ptr;
11928  }
#define JSON_CATCH(exception)
Definition: json.hpp:164
array (ordered collection of values)
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static int array_index(const std::string &s)
Definition: json.hpp:11665
#define JSON_TRY
Definition: json.hpp:163
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
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 12002 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::parse_error::create(), nlohmann::detail::out_of_range::create(), JSON_CATCH, JSON_THROW, JSON_TRY, JSON_UNLIKELY, and nlohmann::detail::object.

12003  {
12004  using size_type = typename BasicJsonType::size_type;
12005  for (const auto& reference_token : reference_tokens)
12006  {
12007  switch (ptr->m_type)
12008  {
12010  {
12011  // note: at performs range check
12012  ptr = &ptr->at(reference_token);
12013  break;
12014  }
12015 
12017  {
12018  if (JSON_UNLIKELY(reference_token == "-"))
12019  {
12020  // "-" always fails the range check
12022  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12023  ") is out of range"));
12024  }
12025 
12026  // error condition (cf. RFC 6901, Sect. 4)
12027  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
12028  {
12030  "array index '" + reference_token +
12031  "' must not begin with '0'"));
12032  }
12033 
12034  // note: at performs range check
12035  JSON_TRY
12036  {
12037  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
12038  }
12039  JSON_CATCH(std::invalid_argument&)
12040  {
12041  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
12042  }
12043  break;
12044  }
12045 
12046  default:
12047  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
12048  }
12049  }
12050 
12051  return *ptr;
12052  }
#define JSON_CATCH(exception)
Definition: json.hpp:164
array (ordered collection of values)
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static int array_index(const std::string &s)
Definition: json.hpp:11665
#define JSON_TRY
Definition: json.hpp:163
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
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 11802 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::parse_error::create(), nlohmann::detail::out_of_range::create(), JSON_CATCH, JSON_THROW, JSON_TRY, JSON_UNLIKELY, nlohmann::detail::null, nlohmann::detail::object, and x.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[]().

11803  {
11804  using size_type = typename BasicJsonType::size_type;
11805  for (const auto& reference_token : reference_tokens)
11806  {
11807  // convert null values to arrays or objects before continuing
11808  if (ptr->m_type == detail::value_t::null)
11809  {
11810  // check if reference token is a number
11811  const bool nums =
11812  std::all_of(reference_token.begin(), reference_token.end(),
11813  [](const char x)
11814  {
11815  return (x >= '0' and x <= '9');
11816  });
11817 
11818  // change value to array for numbers or "-" or to object otherwise
11819  *ptr = (nums or reference_token == "-")
11822  }
11823 
11824  switch (ptr->m_type)
11825  {
11827  {
11828  // use unchecked object access
11829  ptr = &ptr->operator[](reference_token);
11830  break;
11831  }
11832 
11834  {
11835  // error condition (cf. RFC 6901, Sect. 4)
11836  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11837  {
11839  "array index '" + reference_token +
11840  "' must not begin with '0'"));
11841  }
11842 
11843  if (reference_token == "-")
11844  {
11845  // explicitly treat "-" as index beyond the end
11846  ptr = &ptr->operator[](ptr->m_value.array->size());
11847  }
11848  else
11849  {
11850  // convert array index to number; unchecked access
11851  JSON_TRY
11852  {
11853  ptr = &ptr->operator[](
11854  static_cast<size_type>(array_index(reference_token)));
11855  }
11856  JSON_CATCH(std::invalid_argument&)
11857  {
11858  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11859  }
11860  }
11861  break;
11862  }
11863 
11864  default:
11865  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11866  }
11867  }
11868 
11869  return *ptr;
11870  }
#define JSON_CATCH(exception)
Definition: json.hpp:164
array (ordered collection of values)
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static CCL_BEGIN_DECLS double x[111][8]
static int array_index(const std::string &s)
Definition: json.hpp:11665
#define JSON_TRY
Definition: json.hpp:163
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
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 11943 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::parse_error::create(), nlohmann::detail::out_of_range::create(), JSON_CATCH, JSON_THROW, JSON_TRY, JSON_UNLIKELY, and nlohmann::detail::object.

11944  {
11945  using size_type = typename BasicJsonType::size_type;
11946  for (const auto& reference_token : reference_tokens)
11947  {
11948  switch (ptr->m_type)
11949  {
11951  {
11952  // use unchecked object access
11953  ptr = &ptr->operator[](reference_token);
11954  break;
11955  }
11956 
11958  {
11959  if (JSON_UNLIKELY(reference_token == "-"))
11960  {
11961  // "-" cannot be used for const access
11963  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
11964  ") is out of range"));
11965  }
11966 
11967  // error condition (cf. RFC 6901, Sect. 4)
11968  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11969  {
11971  "array index '" + reference_token +
11972  "' must not begin with '0'"));
11973  }
11974 
11975  // use unchecked array access
11976  JSON_TRY
11977  {
11978  ptr = &ptr->operator[](
11979  static_cast<size_type>(array_index(reference_token)));
11980  }
11981  JSON_CATCH(std::invalid_argument&)
11982  {
11983  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11984  }
11985  break;
11986  }
11987 
11988  default:
11989  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11990  }
11991  }
11992 
11993  return *ptr;
11994  }
#define JSON_CATCH(exception)
Definition: json.hpp:164
array (ordered collection of values)
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static int array_index(const std::string &s)
Definition: json.hpp:11665
#define JSON_TRY
Definition: json.hpp:163
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
template<typename BasicJsonType >
bool nlohmann::json_pointer< BasicJsonType >::is_root ( ) const
inlineprivatenoexcept

return whether pointer points to the root document

Definition at line 11697 of file json.hpp.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::patch().

11698  {
11699  return reference_tokens.empty();
11700  }
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
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 11653 of file json.hpp.

11654  {
11655  return to_string();
11656  }
std::string to_string() const
return a string representation of the JSON pointer
Definition: json.hpp:11642
template<typename BasicJsonType >
std::string nlohmann::json_pointer< BasicJsonType >::pop_back ( )
inlineprivate

remove and return last reference pointer

Exceptions
out_of_range.405if JSON pointer has no parent

Definition at line 11684 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, and JSON_UNLIKELY.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::patch().

11685  {
11686  if (JSON_UNLIKELY(is_root()))
11687  {
11688  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
11689  }
11690 
11691  auto last = reference_tokens.back();
11692  reference_tokens.pop_back();
11693  return last;
11694  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool is_root() const noexcept
return whether pointer points to the root document
Definition: json.hpp:11697
#define JSON_THROW(exception)
Definition: json.hpp:162
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
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 12138 of file json.hpp.

12140  {
12141  assert(not f.empty());
12142  for (auto pos = s.find(f); // find first occurrence of f
12143  pos != std::string::npos; // make sure f was found
12144  s.replace(pos, f.size(), t), // replace with t, and
12145  pos = s.find(f, pos + t.size())) // find next occurrence of f
12146  {}
12147  }
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 12063 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_THROW, and JSON_UNLIKELY.

12064  {
12065  std::vector<std::string> result;
12066 
12067  // special case: empty reference string -> no reference tokens
12068  if (reference_string.empty())
12069  {
12070  return result;
12071  }
12072 
12073  // check if nonempty reference string begins with slash
12074  if (JSON_UNLIKELY(reference_string[0] != '/'))
12075  {
12077  "JSON pointer must be empty or begin with '/' - was: '" +
12078  reference_string + "'"));
12079  }
12080 
12081  // extract the reference tokens:
12082  // - slash: position of the last read slash (or end of string)
12083  // - start: position after the previous slash
12084  for (
12085  // search for the first slash after the first character
12086  std::size_t slash = reference_string.find_first_of('/', 1),
12087  // set the beginning of the first reference token
12088  start = 1;
12089  // we can stop if start == 0 (if slash == std::string::npos)
12090  start != 0;
12091  // set the beginning of the next reference token
12092  // (will eventually be 0 if slash == std::string::npos)
12093  start = (slash == std::string::npos) ? 0 : slash + 1,
12094  // find next slash
12095  slash = reference_string.find_first_of('/', start))
12096  {
12097  // use the text between the beginning of the reference token
12098  // (start) and the last slash (slash).
12099  auto reference_token = reference_string.substr(start, slash - start);
12100 
12101  // check reference tokens are properly escaped
12102  for (std::size_t pos = reference_token.find_first_of('~');
12103  pos != std::string::npos;
12104  pos = reference_token.find_first_of('~', pos + 1))
12105  {
12106  assert(reference_token[pos] == '~');
12107 
12108  // ~ must be followed by 0 or 1
12109  if (JSON_UNLIKELY(pos == reference_token.size() - 1 or
12110  (reference_token[pos + 1] != '0' and
12111  reference_token[pos + 1] != '1')))
12112  {
12113  JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'"));
12114  }
12115  }
12116 
12117  // finally, store the reference token
12118  unescape(reference_token);
12119  result.push_back(reference_token);
12120  }
12121 
12122  return result;
12123  }
static void unescape(std::string &s)
unescape "~1" to tilde and "~0" to slash (order is important!)
Definition: json.hpp:12158
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
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 11642 of file json.hpp.

References nlohmann::detail::string.

11643  {
11644  return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
11645  std::string{},
11646  [](const std::string & a, const std::string & b)
11647  {
11648  return a + "/" + escape(b);
11649  });
11650  }
static std::string escape(std::string s)
escape "~" to "~0" and "/" to "~1"
Definition: json.hpp:12150
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
template<typename BasicJsonType >
json_pointer nlohmann::json_pointer< BasicJsonType >::top ( ) const
inlineprivate

Definition at line 11702 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, JSON_UNLIKELY, and nlohmann::json_pointer< BasicJsonType >::reference_tokens.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::patch().

11703  {
11704  if (JSON_UNLIKELY(is_root()))
11705  {
11706  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
11707  }
11708 
11709  json_pointer result = *this;
11710  result.reference_tokens = {reference_tokens[0]};
11711  return result;
11712  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:11623
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool is_root() const noexcept
return whether pointer points to the root document
Definition: json.hpp:11697
#define JSON_THROW(exception)
Definition: json.hpp:162
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::vector< std::string > reference_tokens
the reference tokens
Definition: json.hpp:12274
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 12158 of file json.hpp.

12159  {
12160  replace_substring(s, "~1", "/");
12161  replace_substring(s, "~0", "~");
12162  }
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:12138
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 12234 of file json.hpp.

References nlohmann::detail::type_error::create(), nlohmann::json_pointer< BasicJsonType >::get_and_create(), JSON_THROW, and JSON_UNLIKELY.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::unflatten().

12235  {
12236  if (JSON_UNLIKELY(not value.is_object()))
12237  {
12238  JSON_THROW(detail::type_error::create(314, "only objects can be unflattened"));
12239  }
12240 
12241  BasicJsonType result;
12242 
12243  // iterate the JSON object values
12244  for (const auto& element : *value.m_value.object)
12245  {
12246  if (JSON_UNLIKELY(not element.second.is_primitive()))
12247  {
12248  JSON_THROW(detail::type_error::create(315, "values in object must be primitive"));
12249  }
12250 
12251  // assign value to reference pointed to by JSON pointer; Note that if
12252  // the JSON pointer is "" (i.e., points to the whole value), function
12253  // get_and_create returns a reference to result itself. An assignment
12254  // will then create a primitive value.
12255  json_pointer(element.first).get_and_create(result) = element.second;
12256  }
12257 
12258  return result;
12259  }
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:11623
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
#define JSON_THROW(exception)
Definition: json.hpp:162
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:1024

Friends And Related Function Documentation

template<typename BasicJsonType >
template<template< typename, typename, typename... > class ObjectType, template< typename, typename... > class ArrayType, class StringType , class BooleanType , class NumberIntegerType , class NumberUnsignedType , class NumberFloatType , template< typename > class AllocatorType, template< typename, typename=void > class JSONSerializer>
friend class basic_json
friend

Definition at line 11599 of file json.hpp.

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

Definition at line 12267 of file json.hpp.

12269  {
12270  return not (lhs == rhs);
12271  }
template<typename BasicJsonType >
bool operator== ( json_pointer< BasicJsonType > const &  lhs,
json_pointer< BasicJsonType > const &  rhs 
)
friend

Definition at line 12261 of file json.hpp.

12263  {
12264  return (lhs.reference_tokens == rhs.reference_tokens);
12265  }

Member Data Documentation

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

the reference tokens

Definition at line 12274 of file json.hpp.

Referenced by nlohmann::json_pointer< BasicJsonType >::top().


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