Skip to content

Commit ee0e371

Browse files
author
Piotr Bugara
committed
#37 Added tests for add operation which add field if it doesn't exist or it is equal to null. Added examples in README.md
1 parent 0aca192 commit ee0e371

File tree

3 files changed

+413
-0
lines changed

3 files changed

+413
-0
lines changed

README.md

+124
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Its features are:
2727
* {de,}serialization of JSON Patch and JSON Merge Patch instances with Jackson;
2828
* full support for RFC 6902 operations, including `test`;
2929
* JSON "diff" (RFC 6902 only) with operation factorization.
30+
* support for `JsonPointer` and `JsonPath`
3031

3132
## Versions
3233

@@ -264,6 +265,129 @@ final JsonNode patched = patch.apply(orig);
264265
}
265266
```
266267
<br />
268+
269+
### Add if not exists
270+
It's possible to add element to JsonNode if it does not exist using JsonPath expressions [see more examples of JsonPath](#jsonpath-examples)
271+
* Add `color` field to `bicycle` object if it doesn't exist
272+
`{ "op": "add", "path": "$.store.bicycle[?([email protected])].color", "value": "red" }`
273+
274+
Before:
275+
```json
276+
{
277+
"store": {
278+
"bicycle": {
279+
"price": 19.95
280+
}
281+
}
282+
}
283+
```
284+
285+
After:
286+
```json
287+
{
288+
"store": {
289+
"bicycle": {
290+
"price": 19.95,
291+
"color": "red"
292+
}
293+
}
294+
}
295+
```
296+
* Add value for `color` field to `bicycle` object if it is equal to `null`
297+
`{ "op": "add", "path": "$.store.bicycle[?(@.color == null)].color", "value": "red" }`
298+
299+
Before:
300+
```json
301+
{
302+
"store": {
303+
"bicycle": {
304+
"price": 19.95,
305+
"color": null
306+
}
307+
}
308+
}
309+
```
310+
311+
After:
312+
```json
313+
{
314+
"store": {
315+
"bicycle": {
316+
"price": 19.95,
317+
"color": "red"
318+
}
319+
}
320+
}
321+
```
322+
323+
* Add field `pages` to `book` array if `book` does not contain this field, or it is equal to `null`
324+
`{ "op": "add", "path": "$..book[?([email protected] || @.pages == null)].pages", "value": 250 }`
325+
326+
Before:
327+
```json
328+
{
329+
"store": {
330+
"book": [
331+
{
332+
"category": "reference",
333+
"author": "Nigel Rees",
334+
"title": "Sayings of the Century",
335+
"price": 8.95
336+
},
337+
{
338+
"category": "fiction",
339+
"author": "Herman Melville",
340+
"title": "Moby Dick",
341+
"isbn": "0-553-21311-3",
342+
"price": 8.99,
343+
"pages": null
344+
},
345+
{
346+
"category": "fiction",
347+
"author": "J.R.R. Tolkien",
348+
"title": "The Lord of the Rings",
349+
"isbn": "0-395-19395-8",
350+
"price": 22.99,
351+
"pages": 100
352+
}
353+
]
354+
}
355+
}
356+
```
357+
358+
After:
359+
```json
360+
{
361+
"store": {
362+
"book": [
363+
{
364+
"category": "reference",
365+
"author": "Nigel Rees",
366+
"title": "Sayings of the Century",
367+
"price": 8.95,
368+
"pages": 250
369+
},
370+
{
371+
"category": "fiction",
372+
"author": "Herman Melville",
373+
"title": "Moby Dick",
374+
"isbn": "0-553-21311-3",
375+
"price": 8.99,
376+
"pages": 250
377+
},
378+
{
379+
"category": "fiction",
380+
"author": "J.R.R. Tolkien",
381+
"title": "The Lord of the Rings",
382+
"isbn": "0-395-19395-8",
383+
"price": 22.99,
384+
"pages": 100
385+
}
386+
]
387+
}
388+
}
389+
```
390+
267391
### Remove operation
268392

269393
* Remove element with name `a`

src/test/resources/jsonpatch/add.json

+215
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,221 @@
14781478
},
14791479
"expensive": 10
14801480
}
1481+
},
1482+
{
1483+
"op": { "op": "add", "path": "$.store.bicycle[?([email protected])].color", "value": "red" },
1484+
"node":
1485+
{
1486+
"store": {
1487+
"bicycle": {
1488+
"price": 19.95
1489+
}
1490+
},
1491+
"expensive": 10
1492+
},
1493+
"expected":
1494+
{
1495+
"store": {
1496+
"bicycle": {
1497+
"color": "red",
1498+
"price": 19.95
1499+
}
1500+
},
1501+
"expensive": 10
1502+
}
1503+
},
1504+
1505+
{
1506+
"op": { "op": "add", "path": "$.store.bicycle[?(@.color == null)].color", "value": "red" },
1507+
"node":
1508+
{
1509+
"store": {
1510+
"bicycle": {
1511+
"price": 19.95,
1512+
"color": null
1513+
}
1514+
},
1515+
"expensive": 10
1516+
},
1517+
"expected":
1518+
{
1519+
"store": {
1520+
"bicycle": {
1521+
"color": "red",
1522+
"price": 19.95
1523+
}
1524+
},
1525+
"expensive": 10
1526+
}
1527+
},
1528+
1529+
{
1530+
"op": { "op": "add", "path": "$.store.bicycle[?([email protected])].color", "value": "red" },
1531+
"node":
1532+
{
1533+
"store": {
1534+
"bicycle": {
1535+
"color": "green",
1536+
"price": 19.95
1537+
}
1538+
}
1539+
},
1540+
"expected":
1541+
{
1542+
"store": {
1543+
"bicycle": {
1544+
"color": "green",
1545+
"price": 19.95
1546+
}
1547+
}
1548+
}
1549+
},
1550+
1551+
{
1552+
"op": { "op": "add", "path": "$..book[?([email protected] || @.pages == null)].pages", "value": 250 },
1553+
"node":
1554+
{
1555+
"store": {
1556+
"book": [
1557+
{
1558+
"category": "reference",
1559+
"author": "Nigel Rees",
1560+
"title": "Sayings of the Century",
1561+
"price": 8.95
1562+
},
1563+
{
1564+
"category": "fiction",
1565+
"author": "Herman Melville",
1566+
"title": "Moby Dick",
1567+
"isbn": "0-553-21311-3",
1568+
"price": 8.99,
1569+
"pages": null
1570+
},
1571+
{
1572+
"category": "fiction",
1573+
"author": "J.R.R. Tolkien",
1574+
"title": "The Lord of the Rings",
1575+
"isbn": "0-395-19395-8",
1576+
"price": 22.99,
1577+
"pages": 100
1578+
}
1579+
],
1580+
"bicycle": {
1581+
"color": "red",
1582+
"price": 19.95
1583+
}
1584+
},
1585+
"expensive": 10
1586+
},
1587+
"expected":
1588+
{
1589+
"store": {
1590+
"book": [
1591+
{
1592+
"category": "reference",
1593+
"author": "Nigel Rees",
1594+
"title": "Sayings of the Century",
1595+
"price": 8.95,
1596+
"pages": 250
1597+
},
1598+
{
1599+
"category": "fiction",
1600+
"author": "Herman Melville",
1601+
"title": "Moby Dick",
1602+
"isbn": "0-553-21311-3",
1603+
"price": 8.99,
1604+
"pages": 250
1605+
},
1606+
{
1607+
"category": "fiction",
1608+
"author": "J.R.R. Tolkien",
1609+
"title": "The Lord of the Rings",
1610+
"isbn": "0-395-19395-8",
1611+
"price": 22.99,
1612+
"pages": 100
1613+
}
1614+
],
1615+
"bicycle": {
1616+
"color": "red",
1617+
"price": 19.95
1618+
}
1619+
},
1620+
"expensive": 10
1621+
}
1622+
},
1623+
1624+
{
1625+
"op": { "op": "add", "path": "$..book[?([email protected])].pages", "value": 250 },
1626+
"node":
1627+
{
1628+
"store": {
1629+
"book": [
1630+
{
1631+
"category": "reference",
1632+
"author": "Nigel Rees",
1633+
"title": "Sayings of the Century",
1634+
"price": 8.95
1635+
},
1636+
{
1637+
"category": "fiction",
1638+
"author": "Herman Melville",
1639+
"title": "Moby Dick",
1640+
"isbn": "0-553-21311-3",
1641+
"price": 8.99,
1642+
"pages": null
1643+
},
1644+
{
1645+
"category": "fiction",
1646+
"author": "J.R.R. Tolkien",
1647+
"title": "The Lord of the Rings",
1648+
"isbn": "0-395-19395-8",
1649+
"price": 22.99,
1650+
"pages": 100
1651+
}
1652+
],
1653+
"bicycle": {
1654+
"color": "red",
1655+
"price": 19.95
1656+
}
1657+
},
1658+
"expensive": 10
1659+
},
1660+
"expected":
1661+
{
1662+
"store": {
1663+
"book": [
1664+
{
1665+
"category": "reference",
1666+
"author": "Nigel Rees",
1667+
"title": "Sayings of the Century",
1668+
"price": 8.95,
1669+
"pages": 250
1670+
},
1671+
{
1672+
"category": "fiction",
1673+
"author": "Herman Melville",
1674+
"title": "Moby Dick",
1675+
"isbn": "0-553-21311-3",
1676+
"price": 8.99,
1677+
"pages": null
1678+
},
1679+
{
1680+
"category": "fiction",
1681+
"author": "J.R.R. Tolkien",
1682+
"title": "The Lord of the Rings",
1683+
"isbn": "0-395-19395-8",
1684+
"price": 22.99,
1685+
"pages": 100
1686+
}
1687+
],
1688+
"bicycle": {
1689+
"color": "red",
1690+
"price": 19.95
1691+
}
1692+
},
1693+
"expensive": 10
1694+
}
14811695
}
1696+
14821697
]
14831698
}

0 commit comments

Comments
 (0)