Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow StdSerializers to output primitives and array structures. #49

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions konf-core/src/main/kotlin/com/uchuhimo/konf/source/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,9 @@ internal fun Any?.toCompatibleValue(mapper: ObjectMapper): Any {
is LongArray -> this.toList()
is DoubleArray -> this.toList()
is FloatArray -> this.toList()
is List<*> -> this.map { it!!.toCompatibleValue(mapper) }
is Set<*> -> this.map { it!!.toCompatibleValue(mapper) }
is Array<*> -> this.map { it!!.toCompatibleValue(mapper) }
is Map<*, *> -> this.mapValues { (_, value) -> value!!.toCompatibleValue(mapper) }
is Iterable<*> -> this.map { it.toCompatibleValue(mapper) }
is Array<*> -> this.map { it.toCompatibleValue(mapper) }
is Map<*, *> -> this.mapValues { (_, value) -> value.toCompatibleValue(mapper) }
is Char -> this.toString()
is String,
is Boolean,
Expand All @@ -495,10 +494,7 @@ internal fun Any?.toCompatibleValue(mapper: ObjectMapper): Any {
if (this == null) {
"null"
} else {
val result = mapper.convertValue(this, Map::class.java).mapValues { (_, value) ->
value.toCompatibleValue(mapper)
}
result
mapper.convertValue(this, Any::class.java).toCompatibleValue(mapper)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2017-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.uchuhimo.konf.source.serializer

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.uchuhimo.konf.Config
import com.uchuhimo.konf.ConfigSpec
import com.uchuhimo.konf.source.json.toJson
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.on
import org.junit.jupiter.api.assertDoesNotThrow
import java.io.StringReader
import java.io.StringWriter
import kotlin.test.assertEquals

object PrimitiveStdSerializerSpec : Spek({
val config = Config {
addSpec(WrappedStringSpec)
mapper.registerModule(SimpleModule().apply {
addSerializer(WrappedString::class.java, WrappedStringStdSerializer())
addDeserializer(WrappedString::class.java, WrappedStringStdDeserializer())
})
}

given("a config") {
on("write json") {
val writer = StringWriter()
assertDoesNotThrow {
config.toJson.toWriter(writer)
}
assert(writer.toString().contains("\"wrapped-string\" : \"value\""))
}

val json = """
{
"wrapped-string" : "1234"
}
""".trimIndent()
given("json") {
on("read json") {
val read = config.from.json.reader(StringReader(json))
assertEquals(WrappedString("1234"), read[WrappedStringSpec.wrappedString])
}
}
}

})

private object WrappedStringSpec : ConfigSpec("") {

val wrappedString by optional(name = "wrapped-string", default = WrappedString("value"))
}

private class WrappedStringStdSerializer : StdSerializer<WrappedString>(WrappedString::class.java) {

override fun serialize(value: WrappedString, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeString(value.string)
}
}

private class WrappedStringStdDeserializer : StdDeserializer<WrappedString>(WrappedString::class.java) {

override fun deserialize(p: JsonParser, ctxt: DeserializationContext): WrappedString {
return WrappedString(p.valueAsString)
}
}

private data class WrappedString(val string: String)