Code Monkey home page Code Monkey logo

kjson's Introduction

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

kjson's People

Contributors

chaseberry avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

billzeh

kjson's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.