34template<
typename RefStringType>
45 struct string_t_helper
53 using type = StringType;
58 using string_t =
typename string_t_helper<RefStringType>::type;
63 : reference_tokens(split(s))
70 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
74 return detail::concat(a,
'/', detail::escape(b));
100 reference_tokens.insert(reference_tokens.end(),
101 ptr.reference_tokens.begin(),
102 ptr.reference_tokens.end());
166 reference_tokens.pop_back();
178 return reference_tokens.back();
185 reference_tokens.push_back(token);
192 reference_tokens.push_back(std::move(token));
199 return reference_tokens.empty();
213 template<
typename BasicJsonType>
214 static typename BasicJsonType::size_type array_index(
const string_t& s)
216 using size_type =
typename BasicJsonType::size_type;
230 const char* p = s.c_str();
231 char* p_end =
nullptr;
233 unsigned long long res = std::strtoull(p, &p_end, 10);
248 return static_cast<size_type
>(res);
260 result.reference_tokens = {reference_tokens[0]};
273 template<
typename BasicJsonType>
274 BasicJsonType& get_and_create(BasicJsonType& j)
const
280 for (
const auto& reference_token : reference_tokens)
282 switch (result->type())
286 if (reference_token ==
"0")
289 result = &result->operator[](0);
294 result = &result->operator[](reference_token);
302 result = &result->operator[](reference_token);
309 result = &result->operator[](array_index<BasicJsonType>(reference_token));
353 template<
typename BasicJsonType>
354 BasicJsonType& get_unchecked(BasicJsonType*
ptr)
const
356 for (
const auto& reference_token : reference_tokens)
363 std::all_of(reference_token.begin(), reference_token.end(),
364 [](
const unsigned char x)
366 return std::isdigit(x);
370 *
ptr = (nums || reference_token ==
"-")
380 ptr = &
ptr->operator[](reference_token);
386 if (reference_token ==
"-")
389 ptr = &
ptr->operator[](
ptr->m_value.array->size());
394 ptr = &
ptr->operator[](array_index<BasicJsonType>(reference_token));
421 template<
typename BasicJsonType>
422 BasicJsonType& get_checked(BasicJsonType*
ptr)
const
424 for (
const auto& reference_token : reference_tokens)
431 ptr = &
ptr->at(reference_token);
442 ") is out of range"),
ptr));
446 ptr = &
ptr->at(array_index<BasicJsonType>(reference_token));
479 template<
typename BasicJsonType>
480 const BasicJsonType& get_unchecked(
const BasicJsonType*
ptr)
const
482 for (
const auto& reference_token : reference_tokens)
489 ptr = &
ptr->operator[](reference_token);
502 ptr = &
ptr->operator[](array_index<BasicJsonType>(reference_token));
528 template<
typename BasicJsonType>
529 const BasicJsonType& get_checked(
const BasicJsonType*
ptr)
const
531 for (
const auto& reference_token : reference_tokens)
538 ptr = &
ptr->at(reference_token);
549 ") is out of range"),
ptr));
553 ptr = &
ptr->at(array_index<BasicJsonType>(reference_token));
577 template<
typename BasicJsonType>
578 bool contains(
const BasicJsonType*
ptr)
const
580 for (
const auto& reference_token : reference_tokens)
586 if (!
ptr->contains(reference_token))
592 ptr = &
ptr->operator[](reference_token);
603 if (
JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !(
"0" <= reference_token && reference_token <=
"9")))
615 for (std::size_t i = 1; i < reference_token.size(); i++)
625 const auto idx = array_index<BasicJsonType>(reference_token);
626 if (idx >=
ptr->size())
632 ptr = &
ptr->operator[](idx);
666 static std::vector<string_t> split(
const string_t& reference_string)
668 std::vector<string_t> result;
671 if (reference_string.empty())
687 std::size_t slash = reference_string.find_first_of(
'/', 1),
694 start = (slash == string_t::npos) ? 0 : slash + 1,
696 slash = reference_string.find_first_of(
'/', start))
700 auto reference_token = reference_string.substr(start, slash - start);
703 for (std::size_t pos = reference_token.find_first_of(
'~');
704 pos != string_t::npos;
705 pos = reference_token.find_first_of(
'~', pos + 1))
711 (reference_token[pos + 1] !=
'0' &&
712 reference_token[pos + 1] !=
'1')))
720 result.push_back(reference_token);
734 template<
typename BasicJsonType>
735 static void flatten(
const string_t& reference_string,
736 const BasicJsonType& value,
737 BasicJsonType& result)
739 switch (
value.type())
743 if (
value.m_value.array->empty())
746 result[reference_string] =
nullptr;
751 for (std::size_t i = 0; i <
value.m_value.array->size(); ++i)
754 value.m_value.array->operator[](i), result);
762 if (
value.m_value.object->empty())
765 result[reference_string] =
nullptr;
770 for (
const auto& element : *
value.m_value.object)
789 result[reference_string] =
value;
805 template<
typename BasicJsonType>
807 unflatten(
const BasicJsonType& value)
814 BasicJsonType result;
817 for (
const auto& element : *
value.m_value.object)
828 json_pointer(element.first).get_and_create(result) = element.second;
838 result.reference_tokens = reference_tokens;
845 result.reference_tokens = std::move(reference_tokens);
850#if JSON_HAS_THREE_WAY_COMPARISON
853 template<
typename RefStringTypeRhs>
856 return reference_tokens == rhs.reference_tokens;
868 template<
typename RefStringTypeRhs>
871 return reference_tokens <=> rhs.reference_tokens;
876 template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
883 template<
typename RefStringTypeLhs,
typename StringType>
886 const StringType& rhs);
890 template<
typename RefStringTypeRhs,
typename StringType>
897 template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
904 template<
typename RefStringTypeLhs,
typename StringType>
907 const StringType& rhs);
911 template<
typename RefStringTypeRhs,
typename StringType>
917 template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
925 std::vector<string_t> reference_tokens;
928#if !JSON_HAS_THREE_WAY_COMPARISON
930template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
934 return lhs.reference_tokens == rhs.reference_tokens;
937template<
typename RefStringTypeLhs,
941 const StringType& rhs)
946template<
typename RefStringTypeRhs,
949inline
bool operator==(const StringType& lhs,
955template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
959 return !(lhs == rhs);
962template<
typename RefStringTypeLhs,
966 const StringType& rhs)
968 return !(lhs == rhs);
971template<
typename RefStringTypeRhs,
974inline
bool operator!=(const StringType& lhs,
977 return !(lhs == rhs);
980template<
typename RefStringTypeLhs,
typename RefStringTypeRhs>
984 return lhs.reference_tokens < rhs.reference_tokens;
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
a class to store JSON values
Definition: json.h:96
static out_of_range create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition: exceptions.h:226
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, BasicJsonContext context)
create a parse error exception
Definition: exceptions.h:147
static type_error create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition: exceptions.h:209
JSON Pointer defines a string syntax for identifying a specific value within a JSON document.
Definition: json_pointer.h:36
friend json_pointer operator/(const json_pointer &lhs, string_t token)
create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
Definition: json_pointer.h:131
typename string_t_helper< RefStringType >::type string_t
Definition: json_pointer.h:58
friend json_pointer operator/(const json_pointer &lhs, std::size_t array_idx)
create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
Definition: json_pointer.h:138
json_pointer(const string_t &s="")
create JSON pointer
Definition: json_pointer.h:62
bool empty() const noexcept
return whether pointer points to the root document
Definition: json_pointer.h:197
friend bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
compares two JSON pointers for equality
Definition: json_pointer.h:931
void pop_back()
remove last reference token
Definition: json_pointer.h:159
string_t to_string() const
return a string representation of the JSON pointer
Definition: json_pointer.h:68
json_pointer & operator/=(std::size_t array_idx)
append an array index at the end of this JSON pointer
Definition: json_pointer.h:116
void push_back(string_t &&token)
append an unescaped token at the end of the reference pointer
Definition: json_pointer.h:190
json_pointer & operator/=(const json_pointer &ptr)
append another JSON pointer at the end of this JSON pointer
Definition: json_pointer.h:98
friend 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
Definition: json_pointer.h:123
json_pointer parent_pointer() const
returns the parent of this JSON pointer
Definition: json_pointer.h:145
friend bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
compares two JSON pointers for inequality
Definition: json_pointer.h:956
json_pointer & operator/=(string_t token)
append an unescaped reference token at the end of this JSON pointer
Definition: json_pointer.h:108
friend class json_pointer
Definition: json_pointer.h:42
const string_t & back() const
return last reference token
Definition: json_pointer.h:171
friend std::ostream & operator<<(std::ostream &o, const json_pointer &ptr)
write string representation of the JSON pointer to stream
Definition: json_pointer.h:89
void push_back(const string_t &token)
append an unescaped token at the end of the reference pointer
Definition: json_pointer.h:183
friend bool operator<(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
compares two JSON pointer for less-than
Definition: json_pointer.h:981
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: hedley.h:1396
#define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
Definition: hedley.h:1078
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition: json_pointer.h:931
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition: json_pointer.h:956
bool operator<(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition: json_pointer.h:981
#define WPI_BASIC_JSON_TPL_DECLARATION
Definition: macro_scope.h:236
#define JSON_PRIVATE_UNLESS_TESTED
Definition: macro_scope.h:199
#define JSON_ASSERT(x)
Definition: macro_scope.h:192
#define JSON_THROW(exception)
Definition: macro_scope.h:163
#define WPI_BASIC_JSON_TPL
Definition: macro_scope.h:245
static void unescape(StringType &s)
string unescaping as described in RFC 6901 (Sect. 4)
Definition: string_escape.h:65
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
@ value
the parser finished reading a JSON value
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
type
Definition: core.h:556
StringType escape(StringType s)
string escaping as described in RFC 6901 (Sect. 4)
Definition: string_escape.h:50
std::string to_string(const T &t)
Definition: base.h:92
UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3417