Kotlin DSL for gson
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-07-06 15:05:35 +02:00
parent 486f241eca
commit c2fe59ae45
Signed by: Johannes
GPG Key ID: E76429612C2929F4
7 changed files with 186 additions and 1 deletions

View File

@ -0,0 +1,28 @@
import io.gitlab.jfronny.scripts.*
plugins {
id("commons.library")
kotlin("jvm")
}
dependencies {
implementation(project(":commons-gson"))
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "io.gitlab.jfronny"
artifactId = "commons-gson-dsl"
from(components["java"])
}
}
}
tasks.javadoc {
enabled = false
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons/$version/raw", project(":"))
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons-gson/$version/raw", project(":commons-gson"))
//TODO link gson javadoc (harder to generate than expected)
}

View File

@ -0,0 +1,4 @@
module io.gitlab.jfronny.commons.gson.dsl {
requires io.gitlab.jfronny.commons.gson;
requires kotlin.stdlib;
}

View File

@ -0,0 +1,54 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
import io.gitlab.jfronny.gson.stream.JsonWriter
class ArrayScope(val writer: JsonWriter) {
@JsonDsl
fun jObject(content: ObjectScope.() -> Unit) {
writer.beginObject()
ObjectScope(writer).content()
writer.endObject()
}
@JsonDsl
fun jArray(content: ArrayScope.() -> Unit) {
writer.beginArray()
content()
writer.endObject()
}
@JsonDsl
fun jValue(value: String) {
writer.value(value)
}
@JsonDsl
fun jValue(value: Boolean) {
writer.value(value)
}
@JsonDsl
fun jValue(value: Float) {
writer.value(value)
}
@JsonDsl
fun jValue(value: Double) {
writer.value(value)
}
@JsonDsl
fun jValue(value: Long) {
writer.value(value)
}
@JsonDsl
fun jNull() {
writer.nullValue()
}
@JsonDsl
fun jComment(comment: String) {
writer.comment(comment)
}
}

View File

@ -0,0 +1,36 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
import io.gitlab.jfronny.gson.Gson
import io.gitlab.jfronny.gson.stream.JsonWriter
import java.io.OutputStream
import java.io.Writer
import java.nio.file.Path
import kotlin.io.path.writer
@DslMarker
annotation class JsonDsl
@JsonDsl fun Path.jObject(gson: Gson, content: ObjectScope.() -> Unit) = openJson(gson) { jObject(content) }
@JsonDsl fun Path.jArray(gson: Gson, content: ArrayScope.() -> Unit) = openJson(gson) { jArray(content) }
@JsonDsl fun Writer.jObject(gson: Gson, content: ObjectScope.() -> Unit) = openJson(gson) { jObject(content) }
@JsonDsl fun Writer.jArray(gson: Gson, content: ArrayScope.() -> Unit) = openJson(gson) { jArray(content) }
@JsonDsl fun OutputStream.jObject(gson: Gson, content: ObjectScope.() -> Unit) = openJson(gson) { jObject(content) }
@JsonDsl fun OutputStream.jArray(gson: Gson, content: ArrayScope.() -> Unit) = openJson(gson) { jArray(content) }
@JsonDsl
fun JsonWriter.jObject(content: ObjectScope.() -> Unit) {
beginObject()
ObjectScope(this).content()
endObject()
}
@JsonDsl
fun JsonWriter.jArray(content: ArrayScope.() -> Unit) {
beginArray()
ArrayScope(this).content()
endArray()
}
private fun Path.openJson(gson: Gson, write: JsonWriter.() -> Unit) = writer().use { it.openJson(gson, write) }
private fun Writer.openJson(gson: Gson, write: JsonWriter.() -> Unit) = gson.newJsonWriter(this).use(write)
private fun OutputStream.openJson(gson: Gson, write: JsonWriter.() -> Unit) = writer().use { it.openJson(gson, write) }

View File

@ -0,0 +1,62 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
import io.gitlab.jfronny.gson.stream.JsonWriter
class ObjectScope(val writer: JsonWriter) {
@JsonDsl
fun jObject(name: String, content: ObjectScope.() -> Unit) {
writer.name(name)
writer.beginObject()
content()
writer.endObject()
}
@JsonDsl
fun jArray(name: String, content: ArrayScope.() -> Unit) {
writer.name(name)
writer.beginArray()
ArrayScope(writer).content()
writer.endObject()
}
@JsonDsl
fun jValue(name: String, value: String) {
writer.name(name)
writer.value(value)
}
@JsonDsl
fun jValue(name: String, value: Boolean) {
writer.name(name)
writer.value(value)
}
@JsonDsl
fun jValue(name: String, value: Float) {
writer.name(name)
writer.value(value)
}
@JsonDsl
fun jValue(name: String, value: Double) {
writer.name(name)
writer.value(value)
}
@JsonDsl
fun jValue(name: String, value: Long) {
writer.name(name)
writer.value(value)
}
@JsonDsl
fun jNull(name: String) {
writer.name(name)
writer.nullValue()
}
@JsonDsl
fun jComment(comment: String) {
writer.comment(comment)
}
}

View File

@ -1,6 +1,6 @@
module io.gitlab.jfronny.commons.gson {
requires io.gitlab.jfronny.commons;
requires io.gitlab.jfronny.gson;
requires transitive io.gitlab.jfronny.gson;
requires static org.jetbrains.annotations;
exports io.gitlab.jfronny.commons.serialize.gson.api.v1;
}

View File

@ -1,6 +1,7 @@
rootProject.name = "Commons"
include("commons-gson")
include("commons-gson-dsl")
include("commons-jlhttp")
include("commons-manifold")
include("commons-slf4j")