https://github.com/zsqw123/kson
A lightweight Kotlin DSL to render DSL style Json to String.
https://github.com/zsqw123/kson
dsl json kotlin serialization
Last synced: 2 months ago
JSON representation
A lightweight Kotlin DSL to render DSL style Json to String.
- Host: GitHub
- URL: https://github.com/zsqw123/kson
- Owner: zsqw123
- License: apache-2.0
- Created: 2021-05-03T16:22:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-05T08:06:51.000Z (about 5 years ago)
- Last Synced: 2023-07-04T12:41:45.775Z (almost 3 years ago)
- Topics: dsl, json, kotlin, serialization
- Language: Kotlin
- Homepage:
- Size: 104 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
## Kson
[`简体中文 zh-CN`](./READMECN.MD)
[](https://search.maven.org/artifact/io.github.zsqw123/kson)
A lightweight `Kotlin DSL` to render DSL style `Json` to `String`.
Code: [Kson.kt](https://github.com/zsqw123/Kson/blob/master/src/main/kotlin/Kson.kt)
### DSL
```kotlin
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
```json
{}
{"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`.
```kotlin
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:
```json
{
"awa":1,
"QwQ":false,
"qwq":[
3,
4
]
}
```
### Use
[](https://search.maven.org/artifact/io.github.zsqw123/kson)
```groovy
implementation 'io.github.zsqw123:kson:$latest_version'
```
### Benchmark
Benchmarks have been conducted with the [jmh](https://openjdk.java.net/projects/code-tools/jmh/) OpenJDK tool. Benchmark can be found in [BenchMark.kt](https://github.com/zsqw123/Kson/blob/master/src/test/kotlin/BenchMark.kt)
`Kson` was put side to side with one of the most popular JSON builder for `Java` : [JSON-java](https://github.com/stleary/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](https://github.com/lectra-tech/koson), Only reference its api, But the implementation is not same.
2. Why use `-` ?? Look this:
