testImplementation group: "junit", name: "junit", version: "4.13.2"
testImplementation group: "org.hamcrest", name: "hamcrest-library", version: "1.3"
testImplementation group: "org.mockito", name: "mockito-core", version: "5.11.0"
+ testImplementation group: "com.spotify", name: "hamcrest-jackson", version: "1.3.2"
}
task fatJar(type: Jar) {
--- /dev/null
+package net.pterodactylus.rhynodge.states
+
+import com.fasterxml.jackson.annotation.JsonProperty
+import com.fasterxml.jackson.databind.JsonNode
+
+class JsonState(@JsonProperty("json") val jsonNode: JsonNode) : AbstractState(true) {
+
+ override fun plainText() = jsonNode.toPrettyString()!!
+
+}
--- /dev/null
+package net.pterodactylus.rhynodge.states
+
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import com.spotify.hamcrest.jackson.JsonMatchers.jsonObject
+import org.hamcrest.MatcherAssert.assertThat
+import org.junit.Test
+import java.io.StringWriter
+
+class JsonStateTest {
+
+ @Test
+ fun `json state can be serialized`() {
+ val jsonState = JsonState(jsonObject)
+ StringWriter().use { stringWriter ->
+ objectMapper.writeValue(stringWriter, jsonState)
+ val deserializedState = objectMapper.readValue(stringWriter.toString(), AbstractState::class.java) as JsonState
+ assertThat(deserializedState.jsonNode, jsonObject(jsonObject))
+ }
+ }
+
+ private val jsonObject = objectMapper.createObjectNode()
+ .put("state", "foo")
+ .also {
+ it.putArray("list")
+ .add("value1")
+ .add("value2")
+ }.also {
+ it.putObject("object")
+ .put("foo", true)
+ .put("bar", "yes")
+ .put("baz", 1.5)
+ .putNull("qux")
+ }
+
+}
+
+private val objectMapper = jacksonObjectMapper()