Kson

A lightweight Kotlin DSL to render DSL style Json to String.

Лицензия

Лицензия

Группа

Группа

io.github.zsqw123
Идентификатор

Идентификатор

kson
Последняя версия

Последняя версия

1.1
Дата

Дата

Тип

Тип

pom.sha512
Описание

Описание

Kson
A lightweight Kotlin DSL to render DSL style Json to String.
Ссылка на сайт

Ссылка на сайт

https://github.com/zsqw123/Kson/
Система контроля версий

Система контроля версий

https://github.com/zsqw123/Kson/

Скачать kson

Имя Файла Размер
kson-1.1.pom
kson-1.1-sources.jar 1 KB
kson-1.1-javadoc.jar 261 bytes
Обзор

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.32

Модули Проекта

Данный проект не имеет модулей.

Kson

简体中文 zh-CN

Maven Central

A lightweight Kotlin DSL to render DSL style Json to String.

Code: Kson.kt

DSL

obj{} // empty object
obj { // object contains key-value
    "awa" - 1
    "QwQ" - false
    "\"raw\":\"aaaa\"".raw() // raw json is allowed
}
arr // empty array
arr[1, "s", null] // array with elements
arr[listOf("awa",null,1,false)] // it can provided by any collections

// you can combine these elements whatever you want
arr[
    1, 
    2,
    obj {
        "awa" - 1
        "QwQ" - false
        "qwq" - arr[
            3,
            4
        ]
    }
]

ouputs

{}
{"awa":1,"QwQ":false,"raw":"aaaa"}
[]
[1,"s",null]
["awa",null,1,false]
[1,2,{"awa":1,"QwQ":false,"qwq":[3,4]}]

Pretty outputs

Three methods: pobj, parr, pretty

When pass in the pobj, all children object obj, arr will be pretty.

When pass in the parr, only those who are directly related elements will be pretty, beacuse parr not change the namespace, but it is available to use pretty wrapper to pretty arr.

pobj {
    obj{
        "1" - arr[
            arr[
                obj{
                    // work well, it will pretty print all children
                }
            ]
        ]
    }
}

parr [
    1,
    "awa", // directly related child will be render
    arr[ // this arr will not pretty render
        pobj{
            // `pobj` change the namespace, so here can be pretty render
        }
    ]
]

pretty {
    arr [ // first arr in `pretty` will be pretty
        1,
        "awa",
        arr[
            // available!
        ]
    ]
}

output like this:

{
    "awa":1,
    "QwQ":false,
    "qwq":[
        3,
        4
    ]
}

Use

Maven Central

implementation 'io.github.zsqw123:kson:$latest_version'

Benchmark

Benchmarks have been conducted with the jmh OpenJDK tool. Benchmark can be found in BenchMark.kt

Kson was put side to side with one of the most popular JSON builder for Java : JSON-java

Testing environment : AMD Ryzen 7 4800U, 8 cores 16 threads, VM version: JDK 1.8.0_282, OpenJDK 64-Bit Server VM, 25.282-b08

Score in operations/second (throughput mode), higher is better

Benchmark Score Error Unit
jsonWithBigArray 15746.152 ± 524.543 ops/s
ksonWithBigArray 21958.445 ± 144.323 ops/s
jsonWithBigArray (Pretty) 7477.957 ± 115.563 ops/s
ksonWithBigArray (Pretty) 8568.294 ± 43.227 ops/s

Thanks

  1. This repository inspired by this repo lectra-tech/koson, Only reference its api, But the implementation is not same.
  2. Why use - ?? Look this:

Версии библиотеки

Версия
1.1
1.0