1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
### jsoncons::jsonpath::basic_json_location
```cpp
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
template <typename CharT,typename Allocator = std::allocator<CharT>> (since 0.172.0)
class basic_json_location
```
Two specializations for common character types are defined:
Type |Definition
----------|------------------------------
json_location |`basic_json_location<char>`
wjson_location |`basic_json_location<wchar_t>`
Objects of type `basic_json_location` represents the location of a specific value in a JSON document.
#### Member types
Type |Definition
------------|------------------------------
char_type | `CharT`
allocator_type | Allocator
string_view_type | `jsoncons::basic_string_view<char_type>`
value_type | basic_path_element<CharT,Allocator>
const_iterator | A constant [LegacyRandomAccessIterator](https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator) with a `value_type` of [basic_path_element<char_type>](basic_path_element.md)
iterator | An alias to `const_iterator`
#### Constructors
basic_json_location(const allocator_type& alloc=Allocator()); (1)
basic_json_location(const basic_path_node<char_type>& path,
const allocator_type& alloc=Allocator()); (2)
basic_json_location(const basic_json_location&); (3)
basic_json_location(basic_json_location&&) noexcept; (4)
(1) Constructs an empty `basic_json_location`.
(2) Constructs a `basic_json_location` from a path node.
(3) Copy constructor
(4) Move constructor
#### operator=
basic_json_location& operator=(const basic_json_location&);
basic_json_location& operator=(basic_json_location&&);
#### Iterators
iterator begin() const;
iterator end() const;
Iterator access to the tokens in the pointer.
#### Accessors
bool empty() const
Checks if the location is empty
std::size_t size() const
const path_element_type& operator[](std::size_t index) const
#### Modifiers
void clear();
basic_json_location& append(const string_view_type& name)
Appends `name` to the location.
template <typename IntegerType>
basic_json_location& append(IntegerType index)
Appends `index` to the location.
This overload only participates in overload resolution if `IntegerType` is an integer type.
basic_json_location& append(const basic_json_location& relative_location)
Appends `relative_location` to the location.
#### Static member functions
static parse(const string_view_type& str);
static parse(const string_view_type& str, std::error_code& ec);
Constructs a `basic_json_location` from a normalized path, or an equivalent representation
using the dot notation.
#### Non-member functions
<table border="0">
<tr>
<td><a href="replace.md">replace</a></td>
<td>Replace a value in a JSON document at a specified location with a new value</td>
</tr>
<tr>
<td><a href="get.md">get</a></td>
<td>Returns a pointer to a JSON value in a JSON document at a specified location</td>
</tr>
<tr>
<td><a href="remove.md">remove</a></td>
<td>Remove a JSON node from a JSON document at a specified location</td>
</tr>
</table>
template <typename CharT,typename Allocator = std::allocator<CharT>>
std::basic_string<CharT, std::char_traits<CharT>, Allocator> to_basic_string(const basic_json_location<CharT,Allocator>& location,
const Allocator& alloc = Allocator())
Returns a normalized path
std::string to_string(const json_location& location)
std::wstring to_wstring(const wjson_location& location)
### Examples
The examples below uses the sample data file `books.json`,
```json
{
"books":
[
{
"category": "fiction",
"title" : "A Wild Sheep Chase",
"author" : "Haruki Murakami",
"price" : 22.72
},
{
"category": "fiction",
"title" : "The Night Watch",
"author" : "Sergei Lukyanenko",
"price" : 23.58
},
{
"category": "fiction",
"title" : "The Comedians",
"author" : "Graham Greene",
"price" : 21.99
},
{
"category": "memoir",
"title" : "The Night Watch",
"author" : "Phillips, David Atlee"
}
]
}
```
#### Get a pointer to the book at index 1
```
jsonpath::json_location loc;
loc.append("store").append("book").append(1);
auto result = jsonpath::get(doc, loc); // since 0.175.0
if (result.second)
{
std::cout << *(result.first) << "\n";
}
```
#### Convert a JSONPath normalized path into a JSONPointer
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <fstream>
using jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
namespace jsonpointer = jsoncons::jsonpointer;
#include <ifstream>
int main()
{
std::ifstream is(/*path_to_books_file*/);
json doc = json::parse(is);
auto expr = jsonpath::make_expression<json>("$.books[?(@.category == 'fiction')]");
std::vector<jsonpath::json_location> locations = expr.select_paths(doc, jsonpath::result_options::sort_descending);
for (const auto& location : locations)
{
std::cout << jsonpath::to_string(location) << "\n";
}
std::cout << "\n";
std::vector<jsoncons::jsonpointer::json_pointer> pointers;
for (const auto& location : locations)
{
jsonpointer::json_pointer ptr;
{
for (const jsonpath::path_element& element : location)
{
if (element.has_name())
{
ptr.append(element.name());
}
else
{
ptr.append(element.index());
}
}
}
pointers.push_back(ptr);
}
for (const auto& ptr : pointers)
{
std::cout << jsonpointer::to_string(ptr) << "\n";
}
std::cout << "\n";
}
```
Output:
```
$['books'][2]
$['books'][1]
$['books'][0]
/books/2
/books/1
/books/0
```
|