mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Inline Moshi extension functions to Moshi itself (#1496)
* Move inline extensions to Moshi(.Builder) directly No need for the separation and it allows removing of the import * Remove now-redundant imports
This commit is contained in:
@@ -20,7 +20,6 @@ package com.squareup.moshi.kotlin.codegen
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Test
|
||||
|
||||
|
@@ -17,7 +17,6 @@ package com.squareup.moshi.kotlin.codegen
|
||||
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultConstructorTest {
|
||||
|
@@ -26,7 +26,6 @@ import com.squareup.moshi.JsonReader
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.adapter
|
||||
import com.squareup.moshi.internal.NullSafeJsonAdapter
|
||||
import com.squareup.moshi.kotlin.codegen.annotation.UppercaseInAnnotationPackage
|
||||
import com.squareup.moshi.kotlin.codegen.annotation.UppercaseInAnnotationPackageJsonAdapter
|
||||
|
@@ -18,7 +18,6 @@ package com.squareup.moshi.kotlin.codegen
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import org.junit.Test
|
||||
|
||||
|
@@ -18,7 +18,6 @@ package com.squareup.moshi.kotlin.codegen
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import org.junit.Test
|
||||
|
||||
// Regression tests specific to Moshi-KSP
|
||||
|
@@ -17,7 +17,6 @@ package com.squareup.moshi.kotlin.codegen
|
||||
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
@@ -23,7 +23,6 @@ import com.squareup.moshi.JsonDataException
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.Types
|
||||
import com.squareup.moshi.adapter
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert.fail
|
||||
|
@@ -26,7 +26,6 @@ import com.squareup.moshi.JsonReader
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.adapter
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Test
|
||||
|
@@ -13,6 +13,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
|
||||
package com.squareup.moshi
|
||||
|
||||
import com.squareup.moshi.internal.NonNullJsonAdapter
|
||||
@@ -25,9 +27,11 @@ import kotlin.reflect.typeOf
|
||||
* @return a [JsonAdapter] for [T], creating it if necessary. Note that while nullability of [T]
|
||||
* itself is handled, nested types (such as in generics) are not resolved.
|
||||
*/
|
||||
@Deprecated("Use the Moshi instance version instead", level = DeprecationLevel.HIDDEN)
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <reified T> Moshi.adapter(): JsonAdapter<T> = adapter(typeOf<T>())
|
||||
|
||||
@Deprecated("Use the Moshi instance version instead", level = DeprecationLevel.HIDDEN)
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <reified T> Moshi.Builder.addAdapter(adapter: JsonAdapter<T>): Moshi.Builder = add(typeOf<T>().javaType, adapter)
|
||||
|
||||
@@ -35,6 +39,7 @@ public inline fun <reified T> Moshi.Builder.addAdapter(adapter: JsonAdapter<T>):
|
||||
* @return a [JsonAdapter] for [ktype], creating it if necessary. Note that while nullability of
|
||||
* [ktype] itself is handled, nested types (such as in generics) are not resolved.
|
||||
*/
|
||||
@Deprecated("Use the Moshi instance version instead", level = DeprecationLevel.HIDDEN)
|
||||
@ExperimentalStdlibApi
|
||||
public fun <T> Moshi.adapter(ktype: KType): JsonAdapter<T> {
|
||||
val adapter = adapter<T>(ktype.javaType)
|
||||
|
@@ -17,6 +17,8 @@ package com.squareup.moshi
|
||||
|
||||
import com.squareup.moshi.Types.createJsonQualifierImplementation
|
||||
import com.squareup.moshi.internal.NO_ANNOTATIONS
|
||||
import com.squareup.moshi.internal.NonNullJsonAdapter
|
||||
import com.squareup.moshi.internal.NullSafeJsonAdapter
|
||||
import com.squareup.moshi.internal.canonicalize
|
||||
import com.squareup.moshi.internal.isAnnotationPresent
|
||||
import com.squareup.moshi.internal.removeSubtypeWildcard
|
||||
@@ -24,6 +26,9 @@ import com.squareup.moshi.internal.toStringWithAnnotations
|
||||
import com.squareup.moshi.internal.typesMatch
|
||||
import java.lang.reflect.Type
|
||||
import javax.annotation.CheckReturnValue
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.javaType
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
/**
|
||||
* Coordinates binding between JSON values and Java objects.
|
||||
@@ -68,6 +73,32 @@ public class Moshi internal constructor(builder: Builder) {
|
||||
public fun <T> adapter(type: Type, annotations: Set<Annotation>): JsonAdapter<T> =
|
||||
adapter(type, annotations, fieldName = null)
|
||||
|
||||
/**
|
||||
* @return a [JsonAdapter] for [T], creating it if necessary. Note that while nullability of [T]
|
||||
* itself is handled, nested types (such as in generics) are not resolved.
|
||||
*/
|
||||
@CheckReturnValue
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <reified T> adapter(): JsonAdapter<T> = adapter(typeOf<T>())
|
||||
|
||||
/**
|
||||
* @return a [JsonAdapter] for [ktype], creating it if necessary. Note that while nullability of
|
||||
* [ktype] itself is handled, nested types (such as in generics) are not resolved.
|
||||
*/
|
||||
@CheckReturnValue
|
||||
@ExperimentalStdlibApi
|
||||
public fun <T> adapter(ktype: KType): JsonAdapter<T> {
|
||||
val adapter = adapter<T>(ktype.javaType)
|
||||
return if (adapter is NullSafeJsonAdapter || adapter is NonNullJsonAdapter) {
|
||||
// TODO CR - Assume that these know what they're doing? Or should we defensively avoid wrapping for matching nullability?
|
||||
adapter
|
||||
} else if (ktype.isMarkedNullable) {
|
||||
adapter.nullSafe()
|
||||
} else {
|
||||
adapter.nonNull()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fieldName An optional field name associated with this type. The field name is used as a
|
||||
* hint for better adapter lookup error messages for nested structures.
|
||||
@@ -161,6 +192,10 @@ public class Moshi internal constructor(builder: Builder) {
|
||||
internal val factories = mutableListOf<JsonAdapter.Factory>()
|
||||
internal var lastOffset = 0
|
||||
|
||||
@CheckReturnValue
|
||||
@ExperimentalStdlibApi
|
||||
public inline fun <reified T> addAdapter(adapter: JsonAdapter<T>): Builder = add(typeOf<T>().javaType, adapter)
|
||||
|
||||
public fun <T> add(type: Type, jsonAdapter: JsonAdapter<T>): Builder = apply {
|
||||
add(newAdapterFactory(type, jsonAdapter))
|
||||
}
|
||||
|
Reference in New Issue
Block a user