A JavaScript Object Notation library for Kotlin JVM.

Overview

version license Awesome Kotlin

Discord

Kotlin JSON

Kotlin JSON is a lightweight, highly stylized JavaScript Object Notation (JSON) for The Kotlin Programming Language.

Stylistic

Kotlin JSON allows developers to use the stylistic abilities of Kotlin's DSL API to create a stylish but highly readable and functional Domain Specifying Language:

fun main(args: Array<String>) {
    val json = JSObject {
        "message" to "Hello, World!"
    }
    
    println(json.string("message"))
}
// Hello, World!

Full Type Support

Kotlin JSON supports JSON Objects, JSON Arrays, as well as normal JSON values such as Strings, Booleans, Numbers, and null values!

fun main(args: Array<String>) {
    JSObject {
        "null" to null
        "string" to "Hello, World!"
        "int" to 123
        "long" to 1234567891011121314L
        "float" to 123.4
        "double" to 12345678.9101112
        "object" to JSObject { }
        "array" to JSArray()
    }
}

The following types are supported

Featured

Kotlin JSON packs standard string parsing and IO operations which allow you to simplistically read JSON text from anything from a String to an InputStream!

fun main(args: Array<String>) {
      // my-json-file.json
    // {
    //   "message": "Hello, this is from a file!"
    // }
    val myJsonFile = File("my-json-file.json")
    val json = myJsonFile.readJSObject()
    
    println(json.string("message"))
}
// Hello, this is from a file!

Familiar

Kotlin JSON embraces kotlin style in order to create streamlined operations between it and the kotlin standard library.

fun main(args: Array<String>) {
    // similar to mapOf()
    jsObjectOf("foo" to "bar")
    
    // similar to listOf()
    jsArrayOf("foo", "bar", "baz")
    
    // similar to toMap()
    mapOf("foo" to "bar").toJSObject()
    
    // similar to toList()
    listOf("foo", "bar", "baz").toJSArray()
}

The library provides the interfaces JSObject and JSArray which both extend the MutableMap and MutableList interfaces respectively, and allow for a familiar writing style to the rest of the kotlin language.

Reflective (EXPERIMENTAL)

The reflect module of the library uses reflection based operations to easily and efficiently serialize and deserialize JSObjects from and into class instances!

For the upcoming examples, consider the following 2 data classes:

data class House(
    val address: String,
    val tenants: List<Person>
)

data class Person(
    val name: String,
    val age: Int,
    @JSOptional val mother: Person? = null,
    @JSOptional val father: Person? = null
)

Using the JSDeserializer, JSObjects can be transformed into instances of data classes, as well as other types.

fun main(args: Array<String>) {
    val deserializer = JSDeserializer()
    val json = JSObject {
        "address" to "123 JSON Lane"

        val mother = JSObject {
            "name" to "Maxine"
            "age" to 42
        }

        val father = JSObject {
            "name" to "Logan"
            "age" to 45
        }

        val child = JSObject {
            "name" to "James"
            "age" to 16
            "mother" to mother
            "father" to father
        }

        "tenants" to JSArray(mother, father, child)
    }

    val house = deserializer.deserialize(json, House::class)
    for(tenant in house.tenants) {
        println("${tenant.name} is ${tenant.age}")
    }
}
// Maxine is 42
// Logan is 45
// James is 16

You can also easily serialize any type into a JSObject using an instance of JSSerializer!

fun main(args: Array<String>) {
    val serializer = JSSerializer()
    val house = House(
        address = "123 JSON Lane",
        tenants = listOf(
            Person(name = "John", age = 22),
            Person(name = "Jill", age = 20)
        )
    )

    val json = serializer.serialize(house)
    println(json)
}
// {"address":"123 JSON Lane","tenants":[{"name":"John","age":22}, {"name":"Jill","age":20}]}

Dependency Setup

Gradle

repositories {
    jcenter()
}

dependencies {
    compile "me.kgustave:kotlin-json:${kotlin_json_version}"

    // for reflection support add this
    compile "me.kgustave:kotlin-json-reflect:${kotlin_json_version}"
}

Maven

<repositories>
  <repository>
    <id>jcenter</id>
    <name>jcenter-bintray</name>
    <url>https://jcenter.bintray.com</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>me.kgustave</groupId>
    <artifactId>kotlin-json</artifactId>
    <version>${kotlin_json_version}</version>
    <type>pom</type>
  </dependency>

  <!-- for reflection support add this -->
  <dependency>
    <groupId>me.kgustave</groupId>
    <artifactId>kotlin-json-reflect</artifactId>
    <version>${kotlin_json_version}</version>
  </dependency>
