| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <algorithm> // all_of |
| 4 |
#include <cctype> // isdigit |
| 5 |
#include <limits> // max |
| 6 |
#include <numeric> // accumulate |
| 7 |
#include <string> // string |
| 8 |
#include <utility> // move |
| 9 |
#include <vector> // vector |
| 10 |
|
| 11 |
#include <nlohmann/detail/exceptions.hpp> |
| 12 |
#include <nlohmann/detail/macro_scope.hpp> |
| 13 |
#include <nlohmann/detail/string_escape.hpp> |
| 14 |
#include <nlohmann/detail/value_t.hpp> |
| 15 |
|
| 16 |
namespace nlohmann |
| 17 |
{ |
| 18 |
template<typename BasicJsonType> |
| 19 |
class json_pointer |
| 20 |
{ |
| 21 |
// allow basic_json to access private members |
| 22 |
NLOHMANN_BASIC_JSON_TPL_DECLARATION |
| 23 |
friend class basic_json; |
| 24 |
|
| 25 |
public: |
| 26 |
/*! |
| 27 |
@brief create JSON pointer |
| 28 |
|
| 29 |
Create a JSON pointer according to the syntax described in |
| 30 |
[Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). |
| 31 |
|
| 32 |
@param[in] s string representing the JSON pointer; if omitted, the empty |
| 33 |
string is assumed which references the whole JSON value |
| 34 |
|
| 35 |
@throw parse_error.107 if the given JSON pointer @a s is nonempty and does |
| 36 |
not begin with a slash (`/`); see example below |
| 37 |
|
| 38 |
@throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is |
| 39 |
not followed by `0` (representing `~`) or `1` (representing `/`); see |
| 40 |
example below |
| 41 |
|
| 42 |
@liveexample{The example shows the construction several valid JSON pointers |
| 43 |
as well as the exceptional behavior.,json_pointer} |
| 44 |
|
| 45 |
@since version 2.0.0 |
| 46 |
*/ |
| 47 |
explicit json_pointer(const std::string& s = "") |
| 48 |
: reference_tokens(split(s)) |
| 49 |
{} |
| 50 |
|
| 51 |
/*! |
| 52 |
@brief return a string representation of the JSON pointer |
| 53 |
|
| 54 |
@invariant For each JSON pointer `ptr`, it holds: |
| 55 |
@code {.cpp} |
| 56 |
ptr == json_pointer(ptr.to_string()); |
| 57 |
@endcode |
| 58 |
|
| 59 |
@return a string representation of the JSON pointer |
| 60 |
|
| 61 |
@liveexample{The example shows the result of `to_string`.,json_pointer__to_string} |
| 62 |
|
| 63 |
@since version 2.0.0 |
| 64 |
*/ |
| 65 |
std::string to_string() const |
| 66 |
{ |
| 67 |
return std::accumulate(reference_tokens.begin(), reference_tokens.end(), |
| 68 |
std::string{}, |
| 69 |
[](const std::string & a, const std::string & b) |
| 70 |
{ |
| 71 |
return a + "/" + detail::escape(b); |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
/// @copydoc to_string() |
| 76 |
operator std::string() const |
| 77 |
{ |
| 78 |
return to_string(); |
| 79 |
} |
| 80 |
|
| 81 |
/*! |
| 82 |
@brief append another JSON pointer at the end of this JSON pointer |
| 83 |
|
| 84 |
@param[in] ptr JSON pointer to append |
| 85 |
@return JSON pointer with @a ptr appended |
| 86 |
|
| 87 |
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} |
| 88 |
|
| 89 |
@complexity Linear in the length of @a ptr. |
| 90 |
|
| 91 |
@sa see @ref operator/=(std::string) to append a reference token |
| 92 |
@sa see @ref operator/=(std::size_t) to append an array index |
| 93 |
@sa see @ref operator/(const json_pointer&, const json_pointer&) for a binary operator |
| 94 |
|
| 95 |
@since version 3.6.0 |
| 96 |
*/ |
| 97 |
json_pointer& operator/=(const json_pointer& ptr) |
| 98 |
{ |
| 99 |
reference_tokens.insert(reference_tokens.end(), |
| 100 |
ptr.reference_tokens.begin(), |
| 101 |
ptr.reference_tokens.end()); |
| 102 |
return *this; |
| 103 |
} |
| 104 |
|
| 105 |
/*! |
| 106 |
@brief append an unescaped reference token at the end of this JSON pointer |
| 107 |
|
| 108 |
@param[in] token reference token to append |
| 109 |
@return JSON pointer with @a token appended without escaping @a token |
| 110 |
|
| 111 |
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} |
| 112 |
|
| 113 |
@complexity Amortized constant. |
| 114 |
|
| 115 |
@sa see @ref operator/=(const json_pointer&) to append a JSON pointer |
| 116 |
@sa see @ref operator/=(std::size_t) to append an array index |
| 117 |
@sa see @ref operator/(const json_pointer&, std::size_t) for a binary operator |
| 118 |
|
| 119 |
@since version 3.6.0 |
| 120 |
*/ |
| 121 |
json_pointer& operator/=(std::string token) |
| 122 |
{ |
| 123 |
push_back(std::move(token)); |
| 124 |
return *this; |
| 125 |
} |
| 126 |
|
| 127 |
/*! |
| 128 |
@brief append an array index at the end of this JSON pointer |
| 129 |
|
| 130 |
@param[in] array_idx array index to append |
| 131 |
@return JSON pointer with @a array_idx appended |
| 132 |
|
| 133 |
@liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} |
| 134 |
|
| 135 |
@complexity Amortized constant. |
| 136 |
|
| 137 |
@sa see @ref operator/=(const json_pointer&) to append a JSON pointer |
| 138 |
@sa see @ref operator/=(std::string) to append a reference token |
| 139 |
@sa see @ref operator/(const json_pointer&, std::string) for a binary operator |
| 140 |
|
| 141 |
@since version 3.6.0 |
| 142 |
*/ |
| 143 |
json_pointer& operator/=(std::size_t array_idx) |
| 144 |
{ |
| 145 |
return *this /= std::to_string(array_idx); |
| 146 |
} |
| 147 |
|
| 148 |
/*! |
| 149 |
@brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer |
| 150 |
|
| 151 |
@param[in] lhs JSON pointer |
| 152 |
@param[in] rhs JSON pointer |
| 153 |
@return a new JSON pointer with @a rhs appended to @a lhs |
| 154 |
|
| 155 |
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} |
| 156 |
|
| 157 |
@complexity Linear in the length of @a lhs and @a rhs. |
| 158 |
|
| 159 |
@sa see @ref operator/=(const json_pointer&) to append a JSON pointer |
| 160 |
|
| 161 |
@since version 3.6.0 |
| 162 |
*/ |
| 163 |
friend json_pointer operator/(const json_pointer& lhs, |
| 164 |
const json_pointer& rhs) |
| 165 |
{ |
| 166 |
return json_pointer(lhs) /= rhs; |
| 167 |
} |
| 168 |
|
| 169 |
/*! |
| 170 |
@brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer |
| 171 |
|
| 172 |
@param[in] ptr JSON pointer |
| 173 |
@param[in] token reference token |
| 174 |
@return a new JSON pointer with unescaped @a token appended to @a ptr |
| 175 |
|
| 176 |
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} |
| 177 |
|
| 178 |
@complexity Linear in the length of @a ptr. |
| 179 |
|
| 180 |
@sa see @ref operator/=(std::string) to append a reference token |
| 181 |
|
| 182 |
@since version 3.6.0 |
| 183 |
*/ |
| 184 |
friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param) |
| 185 |
{ |
| 186 |
return json_pointer(ptr) /= std::move(token); |
| 187 |
} |
| 188 |
|
| 189 |
/*! |
| 190 |
@brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer |
| 191 |
|
| 192 |
@param[in] ptr JSON pointer |
| 193 |
@param[in] array_idx array index |
| 194 |
@return a new JSON pointer with @a array_idx appended to @a ptr |
| 195 |
|
| 196 |
@liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} |
| 197 |
|
| 198 |
@complexity Linear in the length of @a ptr. |
| 199 |
|
| 200 |
@sa see @ref operator/=(std::size_t) to append an array index |
| 201 |
|
| 202 |
@since version 3.6.0 |
| 203 |
*/ |
| 204 |
friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx) |
| 205 |
{ |
| 206 |
return json_pointer(ptr) /= array_idx; |
| 207 |
} |
| 208 |
|
| 209 |
/*! |
| 210 |
@brief returns the parent of this JSON pointer |
| 211 |
|
| 212 |
@return parent of this JSON pointer; in case this JSON pointer is the root, |
| 213 |
the root itself is returned |
| 214 |
|
| 215 |
@complexity Linear in the length of the JSON pointer. |
| 216 |
|
| 217 |
@liveexample{The example shows the result of `parent_pointer` for different |
| 218 |
JSON Pointers.,json_pointer__parent_pointer} |
| 219 |
|
| 220 |
@since version 3.6.0 |
| 221 |
*/ |
| 222 |
json_pointer parent_pointer() const |
| 223 |
{ |
| 224 |
if (empty()) |
| 225 |
{ |
| 226 |
return *this; |
| 227 |
} |
| 228 |
|
| 229 |
json_pointer res = *this; |
| 230 |
res.pop_back(); |
| 231 |
return res; |
| 232 |
} |
| 233 |
|
| 234 |
/*! |
| 235 |
@brief remove last reference token |
| 236 |
|
| 237 |
@pre not `empty()` |
| 238 |
|
| 239 |
@liveexample{The example shows the usage of `pop_back`.,json_pointer__pop_back} |
| 240 |
|
| 241 |
@complexity Constant. |
| 242 |
|
| 243 |
@throw out_of_range.405 if JSON pointer has no parent |
| 244 |
|
| 245 |
@since version 3.6.0 |
| 246 |
*/ |
| 247 |
void pop_back() |
| 248 |
{ |
| 249 |
if (JSON_HEDLEY_UNLIKELY(empty())) |
| 250 |
{ |
| 251 |
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); |
| 252 |
} |
| 253 |
|
| 254 |
reference_tokens.pop_back(); |
| 255 |
} |
| 256 |
|
| 257 |
/*! |
| 258 |
@brief return last reference token |
| 259 |
|
| 260 |
@pre not `empty()` |
| 261 |
@return last reference token |
| 262 |
|
| 263 |
@liveexample{The example shows the usage of `back`.,json_pointer__back} |
| 264 |
|
| 265 |
@complexity Constant. |
| 266 |
|
| 267 |
@throw out_of_range.405 if JSON pointer has no parent |
| 268 |
|
| 269 |
@since version 3.6.0 |
| 270 |
*/ |
| 271 |
const std::string& back() const |
| 272 |
{ |
| 273 |
if (JSON_HEDLEY_UNLIKELY(empty())) |
| 274 |
{ |
| 275 |
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); |
| 276 |
} |
| 277 |
|
| 278 |
return reference_tokens.back(); |
| 279 |
} |
| 280 |
|
| 281 |
/*! |
| 282 |
@brief append an unescaped token at the end of the reference pointer |
| 283 |
|
| 284 |
@param[in] token token to add |
| 285 |
|
| 286 |
@complexity Amortized constant. |
| 287 |
|
| 288 |
@liveexample{The example shows the result of `push_back` for different |
| 289 |
JSON Pointers.,json_pointer__push_back} |
| 290 |
|
| 291 |
@since version 3.6.0 |
| 292 |
*/ |
| 293 |
void push_back(const std::string& token) |
| 294 |
{ |
| 295 |
reference_tokens.push_back(token); |
| 296 |
} |
| 297 |
|
| 298 |
/// @copydoc push_back(const std::string&) |
| 299 |
void push_back(std::string&& token) |
| 300 |
{ |
| 301 |
reference_tokens.push_back(std::move(token)); |
| 302 |
} |
| 303 |
|
| 304 |
/*! |
| 305 |
@brief return whether pointer points to the root document |
| 306 |
|
| 307 |
@return true iff the JSON pointer points to the root document |
| 308 |
|
| 309 |
@complexity Constant. |
| 310 |
|
| 311 |
@exceptionsafety No-throw guarantee: this function never throws exceptions. |
| 312 |
|
| 313 |
@liveexample{The example shows the result of `empty` for different JSON |
| 314 |
Pointers.,json_pointer__empty} |
| 315 |
|
| 316 |
@since version 3.6.0 |
| 317 |
*/ |
| 318 |
bool empty() const noexcept |
| 319 |
{ |
| 320 |
return reference_tokens.empty(); |
| 321 |
} |
| 322 |
|
| 323 |
private: |
| 324 |
/*! |
| 325 |
@param[in] s reference token to be converted into an array index |
| 326 |
|
| 327 |
@return integer representation of @a s |
| 328 |
|
| 329 |
@throw parse_error.106 if an array index begins with '0' |
| 330 |
@throw parse_error.109 if an array index begins not with a digit |
| 331 |
@throw out_of_range.404 if string @a s could not be converted to an integer |
| 332 |
@throw out_of_range.410 if an array index exceeds size_type |
| 333 |
*/ |
| 334 |
static typename BasicJsonType::size_type array_index(const std::string& s) |
| 335 |
{ |
| 336 |
using size_type = typename BasicJsonType::size_type; |
| 337 |
|
| 338 |
// error condition (cf. RFC 6901, Sect. 4) |
| 339 |
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) |
| 340 |
{ |
| 341 |
JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType())); |
| 342 |
} |
| 343 |
|
| 344 |
// error condition (cf. RFC 6901, Sect. 4) |
| 345 |
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) |
| 346 |
{ |
| 347 |
JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType())); |
| 348 |
} |
| 349 |
|
| 350 |
std::size_t processed_chars = 0; |
| 351 |
unsigned long long res = 0; // NOLINT(runtime/int) |
| 352 |
JSON_TRY |
| 353 |
{ |
| 354 |
res = std::stoull(s, &processed_chars); |
| 355 |
} |
| 356 |
JSON_CATCH(std::out_of_range&) |
| 357 |
{ |
| 358 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); |
| 359 |
} |
| 360 |
|
| 361 |
// check if the string was completely read |
| 362 |
if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) |
| 363 |
{ |
| 364 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); |
| 365 |
} |
| 366 |
|
| 367 |
// only triggered on special platforms (like 32bit), see also |
| 368 |
// https://github.com/nlohmann/json/pull/2203 |
| 369 |
if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int) |
| 370 |
{ |
| 371 |
JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE |
| 372 |
} |
| 373 |
|
| 374 |
return static_cast<size_type>(res); |
| 375 |
} |
| 376 |
|
| 377 |
JSON_PRIVATE_UNLESS_TESTED: |
| 378 |
json_pointer top() const |
| 379 |
{ |
| 380 |
if (JSON_HEDLEY_UNLIKELY(empty())) |
| 381 |
{ |
| 382 |
JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); |
| 383 |
} |
| 384 |
|
| 385 |
json_pointer result = *this; |
| 386 |
result.reference_tokens = {reference_tokens[0]}; |
| 387 |
return result; |
| 388 |
} |
| 389 |
|
| 390 |
private: |
| 391 |
/*! |
| 392 |
@brief create and return a reference to the pointed to value |
| 393 |
|
| 394 |
@complexity Linear in the number of reference tokens. |
| 395 |
|
| 396 |
@throw parse_error.109 if array index is not a number |
| 397 |
@throw type_error.313 if value cannot be unflattened |
| 398 |
*/ |
| 399 |
BasicJsonType& get_and_create(BasicJsonType& j) const |
| 400 |
{ |
| 401 |
auto* result = &j; |
| 402 |
|
| 403 |
// in case no reference tokens exist, return a reference to the JSON value |
| 404 |
// j which will be overwritten by a primitive value |
| 405 |
for (const auto& reference_token : reference_tokens) |
| 406 |
{ |
| 407 |
switch (result->type()) |
| 408 |
{ |
| 409 |
case detail::value_t::null: |
| 410 |
{ |
| 411 |
if (reference_token == "0") |
| 412 |
{ |
| 413 |
// start a new array if reference token is 0 |
| 414 |
result = &result->operator[](0); |
| 415 |
} |
| 416 |
else |
| 417 |
{ |
| 418 |
// start a new object otherwise |
| 419 |
result = &result->operator[](reference_token); |
| 420 |
} |
| 421 |
break; |
| 422 |
} |
| 423 |
|
| 424 |
case detail::value_t::object: |
| 425 |
{ |
| 426 |
// create an entry in the object |
| 427 |
result = &result->operator[](reference_token); |
| 428 |
break; |
| 429 |
} |
| 430 |
|
| 431 |
case detail::value_t::array: |
| 432 |
{ |
| 433 |
// create an entry in the array |
| 434 |
result = &result->operator[](array_index(reference_token)); |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
The following code is only reached if there exists a reference |
| 440 |
token _and_ the current value is primitive. In this case, we have |
| 441 |
an error situation, because primitive values may only occur as |
| 442 |
single value; that is, with an empty list of reference tokens. |
| 443 |
*/ |
| 444 |
case detail::value_t::string: |
| 445 |
case detail::value_t::boolean: |
| 446 |
case detail::value_t::number_integer: |
| 447 |
case detail::value_t::number_unsigned: |
| 448 |
case detail::value_t::number_float: |
| 449 |
case detail::value_t::binary: |
| 450 |
case detail::value_t::discarded: |
| 451 |
default: |
| 452 |
JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j)); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
return *result; |
| 457 |
} |
| 458 |
|
| 459 |
/*! |
| 460 |
@brief return a reference to the pointed to value |
| 461 |
|
| 462 |
@note This version does not throw if a value is not present, but tries to |
| 463 |
create nested values instead. For instance, calling this function |
| 464 |
with pointer `"/this/that"` on a null value is equivalent to calling |
| 465 |
`operator[]("this").operator[]("that")` on that value, effectively |
| 466 |
changing the null value to an object. |
| 467 |
|
| 468 |
@param[in] ptr a JSON value |
| 469 |
|
| 470 |
@return reference to the JSON value pointed to by the JSON pointer |
| 471 |
|
| 472 |
@complexity Linear in the length of the JSON pointer. |
| 473 |
|
| 474 |
@throw parse_error.106 if an array index begins with '0' |
| 475 |
@throw parse_error.109 if an array index was not a number |
| 476 |
@throw out_of_range.404 if the JSON pointer can not be resolved |
| 477 |
*/ |
| 478 |
BasicJsonType& get_unchecked(BasicJsonType* ptr) const |
| 479 |
{ |
| 480 |
for (const auto& reference_token : reference_tokens) |
| 481 |
{ |
| 482 |
// convert null values to arrays or objects before continuing |
| 483 |
if (ptr->is_null()) |
| 484 |
{ |
| 485 |
// check if reference token is a number |
| 486 |
const bool nums = |
| 487 |
std::all_of(reference_token.begin(), reference_token.end(), |
| 488 |
[](const unsigned char x) |
| 489 |
{ |
| 490 |
return std::isdigit(x); |
| 491 |
}); |
| 492 |
|
| 493 |
// change value to array for numbers or "-" or to object otherwise |
| 494 |
*ptr = (nums || reference_token == "-") |
| 495 |
? detail::value_t::array |
| 496 |
: detail::value_t::object; |
| 497 |
} |
| 498 |
|
| 499 |
switch (ptr->type()) |
| 500 |
{ |
| 501 |
case detail::value_t::object: |
| 502 |
{ |
| 503 |
// use unchecked object access |
| 504 |
ptr = &ptr->operator[](reference_token); |
| 505 |
break; |
| 506 |
} |
| 507 |
|
| 508 |
case detail::value_t::array: |
| 509 |
{ |
| 510 |
if (reference_token == "-") |
| 511 |
{ |
| 512 |
// explicitly treat "-" as index beyond the end |
| 513 |
ptr = &ptr->operator[](ptr->m_value.array->size()); |
| 514 |
} |
| 515 |
else |
| 516 |
{ |
| 517 |
// convert array index to number; unchecked access |
| 518 |
ptr = &ptr->operator[](array_index(reference_token)); |
| 519 |
} |
| 520 |
break; |
| 521 |
} |
| 522 |
|
| 523 |
case detail::value_t::null: |
| 524 |
case detail::value_t::string: |
| 525 |
case detail::value_t::boolean: |
| 526 |
case detail::value_t::number_integer: |
| 527 |
case detail::value_t::number_unsigned: |
| 528 |
case detail::value_t::number_float: |
| 529 |
case detail::value_t::binary: |
| 530 |
case detail::value_t::discarded: |
| 531 |
default: |
| 532 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
return *ptr; |
| 537 |
} |
| 538 |
|
| 539 |
/*! |
| 540 |
@throw parse_error.106 if an array index begins with '0' |
| 541 |
@throw parse_error.109 if an array index was not a number |
| 542 |
@throw out_of_range.402 if the array index '-' is used |
| 543 |
@throw out_of_range.404 if the JSON pointer can not be resolved |
| 544 |
*/ |
| 545 |
BasicJsonType& get_checked(BasicJsonType* ptr) const |
| 546 |
{ |
| 547 |
for (const auto& reference_token : reference_tokens) |
| 548 |
{ |
| 549 |
switch (ptr->type()) |
| 550 |
{ |
| 551 |
case detail::value_t::object: |
| 552 |
{ |
| 553 |
// note: at performs range check |
| 554 |
ptr = &ptr->at(reference_token); |
| 555 |
break; |
| 556 |
} |
| 557 |
|
| 558 |
case detail::value_t::array: |
| 559 |
{ |
| 560 |
if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) |
| 561 |
{ |
| 562 |
// "-" always fails the range check |
| 563 |
JSON_THROW(detail::out_of_range::create(402, |
| 564 |
"array index '-' (" + std::to_string(ptr->m_value.array->size()) + |
| 565 |
") is out of range", *ptr)); |
| 566 |
} |
| 567 |
|
| 568 |
// note: at performs range check |
| 569 |
ptr = &ptr->at(array_index(reference_token)); |
| 570 |
break; |
| 571 |
} |
| 572 |
|
| 573 |
case detail::value_t::null: |
| 574 |
case detail::value_t::string: |
| 575 |
case detail::value_t::boolean: |
| 576 |
case detail::value_t::number_integer: |
| 577 |
case detail::value_t::number_unsigned: |
| 578 |
case detail::value_t::number_float: |
| 579 |
case detail::value_t::binary: |
| 580 |
case detail::value_t::discarded: |
| 581 |
default: |
| 582 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
return *ptr; |
| 587 |
} |
| 588 |
|
| 589 |
/*! |
| 590 |
@brief return a const reference to the pointed to value |
| 591 |
|
| 592 |
@param[in] ptr a JSON value |
| 593 |
|
| 594 |
@return const reference to the JSON value pointed to by the JSON |
| 595 |
pointer |
| 596 |
|
| 597 |
@throw parse_error.106 if an array index begins with '0' |
| 598 |
@throw parse_error.109 if an array index was not a number |
| 599 |
@throw out_of_range.402 if the array index '-' is used |
| 600 |
@throw out_of_range.404 if the JSON pointer can not be resolved |
| 601 |
*/ |
| 602 |
const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const |
| 603 |
{ |
| 604 |
for (const auto& reference_token : reference_tokens) |
| 605 |
{ |
| 606 |
switch (ptr->type()) |
| 607 |
{ |
| 608 |
case detail::value_t::object: |
| 609 |
{ |
| 610 |
// use unchecked object access |
| 611 |
ptr = &ptr->operator[](reference_token); |
| 612 |
break; |
| 613 |
} |
| 614 |
|
| 615 |
case detail::value_t::array: |
| 616 |
{ |
| 617 |
if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) |
| 618 |
{ |
| 619 |
// "-" cannot be used for const access |
| 620 |
JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr)); |
| 621 |
} |
| 622 |
|
| 623 |
// use unchecked array access |
| 624 |
ptr = &ptr->operator[](array_index(reference_token)); |
| 625 |
break; |
| 626 |
} |
| 627 |
|
| 628 |
case detail::value_t::null: |
| 629 |
case detail::value_t::string: |
| 630 |
case detail::value_t::boolean: |
| 631 |
case detail::value_t::number_integer: |
| 632 |
case detail::value_t::number_unsigned: |
| 633 |
case detail::value_t::number_float: |
| 634 |
case detail::value_t::binary: |
| 635 |
case detail::value_t::discarded: |
| 636 |
default: |
| 637 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
return *ptr; |
| 642 |
} |
| 643 |
|
| 644 |
/*! |
| 645 |
@throw parse_error.106 if an array index begins with '0' |
| 646 |
@throw parse_error.109 if an array index was not a number |
| 647 |
@throw out_of_range.402 if the array index '-' is used |
| 648 |
@throw out_of_range.404 if the JSON pointer can not be resolved |
| 649 |
*/ |
| 650 |
const BasicJsonType& get_checked(const BasicJsonType* ptr) const |
| 651 |
{ |
| 652 |
for (const auto& reference_token : reference_tokens) |
| 653 |
{ |
| 654 |
switch (ptr->type()) |
| 655 |
{ |
| 656 |
case detail::value_t::object: |
| 657 |
{ |
| 658 |
// note: at performs range check |
| 659 |
ptr = &ptr->at(reference_token); |
| 660 |
break; |
| 661 |
} |
| 662 |
|
| 663 |
case detail::value_t::array: |
| 664 |
{ |
| 665 |
if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) |
| 666 |
{ |
| 667 |
// "-" always fails the range check |
| 668 |
JSON_THROW(detail::out_of_range::create(402, |
| 669 |
"array index '-' (" + std::to_string(ptr->m_value.array->size()) + |
| 670 |
") is out of range", *ptr)); |
| 671 |
} |
| 672 |
|
| 673 |
// note: at performs range check |
| 674 |
ptr = &ptr->at(array_index(reference_token)); |
| 675 |
break; |
| 676 |
} |
| 677 |
|
| 678 |
case detail::value_t::null: |
| 679 |
case detail::value_t::string: |
| 680 |
case detail::value_t::boolean: |
| 681 |
case detail::value_t::number_integer: |
| 682 |
case detail::value_t::number_unsigned: |
| 683 |
case detail::value_t::number_float: |
| 684 |
case detail::value_t::binary: |
| 685 |
case detail::value_t::discarded: |
| 686 |
default: |
| 687 |
JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
return *ptr; |
| 692 |
} |
| 693 |
|
| 694 |
/*! |
| 695 |
@throw parse_error.106 if an array index begins with '0' |
| 696 |
@throw parse_error.109 if an array index was not a number |
| 697 |
*/ |
| 698 |
bool contains(const BasicJsonType* ptr) const |
| 699 |
{ |
| 700 |
for (const auto& reference_token : reference_tokens) |
| 701 |
{ |
| 702 |
switch (ptr->type()) |
| 703 |
{ |
| 704 |
case detail::value_t::object: |
| 705 |
{ |
| 706 |
if (!ptr->contains(reference_token)) |
| 707 |
{ |
| 708 |
// we did not find the key in the object |
| 709 |
return false; |
| 710 |
} |
| 711 |
|
| 712 |
ptr = &ptr->operator[](reference_token); |
| 713 |
break; |
| 714 |
} |
| 715 |
|
| 716 |
case detail::value_t::array: |
| 717 |
{ |
| 718 |
if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) |
| 719 |
{ |
| 720 |
// "-" always fails the range check |
| 721 |
return false; |
| 722 |
} |
| 723 |
if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9"))) |
| 724 |
{ |
| 725 |
// invalid char |
| 726 |
return false; |
| 727 |
} |
| 728 |
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1)) |
| 729 |
{ |
| 730 |
if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9'))) |
| 731 |
{ |
| 732 |
// first char should be between '1' and '9' |
| 733 |
return false; |
| 734 |
} |
| 735 |
for (std::size_t i = 1; i < reference_token.size(); i++) |
| 736 |
{ |
| 737 |
if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9'))) |
| 738 |
{ |
| 739 |
// other char should be between '0' and '9' |
| 740 |
return false; |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
const auto idx = array_index(reference_token); |
| 746 |
if (idx >= ptr->size()) |
| 747 |
{ |
| 748 |
// index out of range |
| 749 |
return false; |
| 750 |
} |
| 751 |
|
| 752 |
ptr = &ptr->operator[](idx); |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
case detail::value_t::null: |
| 757 |
case detail::value_t::string: |
| 758 |
case detail::value_t::boolean: |
| 759 |
case detail::value_t::number_integer: |
| 760 |
case detail::value_t::number_unsigned: |
| 761 |
case detail::value_t::number_float: |
| 762 |
case detail::value_t::binary: |
| 763 |
case detail::value_t::discarded: |
| 764 |
default: |
| 765 |
{ |
| 766 |
// we do not expect primitive values if there is still a |
| 767 |
// reference token to process |
| 768 |
return false; |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
// no reference token left means we found a primitive value |
| 774 |
return true; |
| 775 |
} |
| 776 |
|
| 777 |
/*! |
| 778 |
@brief split the string input to reference tokens |
| 779 |
|
| 780 |
@note This function is only called by the json_pointer constructor. |
| 781 |
All exceptions below are documented there. |
| 782 |
|
| 783 |
@throw parse_error.107 if the pointer is not empty or begins with '/' |
| 784 |
@throw parse_error.108 if character '~' is not followed by '0' or '1' |
| 785 |
*/ |
| 786 |
static std::vector<std::string> split(const std::string& reference_string) |
| 787 |
{ |
| 788 |
std::vector<std::string> result; |
| 789 |
|
| 790 |
// special case: empty reference string -> no reference tokens |
| 791 |
if (reference_string.empty()) |
| 792 |
{ |
| 793 |
return result; |
| 794 |
} |
| 795 |
|
| 796 |
// check if nonempty reference string begins with slash |
| 797 |
if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) |
| 798 |
{ |
| 799 |
JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType())); |
| 800 |
} |
| 801 |
|
| 802 |
// extract the reference tokens: |
| 803 |
// - slash: position of the last read slash (or end of string) |
| 804 |
// - start: position after the previous slash |
| 805 |
for ( |
| 806 |
// search for the first slash after the first character |
| 807 |
std::size_t slash = reference_string.find_first_of('/', 1), |
| 808 |
// set the beginning of the first reference token |
| 809 |
start = 1; |
| 810 |
// we can stop if start == 0 (if slash == std::string::npos) |
| 811 |
start != 0; |
| 812 |
// set the beginning of the next reference token |
| 813 |
// (will eventually be 0 if slash == std::string::npos) |
| 814 |
start = (slash == std::string::npos) ? 0 : slash + 1, |
| 815 |
// find next slash |
| 816 |
slash = reference_string.find_first_of('/', start)) |
| 817 |
{ |
| 818 |
// use the text between the beginning of the reference token |
| 819 |
// (start) and the last slash (slash). |
| 820 |
auto reference_token = reference_string.substr(start, slash - start); |
| 821 |
|
| 822 |
// check reference tokens are properly escaped |
| 823 |
for (std::size_t pos = reference_token.find_first_of('~'); |
| 824 |
pos != std::string::npos; |
| 825 |
pos = reference_token.find_first_of('~', pos + 1)) |
| 826 |
{ |
| 827 |
JSON_ASSERT(reference_token[pos] == '~'); |
| 828 |
|
| 829 |
// ~ must be followed by 0 or 1 |
| 830 |
if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || |
| 831 |
(reference_token[pos + 1] != '0' && |
| 832 |
reference_token[pos + 1] != '1'))) |
| 833 |
{ |
| 834 |
JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType())); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
// finally, store the reference token |
| 839 |
detail::unescape(reference_token); |
| 840 |
result.push_back(reference_token); |
| 841 |
} |
| 842 |
|
| 843 |
return result; |
| 844 |
} |
| 845 |
|
| 846 |
private: |
| 847 |
/*! |
| 848 |
@param[in] reference_string the reference string to the current value |
| 849 |
@param[in] value the value to consider |
| 850 |
@param[in,out] result the result object to insert values to |
| 851 |
|
| 852 |
@note Empty objects or arrays are flattened to `null`. |
| 853 |
*/ |
| 854 |
static void flatten(const std::string& reference_string, |
| 855 |
const BasicJsonType& value, |
| 856 |
BasicJsonType& result) |
| 857 |
{ |
| 858 |
switch (value.type()) |
| 859 |
{ |
| 860 |
case detail::value_t::array: |
| 861 |
{ |
| 862 |
if (value.m_value.array->empty()) |
| 863 |
{ |
| 864 |
// flatten empty array as null |
| 865 |
result[reference_string] = nullptr; |
| 866 |
} |
| 867 |
else |
| 868 |
{ |
| 869 |
// iterate array and use index as reference string |
| 870 |
for (std::size_t i = 0; i < value.m_value.array->size(); ++i) |
| 871 |
{ |
| 872 |
flatten(reference_string + "/" + std::to_string(i), |
| 873 |
value.m_value.array->operator[](i), result); |
| 874 |
} |
| 875 |
} |
| 876 |
break; |
| 877 |
} |
| 878 |
|
| 879 |
case detail::value_t::object: |
| 880 |
{ |
| 881 |
if (value.m_value.object->empty()) |
| 882 |
{ |
| 883 |
// flatten empty object as null |
| 884 |
result[reference_string] = nullptr; |
| 885 |
} |
| 886 |
else |
| 887 |
{ |
| 888 |
// iterate object and use keys as reference string |
| 889 |
for (const auto& element : *value.m_value.object) |
| 890 |
{ |
| 891 |
flatten(reference_string + "/" + detail::escape(element.first), element.second, result); |
| 892 |
} |
| 893 |
} |
| 894 |
break; |
| 895 |
} |
| 896 |
|
| 897 |
case detail::value_t::null: |
| 898 |
case detail::value_t::string: |
| 899 |
case detail::value_t::boolean: |
| 900 |
case detail::value_t::number_integer: |
| 901 |
case detail::value_t::number_unsigned: |
| 902 |
case detail::value_t::number_float: |
| 903 |
case detail::value_t::binary: |
| 904 |
case detail::value_t::discarded: |
| 905 |
default: |
| 906 |
{ |
| 907 |
// add primitive value with its reference string |
| 908 |
result[reference_string] = value; |
| 909 |
break; |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
/*! |
| 915 |
@param[in] value flattened JSON |
| 916 |
|
| 917 |
@return unflattened JSON |
| 918 |
|
| 919 |
@throw parse_error.109 if array index is not a number |
| 920 |
@throw type_error.314 if value is not an object |
| 921 |
@throw type_error.315 if object values are not primitive |
| 922 |
@throw type_error.313 if value cannot be unflattened |
| 923 |
*/ |
| 924 |
static BasicJsonType |
| 925 |
unflatten(const BasicJsonType& value) |
| 926 |
{ |
| 927 |
if (JSON_HEDLEY_UNLIKELY(!value.is_object())) |
| 928 |
{ |
| 929 |
JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value)); |
| 930 |
} |
| 931 |
|
| 932 |
BasicJsonType result; |
| 933 |
|
| 934 |
// iterate the JSON object values |
| 935 |
for (const auto& element : *value.m_value.object) |
| 936 |
{ |
| 937 |
if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) |
| 938 |
{ |
| 939 |
JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second)); |
| 940 |
} |
| 941 |
|
| 942 |
// assign value to reference pointed to by JSON pointer; Note that if |
| 943 |
// the JSON pointer is "" (i.e., points to the whole value), function |
| 944 |
// get_and_create returns a reference to result itself. An assignment |
| 945 |
// will then create a primitive value. |
| 946 |
json_pointer(element.first).get_and_create(result) = element.second; |
| 947 |
} |
| 948 |
|
| 949 |
return result; |
| 950 |
} |
| 951 |
|
| 952 |
/*! |
| 953 |
@brief compares two JSON pointers for equality |
| 954 |
|
| 955 |
@param[in] lhs JSON pointer to compare |
| 956 |
@param[in] rhs JSON pointer to compare |
| 957 |
@return whether @a lhs is equal to @a rhs |
| 958 |
|
| 959 |
@complexity Linear in the length of the JSON pointer |
| 960 |
|
| 961 |
@exceptionsafety No-throw guarantee: this function never throws exceptions. |
| 962 |
*/ |
| 963 |
friend bool operator==(json_pointer const& lhs, |
| 964 |
json_pointer const& rhs) noexcept |
| 965 |
{ |
| 966 |
return lhs.reference_tokens == rhs.reference_tokens; |
| 967 |
} |
| 968 |
|
| 969 |
/*! |
| 970 |
@brief compares two JSON pointers for inequality |
| 971 |
|
| 972 |
@param[in] lhs JSON pointer to compare |
| 973 |
@param[in] rhs JSON pointer to compare |
| 974 |
@return whether @a lhs is not equal @a rhs |
| 975 |
|
| 976 |
@complexity Linear in the length of the JSON pointer |
| 977 |
|
| 978 |
@exceptionsafety No-throw guarantee: this function never throws exceptions. |
| 979 |
*/ |
| 980 |
friend bool operator!=(json_pointer const& lhs, |
| 981 |
json_pointer const& rhs) noexcept |
| 982 |
{ |
| 983 |
return !(lhs == rhs); |
| 984 |
} |
| 985 |
|
| 986 |
/// the reference tokens |
| 987 |
std::vector<std::string> reference_tokens; |
| 988 |
}; |
| 989 |
} // namespace nlohmann |