Kotlin JSON Builder

A Kotlin typesafe builder for Jackson JsonNodes

Лицензия

Лицензия

Категории

Категории

Kotlin Языки программирования JSON Данные
Группа

Группа

codes.matteo
Идентификатор

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

kotlin-json-builder
Последняя версия

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

1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

Kotlin JSON Builder
A Kotlin typesafe builder for Jackson JsonNodes
Ссылка на сайт

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

https://github.com/matteobanerjee/kotlin-json-builder
Система контроля версий

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

https://github.com/matteobanerjee/kotlin-json-builder

Скачать kotlin-json-builder

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/codes.matteo/kotlin-json-builder/ -->
<dependency>
    <groupId>codes.matteo</groupId>
    <artifactId>kotlin-json-builder</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/codes.matteo/kotlin-json-builder/
implementation 'codes.matteo:kotlin-json-builder:1.0'
// https://jarcasting.com/artifacts/codes.matteo/kotlin-json-builder/
implementation ("codes.matteo:kotlin-json-builder:1.0")
'codes.matteo:kotlin-json-builder:jar:1.0'
<dependency org="codes.matteo" name="kotlin-json-builder" rev="1.0">
  <artifact name="kotlin-json-builder" type="jar" />
</dependency>
@Grapes(
@Grab(group='codes.matteo', module='kotlin-json-builder', version='1.0')
)
libraryDependencies += "codes.matteo" % "kotlin-json-builder" % "1.0"
[codes.matteo/kotlin-json-builder "1.0"]

Зависимости

compile (4)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.21
org.jetbrains.kotlin : kotlin-reflect jar 1.2.21
com.fasterxml.jackson.core : jackson-core jar 2.9.4
com.fasterxml.jackson.core : jackson-databind jar 2.9.4

test (1)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12

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

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

kotlin-json-builder

A reasonably fast, reasonably elegant Kotlin typesafe builder for Jackson JSON objects.

Usage

The DSL provides two functions: jsObject and jsArray. In addition to jsObject and jsArray, basic Kotlin types are supported: Boolean, String, Int, Long and Kotlin Arrays of those types. The DSL errs on the side of typesafety and doesn't allow potentially un-serializable objects to be created. Using Array is preferable over jsArray unless you need deeply nested arrays or mixed type arrays.

  • jsObject provides the /= operator to associate string keys to values
  • jsArray provides the elem method to add elements.

That's it! Here is an example:

val obj = jsObject {
        // nested object
        "data" /= jsObject {
            "id" /= 1
            "name" /= "Joe"
            // native array
            "tags" /= arrayOf("a", "b")
            // jsArray
            "nested_array" /= jsArray { elem(jsArray { elem(1) }) }
        }
      }
 
val arr = jsArray {
  elem(1)
  elem("something")
  elem(obj)
} 

// call `asTree()` to get the JsonNode representation
// a trivial jackson module could be added in the future to us to skip this
val serialized = ObjectMapper().writeValueAsString(obj.asTree())

Benchmarks

This project includes basic JMH benchmarks for serialization. These compare using the DSL to data class databinding and various methods of constructing dynamic JSON objects. The aim is to ensure that DSL performance stays in the same ballpark as databinding and hand-written Jackson ObjectNode code.

To run benchmarks:

./gradlew clean jmh

# if you get errors, you might need to remove the gradle cache:
# rm -r ~/.gradle

Latest results

Benchmark                    Mode  Cnt        Score       Error  Units
BenchKotlin.dataClass       thrpt   20  2532032.245 ± 34718.349  ops/s
BenchKotlin.dsl             thrpt   20  1514419.037 ± 26120.672  ops/s
BenchKotlin.handWritten     thrpt   20  1560949.008 ± 12864.029  ops/s
BenchKotlin.mapOf           thrpt   20  1413689.265 ± 10756.033  ops/s
BenchKotlin.stringInterpol  thrpt   20   106053.933 ±   499.961  ops/s

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

Версия
1.0