</dependencies>

License

kotlin-json is licensed under the Apache 2.0 License

Copyright 2018 Kaidan Gustave

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
๐ŸšŸ Lightweight, and simple scheduling library made for Kotlin (JVM)
๐ŸšŸ Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru ๐ŸšŸ Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

A property/method accessor library for the JVM, written in Kotlin

unlok - unlock your JVM a property/method accessor library for the JVM, written in Kotlin. how to import you can import unlok from maven central just

 A Kotlin library providing a simple, high-performance way to use off-heap native memory in JVM applications.
A Kotlin library providing a simple, high-performance way to use off-heap native memory in JVM applications.

native_memory_allocator A library which uses sun.misc.Unsafe to allocate off-heap native memory. Motivation The goal of this project is to provide a s

๐ŸŽ‘ Up to date IANA timezone database library for Kotlin (JVM, JS, Native)

๐ŸŽ‘ IANA Timezone Library for Kotlin Multiplatform Up to date IANA timezone database library for Kotlin (JVM, JS, Native) Usage import org.noelware.ian

Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web ๋ณธ ์ €์žฅ์†Œ๋Š” ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ๊ธฐ๋ฐ˜ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์›Œํฌ์ˆ(๊ฐ•์ขŒ)์„ ์œ„ํ•ด ์ž‘์„ฑ๋œ ํ…œํ”Œ๋ฆฟ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์›Œํฌ์ˆ ๊ณผ์ •์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋ก ํŠธ์—”๋“œ(front-end)๋Š” Ko

Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform ๋ณธ ์ €์žฅ์†Œ๋Š” INFCON 2022์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ๊ธฐ๋ฐ˜ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ธ์ฆˆ์˜จ๋žฉ์„ ์œ„ํ•ด ์ž‘์„ฑ๋œ ํ…œํ”Œ๋ฆฟ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ํ•ธ์ฆˆ์˜จ ๊ณผ์ •์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ์„

Yaspeller-kt - Asynchronous Yandex.Speller API wrapper for Kotlin/JVM.

yaspeller-kt Asynchronous Yandex.Speller API wrapper for Kotlin/JVM. Installation repositories { maven { url 'https://jitpack.io' }

Port immer for kotlin/jvm
Port immer for kotlin/jvm

1. kimmer Port https://github.com/immerjs/immer for kotlin/jvm (requires kotlin 1.5+). Immer is the winner of the "Breakthrough of the year" React ope

Comments
  • Multiplatform Support

    Multiplatform Support

    This PR introduces multiplatform support and common code integration for the kotlin-json library.

    This PR will introduce a Kotlin2Js implementation of the library with complete support for JSObject, JSArray, parsing functions, and DSL functions.

    Many previously deprecated functions will see removal in this release, as well as the kotlin-json-core module, which will now be replaced by the newly introduced kotlin-json-jvm module

    opened by Shengaero 0
  • Enhancement/reflect experimental

    Enhancement/reflect experimental

    Adds @Experimental annotations to reflect module resources. Additionally, this PR divides the reflective API into two parts, me.kgustave.json.reflect.JSSerialization and me.kgustave.json.reflect.JSDeserialization which allow you to experimentally use either section individually, or both with the -Xuse-experimental Kotlin compiler option

    opened by Shengaero 0
Owner
Kaidan Gustave
I made a thing (I think)
Kaidan Gustave
JavaScript evaluation from kotlin common code for android & iOS

Mobile Kotlin javascript This is a Kotlin MultiPlatform library that allows you to run JavaScript code from common Kotlin code Table of Contents Featu

IceRock Development 14 Aug 29, 2022
Lambda-snake.kt - Snake Game Implementation for Web using Kotlin programming language compiled for Javascript

Projeto da disciplina de Linguagem de Programaรงรฃo Funcional 2021.1 (jan/2022) ??

Alex Candido 3 Jan 10, 2022
๐Ÿš€ Native iOS- and Android- Apps with JavaScript

Titanium Welcome to the Titanium open source project. Titanium provides a mature platform for developers to build completely native cross-platform mob

Team Appcelerator 2.6k Jan 4, 2023
Uproot-JS - Extract JavaScript files from burp suite project with ease

Extract JavaScript files from burp suite project with ease. Disclaimer I am not

Dexter0us 50 Aug 8, 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
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
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