https://github.com/antonsjava/jmom
https://github.com/antonsjava/jmom
json json-manipulation
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/antonsjava/jmom
- Owner: antonsjava
- License: apache-2.0
- Created: 2019-11-18T20:53:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-15T20:35:20.000Z (over 3 years ago)
- Last Synced: 2025-02-01T21:17:33.664Z (4 months ago)
- Topics: json, json-manipulation
- Language: Java
- Size: 24.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jmom
Mom is woman, which tries to modify her son to be nicer.
jmom is java library, which tries to modify json to be nicer.
## Base of usage
The library is based on antonsjava.[json](https://github.com/antonsjava/json) library, which is base for json
manipulation. Json data are provided as instances of json API.Creating json API object from String.
```java
String jsonvalue = .....
JsonValue json = JsonParser.parse(jsonvalue);
```
Creating jmom API object for manipulating json.
```java
Jmom jmom = Jmom.instance()
.xxxxx() //a modification rule
.xxxxx() //a modification rule
.xxxxx() //a modification rule
;
```
Modifying json using jmom API.
```java
jmom.apply(json);
```
Converting json back to String.
```java
String newjsonvalue = json.toCompactString();
```## Json data pointers
Modification rules from Jmom API must address some parts of json to be modified.
The library uses WildPathMatcher syntax for addressing subparts of json.
Individual path elements can be
- simple names like "foo", "bar"
- wild names like "foo-*", "bar-?"
- "**" for representing sequence of path elementsSo pointers can look like
- "/foo/bar/item" represents only "/foo/bar/item"
- "/foo/*/item" represents paths like "/foo/bar/item", "/foo/nobar/item", ....
- "/foo/**/item" represents paths like "/foo/item", "/foo/bar/item", "/foo/bar/bar/item"....Data pointer can be postfixed with '|{int number}'. It moves path selected jsno node to parent
{int number} times. For example "**/config" selects all paths which ends with '/config' but
"**/config|1" selects direct parents of previous selections.## Modification rules
### Add
Add rule allows to add specified json value to given json. It has following parameters
- path - exact path where new json must be inserted. (no wilds are allowed here)
- jsonvalue - value tu be inserted into json
- replace - true is old value should be replaced is exists. (default: false)Examples:
```java
Jmom jmom = Jmom.instance()
.add("/menu/name", JsonFactory.stringLiteral("nove mneo"), true)
.add("/menu/popup/menuitem/2", JsonParser.parse("{\"value\": \"SaveAs\", \"onclick\": \"SaveAs()\"}"), true)
;
```### Remove
Remove rule allows to remove specified parts from given json. It has following parameters
- path - list of data pointers exact path where new json must be inserted.Examples:
```java
Jmom jmom = Jmom.instance()
.remove("/menu/name-?", "**/config|1")
;
```### KeepOnly
KeepOnly rule allows to remove all parts from given json except specified. It has following parameters
- path - list of data pointers exact path where new json must be saved.Examples:
```java
Jmom jmom = Jmom.instance()
.keepOnly("/menu/name-?", "**/config|1")
;
```
### CopyCopy rule allows to copy specified json part to different place of json. It has following parameters
- frompath - path which must be copied. (first part is used if more can be identified)
- topath - exact path where from json must be inserted. (no wilds are allowed here)
- move - true is old part should be deleted (default: false)Examples:
```java
Jmom jmom = Jmom.instance()
.copy("/menu/id", "/menu/ctx/id", true)
;
```### Apply
Apply rule allows to apply a rule on chosen subparts of given json. The rule can be
anything.
- rule - any rule to be aplied.
- path - list of data pointers exact path where rule must be appliedExample: (for each objects containing "street" attribute create new one "streetFull" )
```java
Jmom jmom = Jmom.instance()
.apply((json) -> {
String street = json.asObject().first("street");
String streetnum = json.asObject().first("streetNo");
json.asObject().attr("streetFull", street+" "+streetNo);
}, "**/street|1")
;
```## Maven usage
```
io.github.antonsjava
jmom
LASTVERSION
```