Skip to content

Commit

Permalink
support inline class json serialization #253
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 2, 2021
1 parent 67bf796 commit 56a296c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class EntityImplementation(
"flushChanges" -> this.doFlushChanges()
"discardChanges" -> this.doDiscardChanges()
"delete" -> this.doDelete()
"get" -> this.doGetProperty(args!![0] as String)
"get" -> this.values[args!![0] as String]
"set" -> this.doSetProperty(args!![0] as String, args[1])
"copy" -> this.copy()
else -> throw IllegalStateException("Unrecognized method: $method")
Expand Down Expand Up @@ -156,11 +156,11 @@ internal class EntityImplementation(
@OptIn(ExperimentalUnsignedTypes::class)
fun getProperty(prop: KProperty1<*, *>, unboxInlineValues: Boolean = false): Any? {
if (!unboxInlineValues) {
return doGetProperty(prop.name)
return values[prop.name]
}

val returnType = prop.javaGetter!!.returnType
val value = doGetProperty(prop.name)
val value = values[prop.name]

// Unbox inline class values if necessary.
// In principle, we need to check for all inline classes, but kotlin-reflect is still unable to determine
Expand All @@ -178,10 +178,6 @@ internal class EntityImplementation(
}
}

private fun doGetProperty(name: String): Any? {
return values[name]
}

@OptIn(ExperimentalUnsignedTypes::class)
fun setProperty(prop: KProperty1<*, *>, value: Any?, forceSet: Boolean = false) {
val propType = prop.returnType.jvmErasure
Expand Down
6 changes: 3 additions & 3 deletions ktorm-jackson/ktorm-jackson.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ compileKotlin.dependsOn(generatePackageVersion)

dependencies {
api project(":ktorm-core")
api "com.fasterxml.jackson.core:jackson-databind:2.9.7"
api "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7"
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.7"
api "com.fasterxml.jackson.core:jackson-databind:2.12.3"
api "com.fasterxml.jackson.module:jackson-module-kotlin:2.12.3"
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.3"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.javaGetter

/**
* Created by vince on Aug 13, 2018.
Expand Down Expand Up @@ -97,7 +96,7 @@ internal class EntityDeserializers : SimpleDeserializers() {
parser.nextToken() // skip to field value

if (prop != null) {
val propType = ctxt.constructType(prop.javaGetter!!.genericReturnType)
val propType = ctxt.constructType(prop.getPropertyType())
intoValue[prop.name] = parser.codec.readValue(parser, propType)
} else {
if (parser.currentToken.isStructStart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.fasterxml.jackson.databind.module.SimpleSerializers
import org.ktorm.entity.Entity
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.javaGetter

/**
* Created by vince on Aug 13, 2018.
Expand Down Expand Up @@ -92,13 +91,15 @@ internal class EntitySerializers : SimpleSerializers() {
if (value == null) {
gen.writeNull()
} else {
val propType = serializers.constructType(prop.javaGetter!!.genericReturnType)
val propType = serializers.constructType(prop.getPropertyType())
val ser = serializers.findTypedValueSerializer(propType, true, null)
ser.serialize(value, gen, serializers)
}
}
}



override fun serializeWithType(
entity: Entity<*>,
gen: JsonGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import com.fasterxml.jackson.databind.cfg.MapperConfig
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import kotlin.reflect.KMutableProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaSetter
import kotlin.reflect.jvm.*

internal fun JsonGenerator.configureIndentOutputIfEnabled() {
val codec = this.codec
Expand Down Expand Up @@ -112,3 +110,18 @@ internal inline fun <reified T : Annotation> KProperty1<*, *>.findAnnotationForD

return annotation
}

@OptIn(ExperimentalUnsignedTypes::class)
internal fun KProperty1<*, *>.getPropertyType(): java.lang.reflect.Type {
return when (returnType.jvmErasure) {
UByte::class -> UByte::class.java
UShort::class -> UShort::class.java
UInt::class -> UInt::class.java
ULong::class -> ULong::class.java
UByteArray::class -> UByteArray::class.java
UShortArray::class -> UShortArray::class.java
UIntArray::class -> UIntArray::class.java
ULongArray::class -> ULongArray::class.java
else -> returnType.javaType
}
}
20 changes: 16 additions & 4 deletions ktorm-jackson/src/test/kotlin/org/ktorm/jackson/JacksonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import java.math.BigInteger
/**
* Created by vince on Dec 09, 2018.
*/
@ExperimentalUnsignedTypes
class JacksonTest {

private val objectMapper = ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true)
.findAndRegisterModules()

private val typedObjectMapper = ObjectMapper()
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
.configure(SerializationFeature.INDENT_OUTPUT, true)
.findAndRegisterModules()
private val typedObjectMapper = ObjectMapper().apply {
activateDefaultTyping(polymorphicTypeValidator, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
configure(SerializationFeature.INDENT_OUTPUT, true)
findAndRegisterModules()
}

private val separator = "$"

Expand All @@ -30,6 +32,8 @@ class JacksonTest {
short = 2
int = 3
long = 4
ulong = 123U
ulong0 = 1230U
float = 5.0F
double = 6.0
bigInteger = BigInteger("7")
Expand Down Expand Up @@ -93,6 +97,8 @@ class JacksonTest {
assert(f.short == foo.short)
assert(f.int == foo.int)
assert(f.long == foo.long)
assert(f.ulong == foo.ulong)
assert(f.ulong0 == foo.ulong0)
assert(f.float == foo.float)
assert(f.double == foo.double)
assert(f.bigInteger == foo.bigInteger)
Expand Down Expand Up @@ -135,6 +141,8 @@ class JacksonTest {
"short" : 2,
"int" : 3,
"long" : 4,
"ulong" : 123,
"ulong0" : 1230,
"float" : 5.0,
"double" : 6.0,
"bigInteger" : 7,
Expand Down Expand Up @@ -199,6 +207,8 @@ class JacksonTest {
"short" : 2,
"int" : 3,
"long" : 4,
"ulong" : 123,
"ulong0" : 1230,
"float" : 5.0,
"double" : 6.0,
"bigInteger" : [ "java.math.BigInteger", 7 ],
Expand Down Expand Up @@ -333,6 +343,8 @@ class JacksonTest {
var short: Short
var int: Int
var long: Long
var ulong: ULong
var ulong0: ULong?
var float: Float
var double: Double
var bigInteger: BigInteger
Expand Down

0 comments on commit 56a296c

Please sign in to comment.