Object notation in pure Kotlin!

Related tags

Kotlin KTON
Overview

KTON

Object notation in pure Kotlin!

Build Status Dependency Status license

Gradle

compile 'org.jire.kton:KTON:1.0.1'

Maven

<dependency>
    <groupId>org.jire.kton</groupId>
    <artifactId>KTON</artifactId>
    <version>1.0.1</version>
</dependency>

KTON allows you to notate objects in an easy syntax using pure Kotlin. This is mostly a toy project but it may be useful for certain cases. I'd like to make conversion to JSON and provide more advanced features.

The following shows the differences between JSON, XML, and KTON. (Data used from http://json.org/example.html)

JSON:

{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                { "value": "New", "onclick": "CreateNewDoc()" },
                { "value": "Open", "onclick": "OpenDoc()" },
                { "value": "Close", "onclick": "CloseDoc()" }
            ]
        }
    }
}

XML:

<menu id="file" value="File">
    <popup>
        <menuitem value="New" onclick="CreateNewDoc()" />
        <menuitem value="Open" onclick="OpenDoc()" />
        <menuitem value="Close" onclick="CloseDoc()" />
    </popup>
</menu>

KTON:

val menu = kton {
    "id" to "file"
    "value" to "File"
    "popup" {
        "menuitem" [
            { "value" to "New"; "onclick" to "CreateNewDoc()" },
            { "value" to "Open"; "onclick" to "OpenDoc()" },
            { "value" to "Close"; "onclick" to "CloseDoc()" }
        ]
    }
}

Accessing data from a KTON is done with concise syntax. Using the above example...

// Value access through get operator:
val id = menu["id"] // "file"
val value = menu["value"] // "File"

// Body access through invoke operator:
val popup = menu("popup")
val menuitems = popup("menuitem")

// Array access through get operator specifying index:
val newValue = menuitems[0]["value"] // "New"
val open = menuitems[1]
val openValue = open["value"] // "Open"
val closeOnClick = menuitems[2]["onclick"] // "CloseDoc()"
You might also like...
Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️

kotlin-android-template 🤖 A simple Github template that lets you create an Android/Kotlin project and be up and running in a few seconds. This templa

LifecycleMvp 1.2 0.0 Kotlin  is MVP architecture implementation with Android Architecture Components and Kotlin language features
LifecycleMvp 1.2 0.0 Kotlin is MVP architecture implementation with Android Architecture Components and Kotlin language features

MinSDK 14+ Download Gradle Add to project level build.gradle allprojects { repositories { ... maven { url 'https://jitpack.io' }

Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

👋 A common toolkit (utils) ⚒️ built to help you further reduce Kotlin boilerplate code and improve development efficiency. Do you think 'kotlin-stdlib' or 'android-ktx' is not sweet enough? You need this! 🍭

Toolkit [ 🚧 Work in progress ⛏ 👷 🔧️ 🚧 ] Snapshot version: repositories { maven("https://s01.oss.sonatype.org/content/repositories/snapshots") }

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

Provides Kotlin libs and some features for building Kotlin plugins
Provides Kotlin libs and some features for building Kotlin plugins

Kotlin Plugin Provides Kotlin libs and some features for building awesome Kotlin plugins. Can be used instead of CreeperFace's KotlinLib (don't use to

Notes-App-Kotlin - Notes App Built Using Kotlin
Notes-App-Kotlin - Notes App Built Using Kotlin

Notes-App-Kotlin Splash Screen Home Page Adding New Notes Filter Feature Search

Kotlin-client-dsl - A kotlin-based dsl project for a (Client) - (Plugin) styled program

kotlin-client-dsl a kotlin-based dsl project for a (Client) - (Plugin) styled p

A Kotlin Native program to show the time since a date, using Kotlin LibUI
A Kotlin Native program to show the time since a date, using Kotlin LibUI

TimeSince A Kotlin Native program to show the time since a date, using Kotlin LibUI Report Bug . Request Feature About The Project TimeSince is a Kotl

Comments
  • Example from README does't work

    Example from README does't work

    The example you show in the README doesn't work: trying to access the "menuitem" array by the invoke operator returns null. After some experimenting, I found out that you can access an array skipping the key it should be mapped to and just using the indexed get. Also adding more than one array to an object just adds their elements to one larger collection. I think array construction is bugged.

    opened by polarene 0
  • not 'working for me'

    not 'working for me'

    I forked the project and attempted to add a basic test case. I cant get it to work beyond the most simple case. Either wont compile, or returns nulls for most things. I think arrays are the most problematic but even nested json I couldnt get to work. Could be just me misunderstanding - but that was the reason I tried this -- its simple --

    If you are interested, follow the fork

    -David

    opened by DALDEI 0
Releases(1.0.1)
Owner
Jire
🍖 the coding caveman
Jire
A pure Kotlin/Multiplatform implementation of group operations on Curve25519.

curve25519-kotlin A pure Kotlin/Multiplatform implementation of group operations on Curve25519. Gradle Kotlin DSL: dependencies { implementation("

Andrey Pfau 9 Dec 22, 2022
Template (pure) for KMM application with DI support

KMM di template Template (pure) for KMM application with DI support. Uses Multiplatform-DI for Dependency Injection Features Common architecture (VIP)

Anna Zharkova 8 Oct 18, 2022
Kotlin Android object for global applicationContext

ContextProvider Kotlin Android object for global applicationContext Usage In your Aplication class class YourApp : Application() { override fun o

PaulRB 0 Nov 4, 2021
EVMapper: Simple object mapper for Kotlin

EVMapper - Simple object mapper for Kotlin Simple mapping data class AData(val p1: Int, val p2: Int) data class BData(val p1: Int, val p2: Int) val a

Igor 8 Jan 24, 2022
ShapeShift️ - A Kotlin library for intelligent object mapping and conversion between objects

ShapeShift️ A Kotlin library for intelligent object mapping and conversion between objects. Documentation Installation Maven Gradle Groovy DSL Kotlin

KRUD 127 Dec 7, 2022
A modular object storage framework for Kotlin multiplatform projects.

ObjectStore A modular object storage framework for Kotlin multiplatform projects. Usage ObjectStore provides a simple key/value storage interface whic

Drew Carlson 4 Nov 10, 2022
A thought experiment on architecture, object-oriented programming, and composability.

Journal3 There's barely anything special about the features that Journal3 is offering, it's literally yet another journaling application. What is spec

Hadi Satrio 7 Dec 13, 2022
Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Cash App 1.5k Dec 30, 2022
A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

common sense OSS 0 Jan 20, 2022
Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Gérard Paligot 98 Dec 15, 2022