Skip to content

Commit 71ed642

Browse files
committed
jsonpointer doc and example update
1 parent 2a1a164 commit 71ed642

File tree

5 files changed

+61
-97
lines changed

5 files changed

+61
-97
lines changed

doc/ref/jsonpointer/add.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ On error, returns a null Json value and a [jsonpointer_errc](jsonpointer_errc.md
2626
2727
### Examples
2828
29-
#### Add a member to an object
29+
#### Add a member to a target location that does not already exist
3030
3131
```c++
3232
#include <jsoncons/json.hpp>
@@ -58,7 +58,6 @@ Output:
5858

5959
#### Add an element to the second position in an array
6060

61-
6261
```c++
6362
#include <jsoncons/json.hpp>
6463
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
@@ -87,7 +86,7 @@ Output:
8786
{"foo":["bar","qux","baz"]}
8887
```
8988

90-
#### Add an element to the end of an array
89+
#### Add a value to the end of an array
9190

9291
```c++
9392
#include <jsoncons/json.hpp>
@@ -117,7 +116,7 @@ Output:
117116
{"foo":["bar","baz","qux"]}
118117
```
119118

120-
#### The specified index must not exceed the size of the array
119+
#### Add an object member to a location that already exists
121120

122121
```c++
123122
#include <jsoncons/json.hpp>
@@ -128,12 +127,12 @@ using namespace jsoncons;
128127
int main()
129128
{
130129
json target = json::parse(R"(
131-
{ "foo": [ "bar", "baz" ] }
130+
{ "foo": "bar", "baz" : "abc"}
132131
)");
133132

134133
try
135134
{
136-
jsonpointer::add(target, "/foo/3", json("qux"));
135+
jsonpointer::add(target, "/baz", json("qux"));
137136
std::cout << target << std::endl;
138137
}
139138
catch (const parse_error& e)
@@ -143,11 +142,11 @@ int main()
143142
}
144143
```
145144
Output:
146-
```
147-
Index exceeds array size at line 1 and column 7
145+
```json
146+
{"baz":"qux","foo":"bar"}
148147
```
149148

150-
#### The specified index must not exceed the size of the array
149+
#### Add a value to a location in an array that exceeds the size of the array
151150

152151
```c++
153152
#include <jsoncons/json.hpp>

doc/ref/jsonpointer/remove.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ On error, returns a null Json value and a [jsonpointer_errc](jsonpointer_errc.md
2626
2727
### Examples
2828
29-
#### Remove an array element
29+
#### Remove an object member
3030
3131
```c++
3232
#include <jsoncons/json.hpp>
@@ -37,12 +37,12 @@ using namespace jsoncons;
3737
int main()
3838
{
3939
json target = json::parse(R"(
40-
{ "foo": [ "bar", "qux", "baz" ] }
40+
{ "foo": "bar", "baz" : "qux"}
4141
)");
4242
4343
try
4444
{
45-
jsonpointer::remove(target, "/foo/1");
45+
jsonpointer::remove(target, "/baz");
4646
std::cout << target << std::endl;
4747
}
4848
catch (const parse_error& e)
@@ -53,7 +53,7 @@ int main()
5353
```
5454
Output:
5555
```json
56-
{"foo":["bar","baz"]}
56+
{"foo":"bar"}
5757
```
5858

5959
#### Remove an array element

doc/ref/jsonpointer/replace.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ On error, returns a null Json value and a [jsonpointer_errc](jsonpointer_errc.md
2626
2727
### Examples
2828
29-
#### Replace a value
29+
#### Replace an object value
3030
3131
```c++
3232
#include <jsoncons/json.hpp>
@@ -62,7 +62,7 @@ Output:
6262
}
6363
```
6464

65-
#### Replace a value
65+
#### Replace an array value
6666

6767
```c++
6868
#include <jsoncons/json.hpp>
@@ -73,13 +73,10 @@ using namespace jsoncons;
7373
int main()
7474
{
7575
json target = json::parse(R"(
76-
{
77-
"baz": "qux",
78-
"foo": "bar"
79-
}
76+
{ "foo": [ "bar", "baz" ] }
8077
)");
8178

82-
auto ec = jsonpointer::try_replace(target, "/baz", json("boo"));
79+
auto ec = jsonpointer::try_replace(target, "/foo/1", json("qux"));
8380
if (ec == jsonpointer::jsonpointer_errc())
8481
{
8582
std::cout << pretty_print(target) << std::endl;
@@ -93,8 +90,7 @@ int main()
9390
Output:
9491
```json
9592
{
96-
"baz": "boo",
97-
"foo": "bar"
93+
"foo": ["bar","qux"]
9894
}
9995
```
10096

examples/src/jsonpointer_examples.cpp

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,12 @@ void jsonpointer_add_member_to_object()
8282
}
8383
}
8484

85-
void jsonpointer_try_add_member_to_object()
86-
{
87-
json target = json::parse(R"(
88-
{ "foo": "bar"}
89-
)");
90-
91-
auto ec = jsonpointer::try_add(target, "/baz", json("qux"));
92-
if (ec == jsonpointer::jsonpointer_errc())
93-
{
94-
std::cout << target << std::endl;
95-
}
96-
else
97-
{
98-
std::cout << make_error_code(ec).message() << std::endl;
99-
}
100-
}
101-
10285
void jsonpointer_add_element_to_array()
10386
{
10487
json target = json::parse(R"(
10588
{ "foo": [ "bar", "baz" ] }
10689
)");
10790

108-
try
109-
{
110-
jsonpointer::add(target, "/foo/1", json("qux"));
111-
std::cout << target << std::endl;
112-
}
113-
catch (const parse_error& e)
114-
{
115-
std::cout << e.what() << std::endl;
116-
}
117-
}
118-
119-
void jsonpointer_try_add_element_to_array()
120-
{
121-
json target = json::parse(R"(
122-
{ "foo": [ "bar", "baz" ] }
123-
)");
124-
12591
auto ec = jsonpointer::try_add(target, "/foo/1", json("qux"));
12692
if (ec == jsonpointer::jsonpointer_errc())
12793
{
@@ -150,32 +116,15 @@ void jsonpointer_add_element_to_end_array()
150116
}
151117
}
152118

153-
void jsonpointer_try_add_element_to_end_array()
119+
void jsonpointer_add_value_name_exists()
154120
{
155121
json target = json::parse(R"(
156-
{ "foo": [ "bar", "baz" ] }
157-
)");
158-
159-
auto ec = jsonpointer::try_add(target, "/foo/-", json("qux"));
160-
if (ec == jsonpointer::jsonpointer_errc())
161-
{
162-
std::cout << target << std::endl;
163-
}
164-
else
165-
{
166-
std::cout << make_error_code(ec).message() << std::endl;
167-
}
168-
}
169-
170-
void jsonpointer_add_element_outside_range()
171-
{
172-
json target = json::parse(R"(
173-
{ "foo": [ "bar", "baz" ] }
122+
{ "foo": "bar", "baz" : "abc"}
174123
)");
175124

176125
try
177126
{
178-
jsonpointer::add(target, "/foo/3", json("qux"));
127+
jsonpointer::add(target, "/baz", json("qux"));
179128
std::cout << target << std::endl;
180129
}
181130
catch (const parse_error& e)
@@ -184,7 +133,7 @@ void jsonpointer_add_element_outside_range()
184133
}
185134
}
186135

