KTON - Object notation in pure Kotlin!

Posts 1–2 of 2 · Page 1 of 1
KTON - Object notation in pure Kotlin!
GitHub repository: https://github.com/Jire/KTON

KTON
Object notation in pure Kotlin!

This library is licensed under GLP 3.0.

---

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:
Code:
{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                { "value": "New", "onclick": "CreateNewDoc()" },
                { "value": "Open", "onclick": "OpenDoc()" },
                { "value": "Close", "onclick": "CloseDoc()" }
            ]
        }
    }
}
XML:
Code:
<menu id="file" value="File">
    <popup>
        <menuitem value="New" onclick="CreateNewDoc()" />
        <menuitem value="Open" onclick="OpenDoc()" />
        <menuitem value="Close" onclick="CloseDoc()" />
    </popup>
</menu>
KTON:
Code:
val menu = kton {
    "id".."file"
    "value".."File"
    "popup" {
        "menuitem" {
            arr { "value".."New"; "onclick".."CreateNewDoc()" }
            arr { "value".."Open"; "onclick".."OpenDoc()" }
            arr { "value".."Close"; "onclick".."CloseDoc()" }
        }
    }
}
---

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

Value access through get operator:

menu["id"] => "file", menu["value"] => "File"

Body access through invoke operator:

val popup = menu("popup"), val menuitems = popup("menuitem")

Array access through get operator specifying index:

menuitems[0]["value"] => "New", menuitems[3]["onclick"] => "CloseDoc()"
Very interesting.
I like the idea, good job.
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us