| 20 | /// @sa https://json.nlohmann.me/api/json_pointer/ |
| 21 | template<typename BasicJsonType> |
| 22 | class json_pointer |
| 23 | { |
| 24 | // allow basic_json to access private members |
| 25 | NLOHMANN_BASIC_JSON_TPL_DECLARATION |
| 26 | friend class basic_json; |
| 27 | |
| 28 | public: |
| 29 | /// @brief create JSON pointer |
| 30 | /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/ |
| 31 | explicit json_pointer(const std::string& s = "") |
| 32 | : reference_tokens(split(s)) |
| 33 | {} |
| 34 | |
| 35 | /// @brief return a string representation of the JSON pointer |
| 36 | /// @sa https://json.nlohmann.me/api/json_pointer/to_string/ |
| 37 | std::string to_string() const |
| 38 | { |
| 39 | return std::accumulate(reference_tokens.begin(), reference_tokens.end(), |
| 40 | std::string{}, |
| 41 | [](const std::string & a, const std::string & b) |
| 42 | { |
| 43 | return a + "/" + detail::escape(b); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | /// @brief return a string representation of the JSON pointer |
| 48 | /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/ |
| 49 | operator std::string() const |
| 50 | { |
| 51 | return to_string(); |
| 52 | } |
| 53 | |
| 54 | /// @brief append another JSON pointer at the end of this JSON pointer |
| 55 | /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ |
| 56 | json_pointer& operator/=(const json_pointer& ptr) |
| 57 | { |
| 58 | reference_tokens.insert(reference_tokens.end(), |
| 59 | ptr.reference_tokens.begin(), |
| 60 | ptr.reference_tokens.end()); |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | /// @brief append an unescaped reference token at the end of this JSON pointer |
| 65 | /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ |
| 66 | json_pointer& operator/=(std::string token) |
| 67 | { |
| 68 | push_back(std::move(token)); |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | /// @brief append an array index at the end of this JSON pointer |
| 73 | /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ |
| 74 | json_pointer& operator/=(std::size_t array_idx) |
| 75 | { |
| 76 | return *this /= std::to_string(array_idx); |
| 77 | } |
| 78 | |
| 79 | /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer |