mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Use functions in Util where possible (#1444)
This commit is contained in:
@@ -135,7 +135,7 @@ internal class KotlinJsonAdapter<T>(
|
||||
for (binding in allBindings) {
|
||||
if (binding == null) continue // Skip constructor parameters that aren't properties.
|
||||
|
||||
writer.name(binding.name)
|
||||
writer.name(binding.jsonName)
|
||||
binding.adapter.toJson(writer, binding.get(value))
|
||||
}
|
||||
writer.endObject()
|
||||
@@ -144,8 +144,7 @@ internal class KotlinJsonAdapter<T>(
|
||||
override fun toString() = "KotlinJsonAdapter(${constructor.returnType})"
|
||||
|
||||
data class Binding<K, P>(
|
||||
val name: String,
|
||||
val jsonName: String?,
|
||||
val jsonName: String,
|
||||
val adapter: JsonAdapter<P>,
|
||||
val property: KProperty1<K, P>,
|
||||
val parameter: KParameter?,
|
||||
@@ -264,7 +263,7 @@ public class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
||||
|
||||
if (property !is KMutableProperty1 && parameter == null) continue
|
||||
|
||||
val name = jsonAnnotation?.name?.takeUnless { it == Json.UNSET_NAME } ?: property.name
|
||||
val jsonName = jsonAnnotation?.name?.takeUnless { it == Json.UNSET_NAME } ?: property.name
|
||||
val propertyType = when (val propertyTypeClassifier = property.returnType.classifier) {
|
||||
is KClass<*> -> {
|
||||
if (propertyTypeClassifier.isValue) {
|
||||
@@ -298,8 +297,7 @@ public class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
bindingsByName[property.name] = KotlinJsonAdapter.Binding(
|
||||
name,
|
||||
jsonAnnotation?.name?.takeUnless { it == Json.UNSET_NAME } ?: name,
|
||||
jsonName,
|
||||
adapter,
|
||||
property as KProperty1<Any, Any?>,
|
||||
parameter,
|
||||
@@ -323,7 +321,7 @@ public class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
||||
}
|
||||
|
||||
val nonIgnoredBindings = bindings.filterNotNull()
|
||||
val options = JsonReader.Options.of(*nonIgnoredBindings.map { it.name }.toTypedArray())
|
||||
val options = JsonReader.Options.of(*nonIgnoredBindings.map { it.jsonName }.toTypedArray())
|
||||
return KotlinJsonAdapter(constructor, bindings, nonIgnoredBindings, options).nullSafe()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user