/kon

JSON parsing library written in Kotlin

Primary LanguageKotlinApache License 2.0Apache-2.0

Kon

KON (improperly Kotlin Object Notation) is a JSON parsing library written in Kotlin that uses Kotlin's features

Index

Getting started

In your project's build.gradle

repositories {
	maven { url "https://jitpack.io" }
}

In your modules's build.gradle

dependencies {
    implementation 'com.github.maxpilotto:kon:$version'
}

Quick overview

Basic parsing

val string = """
    {
        "firstName": "John",
        "lastName": "Doe",
        "dob": "1987/09/25"
    }
"""
val json = JsonObject(string)

println(json.getString("firstName"))
println(json.getDate("dob", "yyyy/MM/dd"))

Inline object parsing

data class Address(
    val street: String,
    val number: Int,
    val country: String
)

val json = JsonObject("""
    {
        "firstName": "John",
        "lastName": "Doe",
        "address": {
            "street": "Downing Street",
            "number": 10,
            "country": "England"
        }
    }
""")
val address = json["address"].asObject { 
    Address(
        it.getString("street"),
        it.getInt("number"),
        it.getString("country")
    )
}

Automatic object parsing

//TODO

Get/Set operators

val json = JsonObject("""
    {
        "firstName": "John",
        "lastName": "Doe",
        "addresses": [
            {
                "street": "Downing Street",
                "number": 10,
                "country": "England"
            }
        ]
    }
""")

println(json["firstName"])
println(json["addresses"][0]["street"])

Basic fetch

// Sync call
val todos = JsonService.fetchArray("https://apiservice.com/todos")

// Async call
JsonService.fetchArray("https://apiservice.com/todos") {
    println(it)
}

Fetch and parse

//TODO