SimpleJson is a library for easily writing or reading JSON.
You can use like Map.
You can also make handle to deep nodes with a symbol.
The library was made using Jackson.
- ver1.0.0: SimpleJson-1.0.0.jar
<repositories>
...
<repository>
<id>github</id>
<name>SimpleJson</name>
<url>https://raw.githubusercontent.com/Smile-NS/SimpleJson/mvn-repo/</url>
</repository>
...
</repositories>
<dependencies>
...
<dependency>
<groupId>io.github.smile-ns.simplejson</groupId>
<artifactId>SimpleJson</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
These are ways to use and example codes.
But also look at Javadoc because not all written here.
SimpleJson json = new SimpleJson(); // An empty object.
// An object with the specified file to save.
// If already the file exists, create one by referencing the file.
SimpleJson json = new SimpleJson(file);
SimpleJson json = new SimpleJson(new Foo()); // An object with another instance.
SimpleJson json = new SimpleJson("{\"smile\":\"noob\"}"); // An object with a string.
Put a (pseudo) primitive type element.
SimpleJson json = new SimpleJson();
json.put("id", 10);
json.put("name", "test");
...
{
"id": 10,
"name": "test"
}
In this way, you can also put a value for a deep node.
Separate with '.'
When try to find it that doesn't exsist, create empty one.
SimpleJson json = new SimpleJson();
json.put("smile.iq", 80);
{
"smile": {
"iq": 80
}
}
Put a reference type element.
- Map and List
SimpleJson json = new SimpleJson();
Map<String, Integer> map = new HasMap<>();
map.put("key", 3);
json.put("map", map);
List<String> list = new ArrayList<>();
list.add("smile");
list.add("angry");
json.put("list", list);
{
"map": {
"key": 3
},
"list": [ "smile", "angry" ]
}
- instance
class Foo {
int id = 1;
String name = "test";
}
SimpleJson json = new SimpleJson();
json.put("Foo", new Foo());
{
"Foo": {
"id": 1,
"name": "test"
}
}
When put a reference type element, using SimpleJson#put(String, SimpleJson) is the fastest.
SimpleJson json = new SimpleJson();
SimpleJson json2 = new SimpleJson();
json2.put("key", 10);
json.put("json2", json2);
{
"json2": {
"key": 10
}
}
Remove a selected node.
Like "put", you can remove deep nodes.
json.remove(key, value);
Get a selected node as (pseudo) primitive type, their wrapper classes, or reference type.
You can get from a deep node, too.
json.getInt("key");
json.getList("key2");
Convert and get an object.
- SimpleJson#toString()
- Convert it to String
- SimpleJson#toJsonNode()
- Convert it to JsonNode
- SimpleJson#toJavaObject()
- Convert it to an instance of the specified class
- SimpleJson#toMap()
- Convert it to Map
Save in a file.
If you don't set it to save from a constructor or SimpleJsonProperty#setFile(file), you can't save.
json.save();