https://github.com/piotrkot/oojson
Object-oriented JSON. Wrapper for JSON-P
https://github.com/piotrkot/oojson
json oop
Last synced: 4 months ago
JSON representation
Object-oriented JSON. Wrapper for JSON-P
- Host: GitHub
- URL: https://github.com/piotrkot/oojson
- Owner: piotrkot
- License: mit
- Created: 2018-11-13T12:55:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-01T15:23:45.000Z (over 1 year ago)
- Last Synced: 2025-07-21T14:03:25.180Z (10 months ago)
- Topics: json, oop
- Language: Java
- Size: 120 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/piotrkot/oojson)
[](https://coveralls.io/github/piotrkot/oojson?branch=master)
# Object-oriented Java API for JSON
Simple object-oriented Java API for JSON transformation. It is a wrapper around
[JSON-P](https://javaee.github.io/jsonp/).
## Creating JSON arrays
From values
```java
new JsonArr<>(
true,
"a",
1,
new JsonArr(),
new JsonObj()
);
```
or from a string
```java
new JsonArr(
new StringReader("[false,\"x\",1,[],{}]")
);
```
or from JSON-P array
```java
new JsonArr<>(
Json.createArrayBuilder().add(0).build()
);
```
## Creating JSON objects
From object attributes
```java
new JsonObj(
new Attr<>("bool", true),
new Attr<>("str", "a"),
new Attr<>("num", 0),
new Attr<>("arr", new JsonArr()),
new Attr<>("obj", new JsonObj())
);
```
or from a string
```java
new JsonObj(
new StringReader(
"{\"bool\":false,\"str\":\"A\",\"num\":1,\"arr\":[],\"obj\":{}}"
)
);
```
or from JSON-P object
```java
new JsonObj(
Json.createObjectBuilder().add("name", "John").build()
);
```
## Transforming JSON arrays (with help of [Cactoos](http://www.cactoos.org/) library)
In the example, numerical values are filtered and mapped to string values
```java
JsonArr array = new JsonArr<>(
10,
20
);
new JsonArr<>(
new Mapped<>(
elem -> elem + " points",
new Filtered<>(
elem -> elem > 15,
array.value()
)
)
);
```
The result is array `["20 points"]`.
## Making JSON objects fit
In the example, value of `num` attribute is multiplied by 2.
```java
JsonObj object = new JsonObj(
new Attr<>("str", "A"),
new Attr<>("num", 1)
);
new FitValUpd(
"num",
(object.get("num")) * 2
).make(object);
```
The result is object `{"str":"A","num":2}`.
In the example, attribute with name `delete` is removed and attribute
with name `info` is replaced with attribute `"moreInfo"` and value `true`.
```java
JsonObj object = new JsonObj(
new Attr<>("delete", "private"),
new Attr<>("info", "public info")
);
new FitChain<>(
new FitAttrDel("delete"),
new FitAttrRepl("info", new Attr<>("moreInfo", true))
).make(object);
```
The result is object `{"moreInfo":true}`.
Library supports [JSON specification](https://json.org/).
To get started, add dependency to your project:
```xml
com.github.piotrkot
oojson
2.0.0
```
You may need to add [JSON-P](https://javaee.github.io/jsonp/) dependency:
```xml
jakarta.json
jakarta.json-api
2.1.3
provided
org.eclipse.parsson
parsson
1.1.7
runtime
```
Feel free to fork me on GitHub, report bugs or post comments.
For Pull Requests, please run `mvn clean package`, first.