Kotlin Json DSL
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.fcannizzaro:kson:1.0.3'
}
val data = json {
entry("key", "value")
entry("array") {
val items = (1..4).map { "item $it" }.reversed()
items
}
entry("array-fast", array("fast 0", "fast 1"))
entry("another-key", "another-value")
}
println(data.toString(4))
{
"array-fast": ["fast 0", "fast 1"],
"array": ["item 4", "item 3", "item 2", "item 1"],
"another-key": "another-value",
"key": "value"
}
{
"first": {
"second" : {
"key" : "value"
}
}
}
You can access "key" by simply doing:
val obj : JSONObject = /* already built */
val key = obj["first"]["second"]["key"] as String
Create a JSONObject.
Add a new entry.
Create a JSONArray.
- Entry key cannot be empty.
- Entry value can only be Int, Double, Float, String, JSONArray, JSONObject or Map.
Francesco Saverio Cannizzaro (fcannizzaro)