Concept
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate. It has become a de facto standard for data exchange on the web, especially for APIs. (JSON on Wikipedia)
Sinobu offers built-in support for JSON processing with an emphasis on performance, usability, and integration with Java object models.
✅ Simplicity
Offering straightforward APIs for parsing JSON from various sources and serializing Java objects into JSON.
void read() {
I.json("""
{
"name": "Misa",
"age": 21
}
""");
}
🧩 Model-Centric
Seamlessly mapping JSON data to and from Java objects (POJOs or Records) based on Sinobu's property model, reducing boilerplate.
public void writeRecord() {
record Person(String name, int age) {
}
Person person = new Person("Joe", 23);
String json = I.write(person);
assertSame(json, """
{
"age": 23,
"name": "Joe"
}
""");
}
👉 Direct Manipulation
Parsed JSON can be navigated and mutated via a DOM-like tree model using the JSON class. Access elements by key or index, modify values, or restructure nodes with intuitive syntax.
public void readValue() {
JSON json = I.json("""
{
"number" : 10,
"bool" : false
}
""");
assert json.get(int.class, "number") == 10;
assert json.get(boolean.class, "bool") == false;
}
Reading
You can read JSON from strings, files, and various inputs. All data will be expanded into memory in a tree format. It is not a streaming format, so please be careful when parsing very large JSON.
Access to the value
You can access the value by specifying the key.
public void readValue() {
JSON json = I.json("""
{
"number" : 10,
"bool" : false
}
""");
assert json.get(int.class, "number") == 10;
assert json.get(boolean.class, "bool") == false;
}
Access to the nested value
You can specify a key multiple times to access nested values.
public void readNestedValue() {
JSON json = I.json("""
{
"child1" : {
"name" : "first"
},
"child2" : {
"name" : "second"
}
}
""");
assert json.get("child1").get("name").as(String.class).equals("first");
assert json.get("child2").get("name").as(String.class).equals("second");
}
You can also find all values by the sequential keys.
public void readNestedValueBySequentialKeys() {
JSON json = I.json("""
[
{
"type" : {
"name" : "first"
}
},
{
"type" : {
"name" : "second"
}
}
]
""");
List<String> found = json.find(String.class, "*", "type", "name");
assert found.size() == 2;
assert found.get(0).equals("first");
assert found.get(1).equals("second");
}
Writing
You can write JSON from property-based model.
public void writeRecord() {
record Person(String name, int age) {
}
Person person = new Person("Joe", 23);
String json = I.write(person);
assertSame(json, """
{
"age": 23,
"name": "Joe"
}
""");
}