mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Improve KotlinJsonAdapter performance by invoke KFunction by "call". (#1286)
This commit is contained in:
@@ -94,21 +94,27 @@ internal class KotlinJsonAdapter<T>(
|
|||||||
reader.endObject()
|
reader.endObject()
|
||||||
|
|
||||||
// Confirm all parameters are present, optional, or nullable.
|
// Confirm all parameters are present, optional, or nullable.
|
||||||
|
var isFullInitialized = allBindings.size == constructorSize
|
||||||
for (i in 0 until constructorSize) {
|
for (i in 0 until constructorSize) {
|
||||||
if (values[i] === ABSENT_VALUE && !constructor.parameters[i].isOptional) {
|
if (values[i] === ABSENT_VALUE) {
|
||||||
if (!constructor.parameters[i].type.isMarkedNullable) {
|
when {
|
||||||
throw Util.missingProperty(
|
constructor.parameters[i].isOptional -> isFullInitialized = false
|
||||||
|
constructor.parameters[i].type.isMarkedNullable -> values[i] = null // Replace absent with null.
|
||||||
|
else -> throw Util.missingProperty(
|
||||||
constructor.parameters[i].name,
|
constructor.parameters[i].name,
|
||||||
allBindings[i]?.jsonName,
|
allBindings[i]?.jsonName,
|
||||||
reader
|
reader
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
values[i] = null // Replace absent with null.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the constructor using a Map so that absent optionals get defaults.
|
// Call the constructor using a Map so that absent optionals get defaults.
|
||||||
val result = constructor.callBy(IndexedParameterMap(constructor.parameters, values))
|
val result = if (isFullInitialized) {
|
||||||
|
constructor.call(*values)
|
||||||
|
} else {
|
||||||
|
constructor.callBy(IndexedParameterMap(constructor.parameters, values))
|
||||||
|
}
|
||||||
|
|
||||||
// Set remaining properties.
|
// Set remaining properties.
|
||||||
for (i in constructorSize until allBindings.size) {
|
for (i in constructorSize until allBindings.size) {
|
||||||
|
Reference in New Issue
Block a user