Kotlin JSON Builder

A Kotlin typesafe builder for Jackson JsonNodes

License

License

Categories

Categories

Kotlin Languages JSON Data
GroupId

GroupId

codes.matteo
ArtifactId

ArtifactId

kotlin-json-builder
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Kotlin JSON Builder
A Kotlin typesafe builder for Jackson JsonNodes
Project URL

Project URL

https://github.com/matteobanerjee/kotlin-json-builder
Source Code Management

Source Code Management

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

Download kotlin-json-builder

How to add to project

<!-- 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"]

Dependencies

compile (4)

Group / Artifact Type Version
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)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.0