Simple object-oriented Java API for JSON transformation. It is a wrapper around JSON-P.
From values
new JsonArr<>(
true,
"a",
1,
new JsonArr(),
new JsonObj()
);
or from a string
new JsonArr(
new StringReader("[false,\"x\",1,[],{}]")
);
or from JSON-P array
new JsonArr<>(
Json.createArrayBuilder().add(0).build()
);
From object attributes
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
new JsonObj(
new StringReader(
"{\"bool\":false,\"str\":\"A\",\"num\":1,\"arr\":[],\"obj\":{}}"
)
);
or from JSON-P object
new JsonObj(
Json.createObjectBuilder().add("name", "John").build()
);
Transforming JSON arrays (with help of Cactoos library)
In the example, numerical values are filtered and mapped to string values
JsonArr<Integer> 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"]
.
In the example, value of num
attribute is multiplied by 2.
JsonObj object = new JsonObj(
new Attr<>("str", "A"),
new Attr<>("num", 1)
);
new FitValUpd(
"num",
(object.<Integer>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
.
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.
To get started, add dependency to your project:
<dependency>
<groupId>com.github.piotrkot</groupId>
<artifactId>oojson</artifactId>
<version>2.0.0</version>
</dependency>
You may need to add JSON-P dependency:
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.7</version>
<scope>runtime</scope>
</dependency>
Feel free to fork me on GitHub, report bugs or post comments.
For Pull Requests, please run mvn clean package
, first.