Dokka docs
Version maven-central

Kotlin Object Notation

Lightweight kotlin MPP DSL for building JSON trees

Setup

Just drop the dependency in your commonMain sourceSet

kotlin {
  sourceSets {
    commonMain {
      dependencies {
        implementation("dev.petuska:kon:_")
      }
    }
  }
}

Usage

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:

  • Map
  • Array
  • List
  • KObject
  • KArray
  • Number
  • String
  • Boolean
  • null
  • Pair
  • Triple

val json: ObjectMap = kobj {
  "str" by "string"
  "number"..1
  "boolean" % true
  "object" {
    "str" += "string"
    "pair" to (1 to "2")
    "triple" by Triple(true, false, null)
    "boolean" += true
    "withArray"[1, "2"]
    "nested" {
      "stillGood" by true
      "nullable" by null
    }
  }
  "array"[
      1,
      "2",
      true,
      karr[1, "2", false],
      kobj {
        "inner" += true
      }
  ]
}
json.toString() // will return json string

Modules

  • kon – wrapper module
  • kon-core – core implementation module

GitHub

View Github