| 11550 | { |
| 11551 | template<typename BasicJsonType> |
| 11552 | class json_pointer |
| 11553 | { |
| 11554 | // allow basic_json to access private members |
| 11555 | NLOHMANN_BASIC_JSON_TPL_DECLARATION |
| 11556 | friend class basic_json; |
| 11557 | |
| 11558 | public: |
| 11559 | /*! |
| 11560 | @brief create JSON pointer |
| 11561 | |
| 11562 | Create a JSON pointer according to the syntax described in |
| 11563 | [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). |
| 11564 | |
| 11565 | @param[in] s string representing the JSON pointer; if omitted, the empty |
| 11566 | string is assumed which references the whole JSON value |
| 11567 | |
| 11568 | @throw parse_error.107 if the given JSON pointer @a s is nonempty and does |
| 11569 | not begin with a slash (`/`); see example below |
| 11570 | |
| 11571 | @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is |
| 11572 | not followed by `0` (representing `~`) or `1` (representing `/`); see |
| 11573 | example below |
| 11574 | |
| 11575 | @liveexample{The example shows the construction several valid JSON pointers |
| 11576 | as well as the exceptional behavior.,json_pointer} |
| 11577 | |
| 11578 | @since version 2.0.0 |
| 11579 | */ |
| 11580 | explicit json_pointer(const std::string& s = "") |
| 11581 | : reference_tokens(split(s)) |
| 11582 | {} |
| 11583 | |
| 11584 | /*! |
| 11585 | @brief return a string representation of the JSON pointer |
| 11586 | |
| 11587 | @invariant For each JSON pointer `ptr`, it holds: |
| 11588 | @code {.cpp} |
| 11589 | ptr == json_pointer(ptr.to_string()); |
| 11590 | @endcode |
| 11591 | |
| 11592 | @return a string representation of the JSON pointer |
| 11593 | |
| 11594 | @liveexample{The example shows the result of `to_string`.,json_pointer__to_string} |
| 11595 | |
| 11596 | @since version 2.0.0 |
| 11597 | */ |
| 11598 | std::string to_string() const |
| 11599 | { |
| 11600 | return std::accumulate(reference_tokens.begin(), reference_tokens.end(), |
| 11601 | std::string{}, |
| 11602 | [](const std::string& a, const std::string& b) |
| 11603 | { |
| 11604 | return a + "/" + escape(b); |
| 11605 | }); |
| 11606 | } |
| 11607 | |
| 11608 | /// @copydoc to_string() |
| 11609 | operator std::string() const |