feat(serialize-dsl): port serialize-gson-dsl to commons-serialization
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-04-13 14:21:28 +02:00
parent 1580c3f98d
commit 42a9c78709
Signed by: Johannes
GPG Key ID: E76429612C2929F4
9 changed files with 77 additions and 79 deletions

View File

@ -6,16 +6,17 @@ plugins {
}
dependencies {
implementation(projects.commonsGson)
api(projects.commonsSerialize)
testImplementation(kotlin("test"))
testImplementation(projects.commonsSerializeJson)
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "io.gitlab.jfronny"
artifactId = "commons-gson-dsl"
artifactId = "commons-serialize-dsl"
from(components["java"])
}
@ -30,6 +31,4 @@ tasks.javadoc {
enabled = false
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons/$version/raw", projects.commons)
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons-serialize/$version/raw", projects.commonsSerialize)
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons-gson/$version/raw", projects.commonsGson)
//TODO link gson javadoc (harder to generate than expected)
}

View File

@ -0,0 +1,5 @@
module io.gitlab.jfronny.commons.serialize.dsl {
requires io.gitlab.jfronny.commons.serialize;
requires kotlin.stdlib;
exports io.gitlab.jfronny.commons.serialize.dsl;
}

View File

@ -1,15 +1,15 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
package io.gitlab.jfronny.commons.serialize.dsl
import io.gitlab.jfronny.gson.stream.JsonWriter
import io.gitlab.jfronny.commons.serialize.SerializeWriter
class ArrayScope(val writer: JsonWriter) {
fun jObject(content: (@JsonDsl ObjectScope).() -> Unit) {
class ArrayScope<TEx : Throwable, Writer: SerializeWriter<TEx, Writer>>(val writer: Writer) {
fun jObject(content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) {
writer.beginObject()
ObjectScope(writer).content()
writer.endObject()
}
fun jArray(content: (@JsonDsl ArrayScope).() -> Unit) {
fun jArray(content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) {
writer.beginArray()
content()
writer.endArray()

View File

@ -0,0 +1,55 @@
package io.gitlab.jfronny.commons.serialize.dsl
import io.gitlab.jfronny.commons.serialize.SerializeReader
import io.gitlab.jfronny.commons.serialize.SerializeWriter
import io.gitlab.jfronny.commons.serialize.Transport
import io.gitlab.jfronny.commons.serialize.emulated.DataElement
import io.gitlab.jfronny.commons.serialize.emulated.EmulatedWriter
import java.io.OutputStream
import java.io.StringWriter
import java.io.Writer as IOWriter
import java.nio.file.Path
import kotlin.io.path.writer
@DslMarker
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class JsonDsl
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> Path.jObject(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) = openJson(transport) { jObject(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> Path.jArray(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) = openJson(transport) { jArray(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> IOWriter.jObject(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) = openJson(transport) { jObject(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> IOWriter.jArray(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) = openJson(transport) { jArray(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> OutputStream.jObject(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) = openJson(transport) { jObject(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> OutputStream.jArray(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) = openJson(transport) { jArray(content) }
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> jObjectString(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit): String = StringWriter().use {
it.jObject(transport, content)
it.toString()
}
fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> jArrayString(transport: Transport<TEx, Reader, Writer>, content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit): String = StringWriter().use {
it.jArray(transport, content)
it.toString()
}
fun jObjectTree(content: (@JsonDsl ObjectScope<RuntimeException, EmulatedWriter>).() -> Unit): DataElement.Object = EmulatedWriter().use {
it.jObject(content)
it.get()
} as DataElement.Object
fun jArrayTree(content: (@JsonDsl ArrayScope<RuntimeException, EmulatedWriter>).() -> Unit): DataElement.Array = EmulatedWriter().use {
it.jArray(content)
it.get()
} as DataElement.Array
fun <TEx : Throwable, Writer : SerializeWriter<TEx, Writer>> Writer.jObject(content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) {
beginObject()
ObjectScope(this).content()
endObject()
}
fun <TEx : Throwable, Writer : SerializeWriter<TEx, Writer>> Writer.jArray(content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) {
beginArray()
ArrayScope(this).content()
endArray()
}
private fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> Path.openJson(transport: Transport<TEx, Reader, Writer>, write: Writer.() -> Unit) = writer().use { it.openJson(transport, write) }
private fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> IOWriter.openJson(transport: Transport<TEx, Reader, Writer>, write: Writer.() -> Unit) = transport.createWriter(this).write()
private fun <TEx : Throwable, Reader : SerializeReader<TEx, Reader>, Writer : SerializeWriter<TEx, Writer>> OutputStream.openJson(transport: Transport<TEx, Reader, Writer>, write: Writer.() -> Unit) = writer().use { it.openJson(transport, write) }

View File

@ -1,16 +1,16 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
package io.gitlab.jfronny.commons.serialize.dsl
import io.gitlab.jfronny.gson.stream.JsonWriter
import io.gitlab.jfronny.commons.serialize.SerializeWriter
class ObjectScope(val writer: JsonWriter) {
fun jObject(name: String, content: (@JsonDsl ObjectScope).() -> Unit) {
class ObjectScope<TEx : Throwable, Writer : SerializeWriter<TEx, Writer>>(val writer: Writer) {
fun jObject(name: String, content: (@JsonDsl ObjectScope<TEx, Writer>).() -> Unit) {
writer.name(name)
writer.beginObject()
content()
writer.endObject()
}
fun jArray(name: String, content: (@JsonDsl ArrayScope).() -> Unit) {
fun jArray(name: String, content: (@JsonDsl ArrayScope<TEx, Writer>).() -> Unit) {
writer.name(name)
writer.beginArray()
ArrayScope(writer).content()

View File

@ -1,15 +1,14 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl.test
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders
import io.gitlab.jfronny.commons.serialize.gson.dsl.jObjectString
import io.gitlab.jfronny.commons.serialize.dsl.jObjectString
import io.gitlab.jfronny.commons.serialize.json.JsonTransport
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
class DslTest {
@Test
fun simpleTest() {
assertEquals("""{"hi":[["A",12],{"somEVal":true}]}""", jObjectString(GsonHolders.API.gson) {
assertEquals("""{"hi":[["A",12],{"somEVal":true}]}""", jObjectString(JsonTransport()) {
jArray("hi") {
jArray {
jValue("A")

View File

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

View File

@ -1,55 +0,0 @@
package io.gitlab.jfronny.commons.serialize.gson.dsl
import io.gitlab.jfronny.gson.Gson
import io.gitlab.jfronny.gson.JsonArray
import io.gitlab.jfronny.gson.JsonObject
import io.gitlab.jfronny.gson.stream.JsonTreeWriter
import io.gitlab.jfronny.gson.stream.JsonWriter
import java.io.OutputStream
import java.io.StringWriter
import java.io.Writer
import java.nio.file.Path
import kotlin.io.path.writer
@DslMarker
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class JsonDsl
fun Path.jObject(gson: Gson, content: (@JsonDsl ObjectScope).() -> Unit) = openJson(gson) { jObject(content) }
fun Path.jArray(gson: Gson, content: (@JsonDsl ArrayScope).() -> Unit) = openJson(gson) { jArray(content) }
fun Writer.jObject(gson: Gson, content: (@JsonDsl ObjectScope).() -> Unit) = openJson(gson) { jObject(content) }
fun Writer.jArray(gson: Gson, content: (@JsonDsl ArrayScope).() -> Unit) = openJson(gson) { jArray(content) }
fun OutputStream.jObject(gson: Gson, content: (@JsonDsl ObjectScope).() -> Unit) = openJson(gson) { jObject(content) }
fun OutputStream.jArray(gson: Gson, content: (@JsonDsl ArrayScope).() -> Unit) = openJson(gson) { jArray(content) }
fun jObjectString(gson: Gson, content: (@JsonDsl ObjectScope).() -> Unit): String = StringWriter().use {
it.jObject(gson, content)
it.toString()
}
fun jArrayString(gson: Gson, content: (@JsonDsl ArrayScope).() -> Unit): String = StringWriter().use {
it.jArray(gson, content)
it.toString()
}
fun jObjectTree(content: (@JsonDsl ObjectScope).() -> Unit): JsonObject = JsonTreeWriter().use {
it.jObject(content)
it.get()
}.asJsonObject
fun jArrayTree(content: (@JsonDsl ArrayScope).() -> Unit): JsonArray = JsonTreeWriter().use {
it.jArray(content)
it.get()
}.asJsonArray
fun JsonWriter.jObject(content: (@JsonDsl ObjectScope).() -> Unit) {
beginObject()
ObjectScope(this).content()
endObject()
}
fun JsonWriter.jArray(content: (@JsonDsl 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

@ -6,7 +6,7 @@ include("commons-http-client")
include("commons-http-server")
// serialization
include("commons-serialize")
include("commons-serialize-gson-dsl")
include("commons-serialize-dsl")
include("commons-serialize-json")
include("commons-serialize-databind")
include("commons-serialize-databind-api")