chaseberry / KJson

A full Json implementation in Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KJson

A full Json implementation in Kotlin

Version 0.0.22 for Kotlin 1.1.0

Gradle

Latest version of 'kjson' @ Cloudsmith

To download through Gradle include this in your repositories

maven {
        url  "https://dl.cloudsmith.io/public/chase-s-projects/kjson/maven/" 
    }

Then add the following to your dependencies

compile 'edu.csh.chase.kjson:kjson:0.0.21'

I will be adding it to JCenter once I'm happy with everything, but for now it's in an early beta stage

#Features

val obj = json(
    "key" to "value",
    "key2" to "value2"
)

val arr = json [
    "value",
    "value2"
]

Getting values looks pretty too! (Thanks Kotlin!)

val key = obj["key"]
val key2 = obj["key"] as String

val v = arr[0]
val v2 = arr[1] as String

Or let the library handle casting for you!

val key = obj.getString("key")
val key2 = obj.getInt("key2")// Using the example from above this would return null as a String cannot be cast as an Int

All getter functions have two variations.

get<Type>(key:String):Type?//Returns null if the value is not present, null, or cannot be cast to type

get<Type>(key:String, default:Type):Type//Returns default if the value is not present, null, or cannot be cast to type

You can put a Boolean?, Int?, Double?, String?, Long?, Float?, JsonObject?, JsonArray?, List<Any?>?, and Map<String, Any?>?, or anything that implements JsonSerializable into a JsonObject or Json Array.

Both have support for forEach, iterators, and JsonArray has an indices val.

#Traversal Lets say you have json structure as such

{
    "key" : {
        "key2" : "value"
    }
}

Depth search for embedded JsonObjects or JsonArrays is done as such

val value = obj.traverse("key:key2")
val valueAsString = obj.traverseString("key:key2")

#Delegates

JsonDelegates let you map vals/vars to keys in a JsonObject

val key:String by JsonDelegates.objectVal(someJsonObject)

var v:Int by JsonDelegates.objectVar(someJsonObject)

This will use the name as the key in the object, and changes to the key are updated into the JsonObject

About

A full Json implementation in Kotlin

License:Apache License 2.0


Languages

Language:Kotlin 100.0%