187-
void jsonpointer_try_add_element_outside_range()
136+
void jsonpointer_add_element_outside_range()
188137
{
189138
json target = json::parse(R"(
190139
{ "foo": [ "bar", "baz" ] }
@@ -201,15 +150,15 @@ void jsonpointer_try_add_element_outside_range()
201150
}
202151
}
203152

204-
void jsonpointer_remove()
153+
void jsonpointer_remove_object_member()
205154
{
206155
json target = json::parse(R"(
207-
{ "foo": [ "bar", "qux", "baz" ] }
156+
{ "foo": "bar", "baz" : "qux"}
208157
)");
209158

210159
try
211160
{
212-
jsonpointer::remove(target, "/foo/1");
161+
jsonpointer::remove(target, "/baz");
213162
std::cout << target << std::endl;
214163
}
215164
catch (const parse_error& e)
@@ -218,7 +167,7 @@ void jsonpointer_remove()
218167
}
219168
}
220169

221-
void jsonpointer_try_remove()
170+
void jsonpointer_remove_array_element()
222171
{
223172
json target = json::parse(R"(
224173
{ "foo": [ "bar", "qux", "baz" ] }
@@ -235,7 +184,7 @@ void jsonpointer_try_remove()
235184
}
236185
}
237186

238-
void jsonpointer_replace()
187+
void jsonpointer_replace_object_value()
239188
{
240189
json target = json::parse(R"(
241190
{
@@ -255,16 +204,13 @@ void jsonpointer_replace()
255204
}
256205
}
257206

258-
void jsonpointer_try_replace()
207+
void jsonpointer_replace_array_value()
259208
{
260209
json target = json::parse(R"(
261-
{
262-
"baz": "qux",
263-
"foo": "bar"
264-
}
210+
{ "foo": [ "bar", "baz" ] }
265211
)");
266212

267-
auto ec = jsonpointer::try_replace(target, "/baz", json("boo"));
213+
auto ec = jsonpointer::try_replace(target, "/foo/1", json("qux"));
268214
if (ec == jsonpointer::jsonpointer_errc())
269215
{
270216
std::cout << pretty_print(target) << std::endl;
@@ -281,17 +227,14 @@ void jsonpointer_examples()
281227
jsonpointer_select();
282228
jsonpointer_try_select();
283229
jsonpointer_add_member_to_object();
284-
jsonpointer_try_add_member_to_object();
285230
jsonpointer_add_element_to_array();
286-
jsonpointer_try_add_element_to_array();
287231
jsonpointer_add_element_to_end_array();
288-
jsonpointer_try_add_element_to_end_array();
232+
jsonpointer_add_value_name_exists();
289233
jsonpointer_add_element_outside_range();
290-
jsonpointer_try_add_element_outside_range();
291-
jsonpointer_remove();
292-
jsonpointer_try_remove();
293-
jsonpointer_replace();
294-
jsonpointer_try_replace();
234+
jsonpointer_remove_object_member();
235+
jsonpointer_remove_array_element();
236+
jsonpointer_replace_object_value();
237+
jsonpointer_replace_array_value();
295238
std::cout << std::endl;
296239
}
297240

tests/src/jsonpointer/jsonpointer_tests.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ BOOST_AUTO_TEST_CASE(test_add_array_value)
150150

151151
// remove
152152

153+
BOOST_AUTO_TEST_CASE(test_remove_object_member)
154+
{
155+
json example = json::parse(R"(
156+
{ "foo": "bar", "baz" : "qux"}
157+
)");
158+
159+
const json expected = json::parse(R"(
160+
{ "foo": "bar"}
161+
)");
162+
163+
check_remove(example,"/baz", expected);
164+
}
165+
153166
BOOST_AUTO_TEST_CASE(test_remove_array_element)
154167
{
155168
json example = json::parse(R"(
@@ -165,7 +178,7 @@ BOOST_AUTO_TEST_CASE(test_remove_array_element)
165178

166179
// replace
167180

168-
BOOST_AUTO_TEST_CASE(test_replace_value)
181+
BOOST_AUTO_TEST_CASE(test_replace_object_value)
169182
{
170183
json example = json::parse(R"(
171184
{
@@ -184,6 +197,19 @@ BOOST_AUTO_TEST_CASE(test_replace_value)
184197
check_replace(example,"/baz", json("boo"), expected);
185198
}
186199

200+
BOOST_AUTO_TEST_CASE(test_replace_array_value)
201+
{
202+
json example = json::parse(R"(
203+
{ "foo": [ "bar", "baz" ] }
204+
)");
205+
206+
const json expected = json::parse(R"(
207+
{ "foo": [ "bar", "qux" ] }
208+
)");
209+
210+
check_replace(example,"/foo/1", json("qux"), expected);
211+
}
212+
187213
// move
188214
/*
189215
BOOST_AUTO_TEST_CASE(test_move_value)

0 commit comments

Comments
 (0)