Enhance DSL
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
c2fe59ae45
commit
ef507aad1b
|
@ -18,7 +18,7 @@ dependencies {
|
|||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.3")
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ plugins {
|
|||
|
||||
dependencies {
|
||||
implementation(project(":commons-gson"))
|
||||
|
||||
testImplementation(kotlin("test"))
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
@ -20,6 +22,10 @@ publishing {
|
|||
}
|
||||
}
|
||||
|
||||
tasks.compileKotlin {
|
||||
destinationDirectory = tasks.compileJava.get().destinationDir
|
||||
}
|
||||
|
||||
tasks.javadoc {
|
||||
enabled = false
|
||||
linksOffline("https://maven.frohnmeyer-wds.de/javadoc/artifacts/io/gitlab/jfronny/commons/$version/raw", project(":"))
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
module io.gitlab.jfronny.commons.gson.dsl {
|
||||
requires io.gitlab.jfronny.commons.gson;
|
||||
requires kotlin.stdlib;
|
||||
exports io.gitlab.jfronny.commons.serialize.gson.dsl;
|
||||
}
|
|
@ -3,51 +3,42 @@ 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) {
|
||||
fun jObject(content: (@JsonDsl ObjectScope).() -> Unit) {
|
||||
writer.beginObject()
|
||||
ObjectScope(writer).content()
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
@JsonDsl
|
||||
fun jArray(content: ArrayScope.() -> Unit) {
|
||||
fun jArray(content: (@JsonDsl ArrayScope).() -> Unit) {
|
||||
writer.beginArray()
|
||||
content()
|
||||
writer.endObject()
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
|
|
|
@ -1,31 +1,50 @@
|
|||
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
|
||||
|
||||
@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) }
|
||||
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
|
||||
|
||||
@JsonDsl
|
||||
fun JsonWriter.jObject(content: ObjectScope.() -> Unit) {
|
||||
fun JsonWriter.jObject(content: (@JsonDsl ObjectScope).() -> Unit) {
|
||||
beginObject()
|
||||
ObjectScope(this).content()
|
||||
endObject()
|
||||
}
|
||||
|
||||
@JsonDsl
|
||||
fun JsonWriter.jArray(content: ArrayScope.() -> Unit) {
|
||||
fun JsonWriter.jArray(content: (@JsonDsl ArrayScope).() -> Unit) {
|
||||
beginArray()
|
||||
ArrayScope(this).content()
|
||||
endArray()
|
||||
|
|
|
@ -3,59 +3,56 @@ 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) {
|
||||
fun jObject(name: String, content: (@JsonDsl ObjectScope).() -> Unit) {
|
||||
writer.name(name)
|
||||
writer.beginObject()
|
||||
content()
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
@JsonDsl
|
||||
fun jArray(name: String, content: ArrayScope.() -> Unit) {
|
||||
fun jArray(name: String, content: (@JsonDsl ArrayScope).() -> Unit) {
|
||||
writer.name(name)
|
||||
writer.beginArray()
|
||||
ArrayScope(writer).content()
|
||||
writer.endObject()
|
||||
writer.endArray()
|
||||
}
|
||||
|
||||
@JsonDsl
|
||||
operator fun String.invoke(value: String) = jValue(this, value)
|
||||
operator fun String.invoke(value: Boolean) = jValue(this, value)
|
||||
operator fun String.invoke(value: Float) = jValue(this, value)
|
||||
operator fun String.invoke(value: Double) = jValue(this, value)
|
||||
operator fun String.invoke(value: Long) = jValue(this, value)
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package io.gitlab.jfronny.commons.serialize.gson.dsl.test
|
||||
|
||||
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders
|
||||
import io.gitlab.jfronny.commons.serialize.gson.dsl.jObjectString
|
||||
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) {
|
||||
jArray("hi") {
|
||||
jArray {
|
||||
jValue("A")
|
||||
jValue(12)
|
||||
}
|
||||
jObject {
|
||||
"somEVal"(true)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ import java.util.Objects;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class GsonTest {
|
||||
class GsonTest {
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
GsonHolders.registerSerializer();
|
||||
|
|
Loading…
Reference in New Issue