| 12472 | { |
| 12473 | template<typename BasicJsonType> |
| 12474 | class json_pointer |
| 12475 | { |
| 12476 | // allow basic_json to access private members |
| 12477 | NLOHMANN_BASIC_JSON_TPL_DECLARATION |
| 12478 | friend class basic_json; |
| 12479 | |
| 12480 | public: |
| 12481 | /*! |
| 12482 | @brief create JSON pointer |
| 12483 | |
| 12484 | Create a JSON pointer according to the syntax described in |
| 12485 | [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). |
| 12486 | |
| 12487 | @param[in] s string representing the JSON pointer; if omitted, the empty |
| 12488 | string is assumed which references the whole JSON value |
| 12489 | |
| 12490 | @throw parse_error.107 if the given JSON pointer @a s is nonempty and does |
| 12491 | not begin with a slash (`/`); see example below |
| 12492 | |
| 12493 | @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is |
| 12494 | not followed by `0` (representing `~`) or `1` (representing `/`); see |
| 12495 | example below |
| 12496 | |
| 12497 | @liveexample{The example shows the construction several valid JSON pointers |
| 12498 | as well as the exceptional behavior.,json_pointer} |
| 12499 | |
| 12500 | @since version 2.0.0 |
| 12501 | */ |
| 12502 | explicit json_pointer(const std::string& s = "") |
| 12503 | : reference_tokens(split(s)) |
| 12504 | {} |
| 12505 | |
| 12506 | /*! |
| 12507 | @brief return a string representation of the JSON pointer |
| 12508 | |
| 12509 | @invariant For each JSON pointer `ptr`, it holds: |
| 12510 | @code {.cpp} |
| 12511 | ptr == json_pointer(ptr.to_string()); |
| 12512 | @endcode |
| 12513 | |
| 12514 | @return a string representation of the JSON pointer |
| 12515 | |
| 12516 | @liveexample{The example shows the result of `to_string`.,json_pointer__to_string} |
| 12517 | |
| 12518 | @since version 2.0.0 |
| 12519 | */ |
| 12520 | std::string to_string() const |
| 12521 | { |
| 12522 | return std::accumulate(reference_tokens.begin(), reference_tokens.end(), |
| 12523 | std::string{}, |
| 12524 | [](const std::string & a, const std::string & b) |
| 12525 | { |
| 12526 | return a + "/" + detail::escape(b); |
| 12527 | }); |
| 12528 | } |
| 12529 | |
| 12530 | /// @copydoc to_string() |
| 12531 | operator std::string() const |