
Lightweight DSL for building JSON trees, supporting various types like Map, Array, Collection, and more. Easily convert objects to JSON strings or JS objects with extended serialization options.
Lightweight kotlin MPP DSL for building JSON trees
Just drop the dependency in your commonMain sourceSet
kotlin {
sourceSets {
commonMain {
dependencies {
implementation("dev.petuska:kon:_")
}
}
}
}Build the object map via kobj builder function. Built object overrides Any::toString function to return proper json
representation of the underlying structure. Currently, supported kotlin types for json output are the following:
val json: KON = kobj {
"str" to "string"
"number" to 420
"boolean" to true
"object" {
"str" to "string"
"number" to 1
"pair" to (1 to null)
"triple" to Triple(1, "2", 3)
"boolean" to true
"withArray" [1, "2"]
"nested" {
"stillGood" to true
"nullable" to null
}
}
"consistentObject" to {
"nice?" to 69
}
"array"[
1,
"2",
true,
karr[1, "2", false],
kobj {
"inner" to true
}
]
"singleIntArray".to[1]
"singleIntArray2" to karr[1]
}
json.toString() // Will return json string
json.toJS() // Only available on js target. Will return JS Json object
val jsonObject = json.toJsonObject() // Requires kon-serialization dependency. Will return kotlinx.serialization JsonObject.
jsonObject.toKObject() // Requires kon-serialization dependency. Will return KON KObject.Lightweight kotlin MPP DSL for building JSON trees
Just drop the dependency in your commonMain sourceSet
kotlin {
sourceSets {
commonMain {
dependencies {
implementation("dev.petuska:kon:_")
}
}
}
}Build the object map via kobj builder function. Built object overrides Any::toString function to return proper json
representation of the underlying structure. Currently, supported kotlin types for json output are the following:
val json: KON = kobj {
"str" to "string"
"number" to 420
"boolean" to true
"object" {
"str" to "string"
"number" to 1
"pair" to (1 to null)
"triple" to Triple(1, "2", 3)
"boolean" to true
"withArray" [1, "2"]
"nested" {
"stillGood" to true
"nullable" to null
}
}
"consistentObject" to {
"nice?" to 69
}
"array"[
1,
"2",
true,
karr[1, "2", false],
kobj {
"inner" to true
}
]
"singleIntArray".to[1]
"singleIntArray2" to karr[1]
}
json.toString() // Will return json string
json.toJS() // Only available on js target. Will return JS Json object
val jsonObject = json.toJsonObject() // Requires kon-serialization dependency. Will return kotlinx.serialization JsonObject.
jsonObject.toKObject() // Requires kon-serialization dependency. Will return KON KObject.