Simple Kotlin DSL library for creating JSON strings.
dependencies {
implementation 'io.github.kshired:kotlin-json-dsl:0.0.1'
}
dependencies {
implementation("io.github.kshired:kotlin-json-dsl:0.0.1")
}
<dependency>
<groupId>io.github.kshired</groupId>
<artifactId>kotlin-json-dsl</artifactId>
<version>0.0.1</version>
</dependency>
val json = jsonObject {
"name" to "John"
"age" to 30
"isStudent" to true
"address" to jsonObject {
"street" to "Main St"
"city" to "New York"
}
"friends" to jsonArray {
jsonObject {
"name" to "Alice"
"age" to 25
}
jsonObject {
"name" to "Bob"
"age" to 35
}
}
}
val jsonString = json.toJsonString()
val prettyJsonString = json.toPrettyJsonString()
Output of the above code:
jsonString
:
{"name":"John","age":30,"isStudent":true,"address":{"street":"Main St","city":"New York"},"friends":[{"name":"Alice","age":25},{"name":"Bob","age":35}]}
prettyJsonString
:
{
"name": "John",
"age": 30,
"isStudent": true,
"address": {
"street": "Main St",
"city": "New York"
},
"friends": [
{
"name": "Alice",
"age": 25
},
{
"name": "Bob",
"age": 35
}
]
}