mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Switch to spotless and format code (#1196)
* Add spotless configuration * Reformat! * Add copyright config for build.gradle.kts files * Add toeholds for headers
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
@@ -23,9 +24,9 @@ plugins {
|
||||
tasks.withType<KotlinCompile>().configureEach {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = listOf(
|
||||
"-Werror",
|
||||
"-Xopt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-Xinline-classes"
|
||||
"-Werror",
|
||||
"-Xopt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-Xinline-classes"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@@ -8,9 +8,10 @@ class DefaultConstructorTest {
|
||||
|
||||
@Test fun minimal() {
|
||||
val expected = TestClass("requiredClass")
|
||||
val json = """{"required":"requiredClass"}"""
|
||||
val json =
|
||||
"""{"required":"requiredClass"}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
.fromJson(json)!!
|
||||
.fromJson(json)!!
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
@@ -18,9 +19,10 @@ class DefaultConstructorTest {
|
||||
|
||||
@Test fun allSet() {
|
||||
val expected = TestClass("requiredClass", "customOptional", 4, "setDynamic", 5, 6)
|
||||
val json = """{"required":"requiredClass","optional":"customOptional","optional2":4,"dynamicSelfReferenceOptional":"setDynamic","dynamicOptional":5,"dynamicInlineOptional":6}"""
|
||||
val json =
|
||||
"""{"required":"requiredClass","optional":"customOptional","optional2":4,"dynamicSelfReferenceOptional":"setDynamic","dynamicOptional":5,"dynamicInlineOptional":6}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
.fromJson(json)!!
|
||||
.fromJson(json)!!
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
@@ -28,9 +30,10 @@ class DefaultConstructorTest {
|
||||
|
||||
@Test fun customDynamic() {
|
||||
val expected = TestClass("requiredClass", "customOptional")
|
||||
val json = """{"required":"requiredClass","optional":"customOptional"}"""
|
||||
val json =
|
||||
"""{"required":"requiredClass","optional":"customOptional"}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
.fromJson(json)!!
|
||||
.fromJson(json)!!
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
@@ -39,20 +42,20 @@ class DefaultConstructorTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class TestClass(
|
||||
val required: String,
|
||||
val optional: String = "optional",
|
||||
val optional2: Int = 2,
|
||||
val dynamicSelfReferenceOptional: String = required,
|
||||
val dynamicOptional: Int = createInt(),
|
||||
val dynamicInlineOptional: Int = createInlineInt()
|
||||
val required: String,
|
||||
val optional: String = "optional",
|
||||
val optional2: Int = 2,
|
||||
val dynamicSelfReferenceOptional: String = required,
|
||||
val dynamicOptional: Int = createInt(),
|
||||
val dynamicInlineOptional: Int = createInlineInt()
|
||||
)
|
||||
|
||||
// Regression test for https://github.com/square/moshi/issues/905
|
||||
// Just needs to compile
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class GenericTestClassWithDefaults<T>(
|
||||
val input: String = "",
|
||||
val genericInput: T
|
||||
val input: String = "",
|
||||
val genericInput: T
|
||||
)
|
||||
|
||||
private fun createInt(): Int {
|
||||
|
@@ -32,22 +32,23 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
@JvmStatic
|
||||
fun parameters(): List<Array<*>> {
|
||||
return listOf(
|
||||
arrayOf(true),
|
||||
arrayOf(false)
|
||||
arrayOf(true),
|
||||
arrayOf(false)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val moshi = Moshi.Builder()
|
||||
.apply {
|
||||
if (useReflection) {
|
||||
add(KotlinJsonAdapterFactory())
|
||||
add(object : Factory {
|
||||
.apply {
|
||||
if (useReflection) {
|
||||
add(KotlinJsonAdapterFactory())
|
||||
add(
|
||||
object : Factory {
|
||||
override fun create(
|
||||
type: Type,
|
||||
annotations: MutableSet<out Annotation>,
|
||||
moshi: Moshi
|
||||
type: Type,
|
||||
annotations: MutableSet<out Annotation>,
|
||||
moshi: Moshi
|
||||
): JsonAdapter<*>? {
|
||||
// Prevent falling back to generated adapter lookup
|
||||
val rawType = Types.getRawType(type)
|
||||
@@ -57,11 +58,11 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
}
|
||||
return moshi.nextAdapter<Any>(this, type, annotations)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
.build()
|
||||
|
||||
}
|
||||
.build()
|
||||
|
||||
@Test fun requiredValueAbsent() {
|
||||
val jsonAdapter = moshi.adapter<RequiredValueAbsent>()
|
||||
@@ -107,13 +108,15 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@Test fun nonNullPropertySetToNullFromAdapterFailsWithJsonDataException() {
|
||||
val jsonAdapter = moshi.newBuilder()
|
||||
.add(object {
|
||||
.add(
|
||||
object {
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
@FromJson
|
||||
fun fromJson(string: String): String? = null
|
||||
})
|
||||
.build()
|
||||
.adapter<HasNonNullProperty>()
|
||||
}
|
||||
)
|
||||
.build()
|
||||
.adapter<HasNonNullProperty>()
|
||||
|
||||
try {
|
||||
//language=JSON
|
||||
@@ -143,13 +146,15 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@Test fun nonNullPropertyWithJsonNameSetToNullFromAdapterFailsWithJsonDataException() {
|
||||
val jsonAdapter = moshi.newBuilder()
|
||||
.add(object {
|
||||
.add(
|
||||
object {
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
@FromJson
|
||||
fun fromJson(string: String): String? = null
|
||||
})
|
||||
.build()
|
||||
.adapter<HasNonNullPropertyDifferentJsonName>()
|
||||
}
|
||||
)
|
||||
.build()
|
||||
.adapter<HasNonNullPropertyDifferentJsonName>()
|
||||
|
||||
try {
|
||||
//language=JSON
|
||||
@@ -179,13 +184,15 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@Test fun nonNullConstructorParameterCalledWithNullFromAdapterFailsWithJsonDataException() {
|
||||
val jsonAdapter = moshi.newBuilder()
|
||||
.add(object {
|
||||
.add(
|
||||
object {
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
@FromJson
|
||||
fun fromJson(string: String): String? = null
|
||||
})
|
||||
.build()
|
||||
.adapter<HasNonNullConstructorParameter>()
|
||||
}
|
||||
)
|
||||
.build()
|
||||
.adapter<HasNonNullConstructorParameter>()
|
||||
|
||||
try {
|
||||
//language=JSON
|
||||
@@ -207,7 +214,8 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@Test fun delegatesToInstalledAdaptersBeforeNullChecking() {
|
||||
val localMoshi = moshi.newBuilder()
|
||||
.add(object {
|
||||
.add(
|
||||
object {
|
||||
@FromJson
|
||||
fun fromJson(@Nullable string: String?): String {
|
||||
return string ?: "fallback"
|
||||
@@ -217,23 +225,30 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
fun toJson(@Nullable value: String?): String {
|
||||
return value ?: "fallback"
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
)
|
||||
.build()
|
||||
|
||||
val hasNonNullConstructorParameterAdapter =
|
||||
localMoshi.adapter<HasNonNullConstructorParameter>()
|
||||
assertThat(hasNonNullConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.fromJson("{\"a\":null}")).isEqualTo(HasNonNullConstructorParameter("fallback"))
|
||||
localMoshi.adapter<HasNonNullConstructorParameter>()
|
||||
assertThat(
|
||||
hasNonNullConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.fromJson("{\"a\":null}")
|
||||
).isEqualTo(HasNonNullConstructorParameter("fallback"))
|
||||
|
||||
val hasNullableConstructorParameterAdapter =
|
||||
localMoshi.adapter<HasNullableConstructorParameter>()
|
||||
assertThat(hasNullableConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.fromJson("{\"a\":null}")).isEqualTo(HasNullableConstructorParameter("fallback"))
|
||||
assertThat(hasNullableConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.toJson(HasNullableConstructorParameter(null))).isEqualTo("{\"a\":\"fallback\"}")
|
||||
localMoshi.adapter<HasNullableConstructorParameter>()
|
||||
assertThat(
|
||||
hasNullableConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.fromJson("{\"a\":null}")
|
||||
).isEqualTo(HasNullableConstructorParameter("fallback"))
|
||||
assertThat(
|
||||
hasNullableConstructorParameterAdapter
|
||||
//language=JSON
|
||||
.toJson(HasNullableConstructorParameter(null))
|
||||
).isEqualTo("{\"a\":\"fallback\"}")
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -258,10 +273,12 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
val inline = InlineClass(5)
|
||||
|
||||
val expectedJson = """{"i":5}"""
|
||||
val expectedJson =
|
||||
"""{"i":5}"""
|
||||
assertThat(adapter.toJson(inline)).isEqualTo(expectedJson)
|
||||
|
||||
val testJson = """{"i":6}"""
|
||||
val testJson =
|
||||
"""{"i":6}"""
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.i).isEqualTo(6)
|
||||
}
|
||||
@@ -275,11 +292,13 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val consumer = InlineConsumer(InlineClass(23))
|
||||
|
||||
@Language("JSON")
|
||||
val expectedJson = """{"inline":{"i":23}}"""
|
||||
val expectedJson =
|
||||
"""{"inline":{"i":23}}"""
|
||||
assertThat(adapter.toJson(consumer)).isEqualTo(expectedJson)
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"inline":{"i":42}}"""
|
||||
val testJson =
|
||||
"""{"inline":{"i":42}}"""
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.inline.i).isEqualTo(42)
|
||||
}
|
||||
@@ -289,7 +308,8 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<TextAssetMetaData>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"text":"text"}"""
|
||||
val testJson =
|
||||
"""{"text":"text"}"""
|
||||
|
||||
assertThat(adapter.toJson(TextAssetMetaData("text"))).isEqualTo(testJson)
|
||||
|
||||
@@ -308,7 +328,8 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<InternalAbstractProperty>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"test":"text"}"""
|
||||
val testJson =
|
||||
"""{"test":"text"}"""
|
||||
|
||||
assertThat(adapter.toJson(InternalAbstractProperty("text"))).isEqualTo(testJson)
|
||||
|
||||
@@ -338,7 +359,8 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
assertThat(adapter.toJson(MultipleConstructorsB(6))).isEqualTo("""{"f":{"f":6},"b":6}""")
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"b":6}"""
|
||||
val testJson =
|
||||
"""{"b":6}"""
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.b).isEqualTo(6)
|
||||
}
|
||||
@@ -348,14 +370,15 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class MultipleConstructorsB(val f: MultipleConstructorsA = MultipleConstructorsA(5), val b: Int) {
|
||||
constructor(f: Int, b: Int = 6): this(MultipleConstructorsA(f), b)
|
||||
constructor(f: Int, b: Int = 6) : this(MultipleConstructorsA(f), b)
|
||||
}
|
||||
|
||||
@Test fun `multiple non-property parameters`() {
|
||||
val adapter = moshi.adapter<MultipleNonPropertyParameters>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"prop":7}"""
|
||||
val testJson =
|
||||
"""{"prop":7}"""
|
||||
|
||||
assertThat(adapter.toJson(MultipleNonPropertyParameters(7))).isEqualTo(testJson)
|
||||
|
||||
@@ -365,9 +388,9 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class MultipleNonPropertyParameters(
|
||||
val prop: Int,
|
||||
param1: Int = 1,
|
||||
param2: Int = 2
|
||||
val prop: Int,
|
||||
param1: Int = 1,
|
||||
param2: Int = 2
|
||||
) {
|
||||
init {
|
||||
// Ensure the params always uses their default value
|
||||
@@ -381,10 +404,11 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<OnlyMultipleNonPropertyParameters>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"prop":7}"""
|
||||
val testJson =
|
||||
"""{"prop":7}"""
|
||||
|
||||
assertThat(adapter.toJson(OnlyMultipleNonPropertyParameters().apply { prop = 7 }))
|
||||
.isEqualTo(testJson)
|
||||
.isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.prop).isEqualTo(7)
|
||||
@@ -392,8 +416,8 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class OnlyMultipleNonPropertyParameters(
|
||||
param1: Int = 1,
|
||||
param2: Int = 2
|
||||
param1: Int = 1,
|
||||
param2: Int = 2
|
||||
) {
|
||||
init {
|
||||
// Ensure the params always uses their default value
|
||||
@@ -406,20 +430,21 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@Test fun typeAliasUnwrapping() {
|
||||
val adapter = moshi
|
||||
.newBuilder()
|
||||
.add(Types.supertypeOf(Int::class.javaObjectType), moshi.adapter<Int>())
|
||||
.build()
|
||||
.adapter<TypeAliasUnwrapping>()
|
||||
.newBuilder()
|
||||
.add(Types.supertypeOf(Int::class.javaObjectType), moshi.adapter<Int>())
|
||||
.build()
|
||||
.adapter<TypeAliasUnwrapping>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"simpleClass":6,"parameterized":{"value":6},"wildcardIn":{"value":6},"wildcardOut":{"value":6},"complex":{"value":[{"value":6}]}}"""
|
||||
val testJson =
|
||||
"""{"simpleClass":6,"parameterized":{"value":6},"wildcardIn":{"value":6},"wildcardOut":{"value":6},"complex":{"value":[{"value":6}]}}"""
|
||||
|
||||
val testValue = TypeAliasUnwrapping(
|
||||
simpleClass = 6,
|
||||
parameterized = GenericClass(6),
|
||||
wildcardIn = GenericClass(6),
|
||||
wildcardOut = GenericClass(6),
|
||||
complex = GenericClass(listOf(GenericClass(6)))
|
||||
simpleClass = 6,
|
||||
parameterized = GenericClass(6),
|
||||
wildcardIn = GenericClass(6),
|
||||
wildcardOut = GenericClass(6),
|
||||
complex = GenericClass(listOf(GenericClass(6)))
|
||||
)
|
||||
assertThat(adapter.toJson(testValue)).isEqualTo(testJson)
|
||||
|
||||
@@ -429,11 +454,11 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class TypeAliasUnwrapping(
|
||||
val simpleClass: TypeAlias,
|
||||
val parameterized: GenericClass<TypeAlias>,
|
||||
val wildcardIn: GenericClass<in TypeAlias>,
|
||||
val wildcardOut: GenericClass<out TypeAlias>,
|
||||
val complex: GenericClass<GenericTypeAlias>?
|
||||
val simpleClass: TypeAlias,
|
||||
val parameterized: GenericClass<TypeAlias>,
|
||||
val wildcardIn: GenericClass<in TypeAlias>,
|
||||
val wildcardOut: GenericClass<out TypeAlias>,
|
||||
val complex: GenericClass<GenericTypeAlias>?
|
||||
)
|
||||
|
||||
// Regression test for https://github.com/square/moshi/issues/991
|
||||
@@ -441,21 +466,22 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<NullablePrimitives>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"objectType":"value","boolean":true,"byte":3,"char":"a","short":3,"int":3,"long":3,"float":3.2,"double":3.2}"""
|
||||
val testJson =
|
||||
"""{"objectType":"value","boolean":true,"byte":3,"char":"a","short":3,"int":3,"long":3,"float":3.2,"double":3.2}"""
|
||||
|
||||
val instance = NullablePrimitives(
|
||||
objectType = "value",
|
||||
boolean = true,
|
||||
byte = 3,
|
||||
char = 'a',
|
||||
short = 3,
|
||||
int = 3,
|
||||
long = 3,
|
||||
float = 3.2f,
|
||||
double = 3.2
|
||||
objectType = "value",
|
||||
boolean = true,
|
||||
byte = 3,
|
||||
char = 'a',
|
||||
short = 3,
|
||||
int = 3,
|
||||
long = 3,
|
||||
float = 3.2f,
|
||||
double = 3.2
|
||||
)
|
||||
assertThat(adapter.toJson(instance))
|
||||
.isEqualTo(testJson)
|
||||
.isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result).isEqualTo(instance)
|
||||
@@ -463,23 +489,23 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class NullablePrimitives(
|
||||
val objectType: String = "",
|
||||
val boolean: Boolean,
|
||||
val nullableBoolean: Boolean? = null,
|
||||
val byte: Byte,
|
||||
val nullableByte: Byte? = null,
|
||||
val char: Char,
|
||||
val nullableChar: Char? = null,
|
||||
val short: Short,
|
||||
val nullableShort: Short? = null,
|
||||
val int: Int,
|
||||
val nullableInt: Int? = null,
|
||||
val long: Long,
|
||||
val nullableLong: Long? = null,
|
||||
val float: Float,
|
||||
val nullableFloat: Float? = null,
|
||||
val double: Double,
|
||||
val nullableDouble: Double? = null
|
||||
val objectType: String = "",
|
||||
val boolean: Boolean,
|
||||
val nullableBoolean: Boolean? = null,
|
||||
val byte: Byte,
|
||||
val nullableByte: Byte? = null,
|
||||
val char: Char,
|
||||
val nullableChar: Char? = null,
|
||||
val short: Short,
|
||||
val nullableShort: Short? = null,
|
||||
val int: Int,
|
||||
val nullableInt: Int? = null,
|
||||
val long: Long,
|
||||
val nullableLong: Long? = null,
|
||||
val float: Float,
|
||||
val nullableFloat: Float? = null,
|
||||
val double: Double,
|
||||
val nullableDouble: Double? = null
|
||||
)
|
||||
|
||||
// Regression test for https://github.com/square/moshi/issues/990
|
||||
@@ -487,10 +513,11 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<NullableList>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"nullableList":null}"""
|
||||
val testJson =
|
||||
"""{"nullableList":null}"""
|
||||
|
||||
assertThat(adapter.serializeNulls().toJson(NullableList(null)))
|
||||
.isEqualTo(testJson)
|
||||
.isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.nullableList).isNull()
|
||||
@@ -503,11 +530,12 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<TypeAliasNullability>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"aShouldBeNonNull":3,"nullableAShouldBeNullable":null,"redundantNullableAShouldBeNullable":null,"manuallyNullableAShouldBeNullable":null,"convolutedMultiNullableShouldBeNullable":null,"deepNestedNullableShouldBeNullable":null}"""
|
||||
val testJson =
|
||||
"""{"aShouldBeNonNull":3,"nullableAShouldBeNullable":null,"redundantNullableAShouldBeNullable":null,"manuallyNullableAShouldBeNullable":null,"convolutedMultiNullableShouldBeNullable":null,"deepNestedNullableShouldBeNullable":null}"""
|
||||
|
||||
val instance = TypeAliasNullability(3, null, null, null, null, null)
|
||||
assertThat(adapter.serializeNulls().toJson(instance))
|
||||
.isEqualTo(testJson)
|
||||
.isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result).isEqualTo(instance)
|
||||
@@ -516,12 +544,12 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
@Suppress("REDUNDANT_NULLABLE")
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class TypeAliasNullability(
|
||||
val aShouldBeNonNull: A,
|
||||
val nullableAShouldBeNullable: NullableA,
|
||||
val redundantNullableAShouldBeNullable: NullableA?,
|
||||
val manuallyNullableAShouldBeNullable: A?,
|
||||
val convolutedMultiNullableShouldBeNullable: NullableB?,
|
||||
val deepNestedNullableShouldBeNullable: E
|
||||
val aShouldBeNonNull: A,
|
||||
val nullableAShouldBeNullable: NullableA,
|
||||
val redundantNullableAShouldBeNullable: NullableA?,
|
||||
val manuallyNullableAShouldBeNullable: A?,
|
||||
val convolutedMultiNullableShouldBeNullable: NullableB?,
|
||||
val deepNestedNullableShouldBeNullable: E
|
||||
)
|
||||
|
||||
// Regression test for https://github.com/square/moshi/issues/1009
|
||||
@@ -529,11 +557,12 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
val adapter = moshi.adapter<OutDeclaration<Int>>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"input":3}"""
|
||||
val testJson =
|
||||
"""{"input":3}"""
|
||||
|
||||
val instance = OutDeclaration(3)
|
||||
assertThat(adapter.serializeNulls().toJson(instance))
|
||||
.isEqualTo(testJson)
|
||||
.isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result).isEqualTo(instance)
|
||||
|
@@ -18,7 +18,8 @@ class ComplexGenericsInheritanceTest {
|
||||
val adapter = moshi.adapter<PersonResponse>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
val json =
|
||||
"""{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
val testInstance = PersonResponse().apply {
|
||||
@@ -33,7 +34,8 @@ class ComplexGenericsInheritanceTest {
|
||||
val adapter = moshi.adapter<NestedPersonResponse>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
val json =
|
||||
"""{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
val testInstance = NestedPersonResponse().apply {
|
||||
@@ -48,7 +50,8 @@ class ComplexGenericsInheritanceTest {
|
||||
val adapter = moshi.adapter<UntypedNestedPersonResponse<Person>>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
val json =
|
||||
"""{"data":{"name":"foo"},"data2":"bar","data3":"baz"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
val testInstance = UntypedNestedPersonResponse<Person>().apply {
|
||||
@@ -63,16 +66,17 @@ class ComplexGenericsInheritanceTest {
|
||||
val adapter = moshi.adapter<Layer4<Person, UntypedNestedPersonResponse<Person>>>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"layer4E":{"name":"layer4E"},"layer4F":{"data":{"name":"layer4F"},"data2":"layer4F","data3":"layer4F"},"layer3C":[1,2,3],"layer3D":"layer3D","layer2":"layer2","layer1":"layer1"}"""
|
||||
val json =
|
||||
"""{"layer4E":{"name":"layer4E"},"layer4F":{"data":{"name":"layer4F"},"data2":"layer4F","data3":"layer4F"},"layer3C":[1,2,3],"layer3D":"layer3D","layer2":"layer2","layer1":"layer1"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
val testInstance = Layer4(
|
||||
layer4E = Person("layer4E"),
|
||||
layer4F = UntypedNestedPersonResponse<Person>().apply {
|
||||
data = Person("layer4F")
|
||||
data2 = "layer4F"
|
||||
data3 = "layer4F"
|
||||
}
|
||||
layer4E = Person("layer4E"),
|
||||
layer4F = UntypedNestedPersonResponse<Person>().apply {
|
||||
data = Person("layer4F")
|
||||
data2 = "layer4F"
|
||||
data3 = "layer4F"
|
||||
}
|
||||
).apply {
|
||||
layer3C = listOf(1, 2, 3)
|
||||
layer3D = "layer3D"
|
||||
@@ -97,7 +101,8 @@ data class Person(val name: String) : Personable
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class PersonResponse(
|
||||
val extra: String? = null) : ResponseWithSettableProperty<Person, String>()
|
||||
val extra: String? = null
|
||||
) : ResponseWithSettableProperty<Person, String>()
|
||||
|
||||
abstract class NestedResponse<T : Personable> : ResponseWithSettableProperty<T, String>()
|
||||
|
||||
@@ -106,7 +111,7 @@ data class NestedPersonResponse(val extra: String? = null) : NestedResponse<Pers
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class UntypedNestedPersonResponse<T : Personable>(
|
||||
val extra: String? = null
|
||||
val extra: String? = null
|
||||
) : NestedResponse<T>()
|
||||
|
||||
interface LayerInterface<I>
|
||||
@@ -126,6 +131,6 @@ abstract class Layer3<C, D> : Layer2<D>() {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class Layer4<E : Personable, F>(
|
||||
val layer4E: E,
|
||||
val layer4F: F? = null
|
||||
val layer4E: E,
|
||||
val layer4F: F? = null
|
||||
) : Layer3<List<Int>, String>(), LayerInterface<String>
|
||||
|
@@ -50,17 +50,22 @@ class GeneratedAdaptersTest {
|
||||
|
||||
// Read
|
||||
@Language("JSON")
|
||||
val json = """{"foo": "bar"}"""
|
||||
val json =
|
||||
"""{"foo": "bar"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.bar).isEqualTo("bar")
|
||||
|
||||
// Write
|
||||
@Language("JSON")
|
||||
val expectedJson = """{"foo":"baz"}"""
|
||||
val expectedJson =
|
||||
"""{"foo":"baz"}"""
|
||||
|
||||
assertThat(adapter.toJson(
|
||||
JsonAnnotation("baz"))).isEqualTo(expectedJson)
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
JsonAnnotation("baz")
|
||||
)
|
||||
).isEqualTo(expectedJson)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -79,8 +84,11 @@ class GeneratedAdaptersTest {
|
||||
// Write
|
||||
val expectedJson = "{\"\$foo\":\"baz\"}"
|
||||
|
||||
assertThat(adapter.toJson(
|
||||
JsonAnnotationWithDollarSign("baz"))).isEqualTo(expectedJson)
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
JsonAnnotationWithDollarSign("baz")
|
||||
)
|
||||
).isEqualTo(expectedJson)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -91,16 +99,21 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<JsonAnnotationWithQuotationMark>()
|
||||
|
||||
// Read
|
||||
val json = """{"\"foo\"": "bar"}"""
|
||||
val json =
|
||||
"""{"\"foo\"": "bar"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.bar).isEqualTo("bar")
|
||||
|
||||
// Write
|
||||
val expectedJson = """{"\"foo\"":"baz"}"""
|
||||
val expectedJson =
|
||||
"""{"\"foo\"":"baz"}"""
|
||||
|
||||
assertThat(adapter.toJson(
|
||||
JsonAnnotationWithQuotationMark("baz"))).isEqualTo(expectedJson)
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
JsonAnnotationWithQuotationMark("baz")
|
||||
)
|
||||
).isEqualTo(expectedJson)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -112,7 +125,8 @@ class GeneratedAdaptersTest {
|
||||
|
||||
// Read/write with default values
|
||||
@Language("JSON")
|
||||
val json = """{"foo":"fooString"}"""
|
||||
val json =
|
||||
"""{"foo":"fooString"}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.foo).isEqualTo("fooString")
|
||||
@@ -123,13 +137,18 @@ class GeneratedAdaptersTest {
|
||||
isEmpty()
|
||||
}
|
||||
|
||||
@Language("JSON") val expected = """{"foo":"fooString","bar":"","bazList":[]}"""
|
||||
assertThat(adapter.toJson(
|
||||
DefaultValues("fooString"))).isEqualTo(expected)
|
||||
@Language("JSON") val expected =
|
||||
"""{"foo":"fooString","bar":"","bazList":[]}"""
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
DefaultValues("fooString")
|
||||
)
|
||||
).isEqualTo(expected)
|
||||
|
||||
// Read/write with real values
|
||||
@Language("JSON")
|
||||
val json2 = """
|
||||
val json2 =
|
||||
"""
|
||||
{"foo":"fooString","bar":"barString","nullableBar":"bar","bazList":["baz"]}
|
||||
""".trimIndent()
|
||||
|
||||
@@ -146,14 +165,16 @@ class GeneratedAdaptersTest {
|
||||
val foo: String,
|
||||
val bar: String = "",
|
||||
val nullableBar: String? = null,
|
||||
val bazList: List<String> = emptyList())
|
||||
val bazList: List<String> = emptyList()
|
||||
)
|
||||
|
||||
@Test
|
||||
fun nullableArray() {
|
||||
val adapter = moshi.adapter<NullableArray>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"data":[null,"why"]}"""
|
||||
val json =
|
||||
"""{"data":[null,"why"]}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.data).containsExactly(null, "why")
|
||||
@@ -168,7 +189,8 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<PrimitiveArray>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"ints":[0,1]}"""
|
||||
val json =
|
||||
"""{"ints":[0,1]}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.ints).containsExactly(0, 1)
|
||||
@@ -183,9 +205,11 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<NullabeTypes>()
|
||||
|
||||
@Language("JSON")
|
||||
val json = """{"foo":"foo","nullableString":null}"""
|
||||
val json =
|
||||
"""{"foo":"foo","nullableString":null}"""
|
||||
@Language("JSON")
|
||||
val invalidJson = """{"foo":null,"nullableString":null}"""
|
||||
val invalidJson =
|
||||
"""{"foo":null,"nullableString":null}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.foo).isEqualTo("foo")
|
||||
@@ -201,8 +225,8 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class NullabeTypes(
|
||||
val foo: String,
|
||||
val nullableString: String?
|
||||
val foo: String,
|
||||
val nullableString: String?
|
||||
)
|
||||
|
||||
@Test
|
||||
@@ -210,12 +234,12 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<SpecialCollections>()
|
||||
|
||||
val specialCollections = SpecialCollections(
|
||||
mutableListOf(),
|
||||
mutableSetOf(),
|
||||
mutableMapOf(),
|
||||
emptyList(),
|
||||
emptySet(),
|
||||
emptyMap()
|
||||
mutableListOf(),
|
||||
mutableSetOf(),
|
||||
mutableMapOf(),
|
||||
emptyList(),
|
||||
emptySet(),
|
||||
emptyMap()
|
||||
)
|
||||
|
||||
val json = adapter.toJson(specialCollections)
|
||||
@@ -225,12 +249,12 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class SpecialCollections(
|
||||
val mutableList: MutableList<String>,
|
||||
val mutableSet: MutableSet<String>,
|
||||
val mutableMap: MutableMap<String, String>,
|
||||
val immutableList: List<String>,
|
||||
val immutableSet: Set<String>,
|
||||
val immutableMap: Map<String, String>
|
||||
val mutableList: MutableList<String>,
|
||||
val mutableSet: MutableSet<String>,
|
||||
val mutableMap: MutableMap<String, String>,
|
||||
val immutableList: List<String>,
|
||||
val immutableSet: Set<String>,
|
||||
val immutableMap: Map<String, String>
|
||||
)
|
||||
|
||||
@Test
|
||||
@@ -238,18 +262,18 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<MutableProperties>()
|
||||
|
||||
val mutableProperties = MutableProperties(
|
||||
"immutableProperty",
|
||||
"mutableProperty",
|
||||
mutableListOf("immutableMutableList"),
|
||||
mutableListOf("immutableImmutableList"),
|
||||
mutableListOf("mutableMutableList"),
|
||||
mutableListOf("mutableImmutableList"),
|
||||
"immutableProperty",
|
||||
"mutableProperty",
|
||||
mutableListOf("immutableMutableList"),
|
||||
mutableListOf("immutableImmutableList"),
|
||||
mutableListOf("mutableMutableList"),
|
||||
mutableListOf("mutableImmutableList")
|
||||
"immutableProperty",
|
||||
"mutableProperty",
|
||||
mutableListOf("immutableMutableList"),
|
||||
mutableListOf("immutableImmutableList"),
|
||||
mutableListOf("mutableMutableList"),
|
||||
mutableListOf("mutableImmutableList"),
|
||||
"immutableProperty",
|
||||
"mutableProperty",
|
||||
mutableListOf("immutableMutableList"),
|
||||
mutableListOf("immutableImmutableList"),
|
||||
mutableListOf("mutableMutableList"),
|
||||
mutableListOf("mutableImmutableList")
|
||||
)
|
||||
|
||||
val json = adapter.toJson(mutableProperties)
|
||||
@@ -259,42 +283,45 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class MutableProperties(
|
||||
val immutableProperty: String,
|
||||
var mutableProperty: String,
|
||||
val immutableMutableList: MutableList<String>,
|
||||
val immutableImmutableList: List<String>,
|
||||
var mutableMutableList: MutableList<String>,
|
||||
var mutableImmutableList: List<String>,
|
||||
val nullableImmutableProperty: String?,
|
||||
var nullableMutableProperty: String?,
|
||||
val nullableImmutableMutableList: MutableList<String>?,
|
||||
val nullableImmutableImmutableList: List<String>?,
|
||||
var nullableMutableMutableList: MutableList<String>?,
|
||||
var nullableMutableImmutableList: List<String>
|
||||
val immutableProperty: String,
|
||||
var mutableProperty: String,
|
||||
val immutableMutableList: MutableList<String>,
|
||||
val immutableImmutableList: List<String>,
|
||||
var mutableMutableList: MutableList<String>,
|
||||
var mutableImmutableList: List<String>,
|
||||
val nullableImmutableProperty: String?,
|
||||
var nullableMutableProperty: String?,
|
||||
val nullableImmutableMutableList: MutableList<String>?,
|
||||
val nullableImmutableImmutableList: List<String>?,
|
||||
var nullableMutableMutableList: MutableList<String>?,
|
||||
var nullableMutableImmutableList: List<String>
|
||||
)
|
||||
|
||||
@Test
|
||||
fun nullableTypeParams() {
|
||||
val adapter = moshi.adapter<NullableTypeParams<Int>>(
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
GeneratedAdaptersTest::class.java,
|
||||
NullableTypeParams::class.java, Int::class.javaObjectType))
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
GeneratedAdaptersTest::class.java,
|
||||
NullableTypeParams::class.java,
|
||||
Int::class.javaObjectType
|
||||
)
|
||||
)
|
||||
val nullSerializing = adapter.serializeNulls()
|
||||
|
||||
val nullableTypeParams = NullableTypeParams(
|
||||
listOf("foo", null, "bar"),
|
||||
setOf("foo", null, "bar"),
|
||||
mapOf("foo" to "bar", "baz" to null),
|
||||
null,
|
||||
1
|
||||
listOf("foo", null, "bar"),
|
||||
setOf("foo", null, "bar"),
|
||||
mapOf("foo" to "bar", "baz" to null),
|
||||
null,
|
||||
1
|
||||
)
|
||||
|
||||
val noNullsTypeParams = NullableTypeParams(
|
||||
nullableTypeParams.nullableList,
|
||||
nullableTypeParams.nullableSet,
|
||||
nullableTypeParams.nullableMap.filterValues { it != null },
|
||||
null,
|
||||
1
|
||||
nullableTypeParams.nullableList,
|
||||
nullableTypeParams.nullableSet,
|
||||
nullableTypeParams.nullableMap.filterValues { it != null },
|
||||
null,
|
||||
1
|
||||
)
|
||||
|
||||
val json = adapter.toJson(nullableTypeParams)
|
||||
@@ -330,8 +357,10 @@ class GeneratedAdaptersTest {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val jsonAdapter = moshi.adapter<ConstructorParameters>()
|
||||
|
||||
val encoded = ConstructorParameters(3,
|
||||
5)
|
||||
val encoded = ConstructorParameters(
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":3,"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!!
|
||||
@@ -367,7 +396,8 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<ConstructorParametersAndProperties>()
|
||||
|
||||
val encoded = ConstructorParametersAndProperties(
|
||||
3)
|
||||
3
|
||||
)
|
||||
encoded.b = 5
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":3,"b":5}""")
|
||||
|
||||
@@ -386,7 +416,9 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<ImmutableConstructorParameters>()
|
||||
|
||||
val encoded = ImmutableConstructorParameters(
|
||||
3, 5)
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":3,"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!!
|
||||
@@ -420,7 +452,9 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<ConstructorDefaultValues>()
|
||||
|
||||
val encoded = ConstructorDefaultValues(
|
||||
3, 5)
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":3,"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"b":6}""")!!
|
||||
@@ -465,12 +499,14 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@Test fun constructorParameterWithQualifier() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<ConstructorParameterWithQualifier>()
|
||||
|
||||
val encoded = ConstructorParameterWithQualifier(
|
||||
"Android", "Banana")
|
||||
"Android",
|
||||
"Banana"
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":"ANDROID","b":"Banana"}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":"Android","b":"Banana"}""")!!
|
||||
@@ -483,8 +519,8 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@Test fun propertyWithQualifier() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<PropertyWithQualifier>()
|
||||
|
||||
val encoded = PropertyWithQualifier()
|
||||
@@ -508,7 +544,9 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<ConstructorParameterWithJsonName>()
|
||||
|
||||
val encoded = ConstructorParameterWithJsonName(
|
||||
3, 5)
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"key a":3,"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"key a":4,"b":6}""")!!
|
||||
@@ -544,7 +582,9 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<TransientConstructorParameter>()
|
||||
|
||||
val encoded = TransientConstructorParameter(
|
||||
3, 5)
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!!
|
||||
@@ -618,7 +658,7 @@ class GeneratedAdaptersTest {
|
||||
@JsonClass(generateAdapter = true)
|
||||
class TransientDelegateProperty {
|
||||
|
||||
private fun <T>delegate(initial: T) = Delegates.observable(initial) { _, _, _-> }
|
||||
private fun <T> delegate(initial: T) = Delegates.observable(initial) { _, _, _ -> }
|
||||
|
||||
@delegate:Transient var a: Int by delegate(-1)
|
||||
@delegate:Transient private var b: Int by delegate(-1)
|
||||
@@ -637,14 +677,16 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<ManyProperties32>()
|
||||
|
||||
val encoded = ManyProperties32(
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132)
|
||||
val json = ("""
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132
|
||||
)
|
||||
val json = (
|
||||
"""
|
||||
|{
|
||||
|"v01":101,"v02":102,"v03":103,"v04":104,"v05":105,
|
||||
|"v06":106,"v07":107,"v08":108,"v09":109,"v10":110,
|
||||
@@ -654,7 +696,8 @@ class GeneratedAdaptersTest {
|
||||
|"v26":126,"v27":127,"v28":128,"v29":129,"v30":130,
|
||||
|"v31":131,"v32":132
|
||||
|}
|
||||
|""").trimMargin().replace("\n", "")
|
||||
|"""
|
||||
).trimMargin().replace("\n", "")
|
||||
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo(json)
|
||||
|
||||
@@ -665,27 +708,55 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class ManyProperties32(
|
||||
var v01: Int, var v02: Int, var v03: Int, var v04: Int, var v05: Int,
|
||||
var v06: Int, var v07: Int, var v08: Int, var v09: Int, var v10: Int,
|
||||
var v11: Int, var v12: Int, var v13: Int, var v14: Int, var v15: Int,
|
||||
var v16: Int, var v17: Int, var v18: Int, var v19: Int, var v20: Int,
|
||||
var v21: Int, var v22: Int, var v23: Int, var v24: Int, var v25: Int,
|
||||
var v26: Int, var v27: Int, var v28: Int, var v29: Int, var v30: Int,
|
||||
var v31: Int, var v32: Int)
|
||||
var v01: Int,
|
||||
var v02: Int,
|
||||
var v03: Int,
|
||||
var v04: Int,
|
||||
var v05: Int,
|
||||
var v06: Int,
|
||||
var v07: Int,
|
||||
var v08: Int,
|
||||
var v09: Int,
|
||||
var v10: Int,
|
||||
var v11: Int,
|
||||
var v12: Int,
|
||||
var v13: Int,
|
||||
var v14: Int,
|
||||
var v15: Int,
|
||||
var v16: Int,
|
||||
var v17: Int,
|
||||
var v18: Int,
|
||||
var v19: Int,
|
||||
var v20: Int,
|
||||
var v21: Int,
|
||||
var v22: Int,
|
||||
var v23: Int,
|
||||
var v24: Int,
|
||||
var v25: Int,
|
||||
var v26: Int,
|
||||
var v27: Int,
|
||||
var v28: Int,
|
||||
var v29: Int,
|
||||
var v30: Int,
|
||||
var v31: Int,
|
||||
var v32: Int
|
||||
)
|
||||
|
||||
@Test fun manyProperties33() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val jsonAdapter = moshi.adapter<ManyProperties33>()
|
||||
|
||||
val encoded = ManyProperties33(
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132, 133)
|
||||
val json = ("""
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132, 133
|
||||
)
|
||||
val json = (
|
||||
"""
|
||||
|{
|
||||
|"v01":101,"v02":102,"v03":103,"v04":104,"v05":105,
|
||||
|"v06":106,"v07":107,"v08":108,"v09":109,"v10":110,
|
||||
@@ -695,7 +766,8 @@ class GeneratedAdaptersTest {
|
||||
|"v26":126,"v27":127,"v28":128,"v29":129,"v30":130,
|
||||
|"v31":131,"v32":132,"v33":133
|
||||
|}
|
||||
|""").trimMargin().replace("\n", "")
|
||||
|"""
|
||||
).trimMargin().replace("\n", "")
|
||||
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo(json)
|
||||
|
||||
@@ -707,13 +779,40 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class ManyProperties33(
|
||||
var v01: Int, var v02: Int, var v03: Int, var v04: Int, var v05: Int,
|
||||
var v06: Int, var v07: Int, var v08: Int, var v09: Int, var v10: Int,
|
||||
var v11: Int, var v12: Int, var v13: Int, var v14: Int, var v15: Int,
|
||||
var v16: Int, var v17: Int, var v18: Int, var v19: Int, var v20: Int,
|
||||
var v21: Int, var v22: Int, var v23: Int, var v24: Int, var v25: Int,
|
||||
var v26: Int, var v27: Int, var v28: Int, var v29: Int, var v30: Int,
|
||||
var v31: Int, var v32: Int, var v33: Int)
|
||||
var v01: Int,
|
||||
var v02: Int,
|
||||
var v03: Int,
|
||||
var v04: Int,
|
||||
var v05: Int,
|
||||
var v06: Int,
|
||||
var v07: Int,
|
||||
var v08: Int,
|
||||
var v09: Int,
|
||||
var v10: Int,
|
||||
var v11: Int,
|
||||
var v12: Int,
|
||||
var v13: Int,
|
||||
var v14: Int,
|
||||
var v15: Int,
|
||||
var v16: Int,
|
||||
var v17: Int,
|
||||
var v18: Int,
|
||||
var v19: Int,
|
||||
var v20: Int,
|
||||
var v21: Int,
|
||||
var v22: Int,
|
||||
var v23: Int,
|
||||
var v24: Int,
|
||||
var v25: Int,
|
||||
var v26: Int,
|
||||
var v27: Int,
|
||||
var v28: Int,
|
||||
var v29: Int,
|
||||
var v30: Int,
|
||||
var v31: Int,
|
||||
var v32: Int,
|
||||
var v33: Int
|
||||
)
|
||||
|
||||
@Test fun unsettablePropertyIgnored() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
@@ -749,7 +848,7 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class GetterOnly(var a: Int, var b: Int) {
|
||||
val total : Int
|
||||
val total: Int
|
||||
get() = a + b
|
||||
}
|
||||
|
||||
@@ -775,7 +874,7 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class GetterAndSetter(var a: Int, var b: Int) {
|
||||
var total : Int
|
||||
var total: Int
|
||||
get() = a + b
|
||||
set(value) {
|
||||
b = value - a
|
||||
@@ -787,7 +886,9 @@ class GeneratedAdaptersTest {
|
||||
val jsonAdapter = moshi.adapter<SubtypeConstructorParameters>()
|
||||
|
||||
val encoded = SubtypeConstructorParameters(
|
||||
3, 5)
|
||||
3,
|
||||
5
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":3,"b":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!!
|
||||
@@ -831,7 +932,7 @@ class GeneratedAdaptersTest {
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
@@ -846,7 +947,7 @@ class GeneratedAdaptersTest {
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
@@ -881,18 +982,20 @@ class GeneratedAdaptersTest {
|
||||
/** https://github.com/square/moshi/issues/563 */
|
||||
@Test fun qualifiedAdaptersAreShared() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<MultiplePropertiesShareAdapter>()
|
||||
|
||||
val encoded = MultiplePropertiesShareAdapter(
|
||||
"Android", "Banana")
|
||||
"Android",
|
||||
"Banana"
|
||||
)
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"a":"ANDROID","b":"BANANA"}""")
|
||||
|
||||
val delegateAdapters = GeneratedAdaptersTest_MultiplePropertiesShareAdapterJsonAdapter::class
|
||||
.memberProperties.filter {
|
||||
it.returnType.classifier == JsonAdapter::class
|
||||
}
|
||||
.memberProperties.filter {
|
||||
it.returnType.classifier == JsonAdapter::class
|
||||
}
|
||||
assertThat(delegateAdapters).hasSize(1)
|
||||
}
|
||||
|
||||
@@ -904,12 +1007,15 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@Test fun toJsonOnly() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(CustomToJsonOnlyAdapter())
|
||||
.build()
|
||||
.add(CustomToJsonOnlyAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<CustomToJsonOnly>()
|
||||
|
||||
assertThat(jsonAdapter.toJson(
|
||||
CustomToJsonOnly(1, 2))).isEqualTo("""[1,2]""")
|
||||
assertThat(
|
||||
jsonAdapter.toJson(
|
||||
CustomToJsonOnly(1, 2)
|
||||
)
|
||||
).isEqualTo("""[1,2]""")
|
||||
|
||||
val fromJson = jsonAdapter.fromJson("""{"a":3,"b":4}""")!!
|
||||
assertThat(fromJson.a).isEqualTo(3)
|
||||
@@ -920,19 +1026,22 @@ class GeneratedAdaptersTest {
|
||||
class CustomToJsonOnly(var a: Int, var b: Int)
|
||||
|
||||
class CustomToJsonOnlyAdapter {
|
||||
@ToJson fun toJson(v: CustomToJsonOnly) : List<Int> {
|
||||
@ToJson fun toJson(v: CustomToJsonOnly): List<Int> {
|
||||
return listOf(v.a, v.b)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun fromJsonOnly() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(CustomFromJsonOnlyAdapter())
|
||||
.build()
|
||||
.add(CustomFromJsonOnlyAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<CustomFromJsonOnly>()
|
||||
|
||||
assertThat(jsonAdapter.toJson(
|
||||
CustomFromJsonOnly(1, 2))).isEqualTo("""{"a":1,"b":2}""")
|
||||
assertThat(
|
||||
jsonAdapter.toJson(
|
||||
CustomFromJsonOnly(1, 2)
|
||||
)
|
||||
).isEqualTo("""{"a":1,"b":2}""")
|
||||
|
||||
val fromJson = jsonAdapter.fromJson("""[3,4]""")!!
|
||||
assertThat(fromJson.a).isEqualTo(3)
|
||||
@@ -943,7 +1052,7 @@ class GeneratedAdaptersTest {
|
||||
class CustomFromJsonOnly(var a: Int, var b: Int)
|
||||
|
||||
class CustomFromJsonOnlyAdapter {
|
||||
@FromJson fun fromJson(v: List<Int>) : CustomFromJsonOnly {
|
||||
@FromJson fun fromJson(v: List<Int>): CustomFromJsonOnly {
|
||||
return CustomFromJsonOnly(v[0], v[1])
|
||||
}
|
||||
}
|
||||
@@ -977,8 +1086,8 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@Test fun propertyIsNothing() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(NothingAdapter())
|
||||
.build()
|
||||
.add(NothingAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<HasNothingProperty>().serializeNulls()
|
||||
|
||||
val toJson = HasNothingProperty()
|
||||
@@ -995,7 +1104,7 @@ class GeneratedAdaptersTest {
|
||||
jsonWriter.nullValue()
|
||||
}
|
||||
|
||||
@FromJson fun fromJson(jsonReader: JsonReader) : Nothing? {
|
||||
@FromJson fun fromJson(jsonReader: JsonReader): Nothing? {
|
||||
jsonReader.skipValue()
|
||||
return null
|
||||
}
|
||||
@@ -1010,10 +1119,14 @@ class GeneratedAdaptersTest {
|
||||
@Test fun enclosedParameterizedType() {
|
||||
val jsonAdapter = moshi.adapter<HasParameterizedProperty>()
|
||||
|
||||
assertThat(jsonAdapter.toJson(
|
||||
assertThat(
|
||||
jsonAdapter.toJson(
|
||||
HasParameterizedProperty(
|
||||
Twins("1", "2"))))
|
||||
.isEqualTo("""{"twins":{"a":"1","b":"2"}}""")
|
||||
Twins("1", "2")
|
||||
)
|
||||
)
|
||||
)
|
||||
.isEqualTo("""{"twins":{"a":"1","b":"2"}}""")
|
||||
|
||||
val hasParameterizedProperty = jsonAdapter.fromJson("""{"twins":{"a":"3","b":"4"}}""")!!
|
||||
assertThat(hasParameterizedProperty.twins.a).isEqualTo("3")
|
||||
@@ -1033,8 +1146,11 @@ class GeneratedAdaptersTest {
|
||||
assertThat(instance.AAA).isEqualTo(1)
|
||||
assertThat(instance.BBB).isEqualTo(2)
|
||||
|
||||
assertThat(adapter.toJson(
|
||||
UppercasePropertyName(3, 4))).isEqualTo("""{"AAA":3,"BBB":4}""")
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
UppercasePropertyName(3, 4)
|
||||
)
|
||||
).isEqualTo("""{"AAA":3,"BBB":4}""")
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -1064,10 +1180,10 @@ class GeneratedAdaptersTest {
|
||||
annotation class Uppercase(val inFrench: Boolean, val onSundays: Boolean = false)
|
||||
|
||||
class UppercaseJsonAdapter {
|
||||
@ToJson fun toJson(@Uppercase(inFrench = true) s: String) : String {
|
||||
@ToJson fun toJson(@Uppercase(inFrench = true) s: String): String {
|
||||
return s.toUpperCase(Locale.US)
|
||||
}
|
||||
@FromJson @Uppercase(inFrench = true) fun fromJson(s: String) : String {
|
||||
@FromJson @Uppercase(inFrench = true) fun fromJson(s: String): String {
|
||||
return s.toLowerCase(Locale.US)
|
||||
}
|
||||
}
|
||||
@@ -1077,9 +1193,10 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@Test fun nullablePrimitivesUseBoxedPrimitiveAdapters() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(JsonAdapter.Factory { type, _, _ ->
|
||||
.add(
|
||||
JsonAdapter.Factory { type, _, _ ->
|
||||
if (Boolean::class.javaObjectType == type) {
|
||||
return@Factory object:JsonAdapter<Boolean?>() {
|
||||
return@Factory object : JsonAdapter<Boolean?>() {
|
||||
override fun fromJson(reader: JsonReader): Boolean? {
|
||||
if (reader.peek() != JsonReader.Token.BOOLEAN) {
|
||||
reader.skipValue()
|
||||
@@ -1094,13 +1211,17 @@ class GeneratedAdaptersTest {
|
||||
}
|
||||
}
|
||||
null
|
||||
})
|
||||
.build()
|
||||
}
|
||||
)
|
||||
.build()
|
||||
val adapter = moshi.adapter<HasNullableBoolean>().serializeNulls()
|
||||
assertThat(adapter.fromJson("""{"boolean":"not a boolean"}"""))
|
||||
.isEqualTo(HasNullableBoolean(null))
|
||||
assertThat(adapter.toJson(
|
||||
HasNullableBoolean(null))).isEqualTo("""{"boolean":null}""")
|
||||
.isEqualTo(HasNullableBoolean(null))
|
||||
assertThat(
|
||||
adapter.toJson(
|
||||
HasNullableBoolean(null)
|
||||
)
|
||||
).isEqualTo("""{"boolean":null}""")
|
||||
}
|
||||
|
||||
@Test fun adaptersAreNullSafe() {
|
||||
@@ -1118,13 +1239,16 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<HasCollectionOfPrimitives>()
|
||||
|
||||
val encoded = HasCollectionOfPrimitives(
|
||||
listOf(1, 2, -3))
|
||||
listOf(1, 2, -3)
|
||||
)
|
||||
assertThat(adapter.toJson(encoded)).isEqualTo("""{"listOfInts":[1,2,-3]}""")
|
||||
|
||||
val decoded = adapter.fromJson("""{"listOfInts":[4,-5,6]}""")!!
|
||||
assertThat(decoded).isEqualTo(
|
||||
HasCollectionOfPrimitives(
|
||||
listOf(4, -5, 6)))
|
||||
HasCollectionOfPrimitives(
|
||||
listOf(4, -5, 6)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true, generator = "custom")
|
||||
@@ -1135,7 +1259,8 @@ class GeneratedAdaptersTest {
|
||||
val adapter = moshi.adapter<CustomGeneratedClass>()
|
||||
val unwrapped = (adapter as NullSafeJsonAdapter<CustomGeneratedClass>).delegate()
|
||||
assertThat(unwrapped).isInstanceOf(
|
||||
GeneratedAdaptersTest_CustomGeneratedClassJsonAdapter::class.java)
|
||||
GeneratedAdaptersTest_CustomGeneratedClassJsonAdapter::class.java
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true, generator = "custom")
|
||||
@@ -1176,7 +1301,7 @@ class GeneratedAdaptersTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class ClassWithFieldJson(
|
||||
@field:Json(name = "_links") val links: String
|
||||
@field:Json(name = "_links") val links: String
|
||||
) {
|
||||
@field:Json(name = "_ids") var ids: String? = null
|
||||
}
|
||||
@@ -1206,7 +1331,6 @@ class GeneratedAdaptersTest {
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DeprecatedProperty(@Deprecated("Deprecated for reasons") val foo: String)
|
||||
|
||||
|
||||
@Target(TYPE)
|
||||
annotation class TypeAnnotation
|
||||
|
||||
@@ -1216,8 +1340,8 @@ class GeneratedAdaptersTest {
|
||||
*/
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class TypeAnnotationClass(
|
||||
val propertyWithAnnotatedType: @TypeAnnotation String = "",
|
||||
val generic: List<@TypeAnnotation String>
|
||||
val propertyWithAnnotatedType: @TypeAnnotation String = "",
|
||||
val generic: List<@TypeAnnotation String>
|
||||
)
|
||||
|
||||
@Test fun typesSizeCheckMessages_noArgs() {
|
||||
@@ -1232,8 +1356,8 @@ class GeneratedAdaptersTest {
|
||||
@Test fun typesSizeCheckMessages_wrongNumberOfArgs() {
|
||||
try {
|
||||
GeneratedAdaptersTest_MultipleGenericsJsonAdapter<String, Any, Any, Any>(
|
||||
moshi,
|
||||
arrayOf(String::class.java)
|
||||
moshi,
|
||||
arrayOf(String::class.java)
|
||||
)
|
||||
fail("Should have failed to construct the adapter due to wrong number of generics")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
@@ -1249,13 +1373,13 @@ class GeneratedAdaptersTest {
|
||||
// Compile-only test
|
||||
@JsonClass(generateAdapter = true)
|
||||
internal data class MismatchParentAndNestedClassVisibility(
|
||||
val type: Int,
|
||||
val name: String? = null
|
||||
val type: Int,
|
||||
val name: String? = null
|
||||
) {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class NestedClass(
|
||||
val nestedProperty: String
|
||||
val nestedProperty: String
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1263,22 +1387,22 @@ internal data class MismatchParentAndNestedClassVisibility(
|
||||
// Compile-only test
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class KeysWithSpaces(
|
||||
@Json(name = "1. Information") val information: String,
|
||||
@Json(name = "2. Symbol") val symbol: String,
|
||||
@Json(name = "3. Last Refreshed") val lastRefreshed: String,
|
||||
@Json(name = "4. Interval") val interval: String,
|
||||
@Json(name = "5. Output Size") val size: String,
|
||||
@Json(name = "6. Time Zone") val timeZone: String
|
||||
@Json(name = "1. Information") val information: String,
|
||||
@Json(name = "2. Symbol") val symbol: String,
|
||||
@Json(name = "3. Last Refreshed") val lastRefreshed: String,
|
||||
@Json(name = "4. Interval") val interval: String,
|
||||
@Json(name = "5. Output Size") val size: String,
|
||||
@Json(name = "6. Time Zone") val timeZone: String
|
||||
)
|
||||
|
||||
// Has to be outside to avoid Types seeing an owning class
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class NullableTypeParams<T>(
|
||||
val nullableList: List<String?>,
|
||||
val nullableSet: Set<String?>,
|
||||
val nullableMap: Map<String, String?>,
|
||||
val nullableT: T?,
|
||||
val nonNullT: T
|
||||
val nullableList: List<String?>,
|
||||
val nullableSet: Set<String?>,
|
||||
val nullableMap: Map<String, String?>,
|
||||
val nullableT: T?,
|
||||
val nonNullT: T
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -1315,7 +1439,7 @@ data class SmokeTestType(
|
||||
// Compile only, regression test for https://github.com/square/moshi/issues/848
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class Hotwords(
|
||||
val `class`: List<String>?
|
||||
val `class`: List<String>?
|
||||
)
|
||||
|
||||
typealias TypeAliasName = String
|
||||
|
@@ -38,10 +38,11 @@ class MultipleMasksTest {
|
||||
|
||||
// Set some arbitrary values to make sure offsets are aligning correctly
|
||||
@Language("JSON")
|
||||
val json = """{"arg50":500,"arg3":34,"arg11":11,"arg65":67}"""
|
||||
val json =
|
||||
"""{"arg50":500,"arg3":34,"arg11":11,"arg65":67}"""
|
||||
|
||||
val instance = Moshi.Builder().build().adapter(MultipleMasks::class.java)
|
||||
.fromJson(json)!!
|
||||
.fromJson(json)!!
|
||||
|
||||
assertEquals(instance.arg2, 2)
|
||||
assertEquals(instance.arg3, 34)
|
||||
@@ -55,70 +56,70 @@ class MultipleMasksTest {
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class MultipleMasks(
|
||||
val arg0: Long = 0,
|
||||
val arg1: Long = 1,
|
||||
val arg2: Long = 2,
|
||||
val arg3: Long = 3,
|
||||
val arg4: Long = 4,
|
||||
val arg5: Long = 5,
|
||||
val arg6: Long = 6,
|
||||
val arg7: Long = 7,
|
||||
val arg8: Long = 8,
|
||||
val arg9: Long = 9,
|
||||
val arg10: Long = 10,
|
||||
val arg11: Long,
|
||||
val arg12: Long = 12,
|
||||
val arg13: Long = 13,
|
||||
val arg14: Long = 14,
|
||||
val arg15: Long = 15,
|
||||
val arg16: Long = 16,
|
||||
val arg17: Long = 17,
|
||||
val arg18: Long = 18,
|
||||
val arg19: Long = 19,
|
||||
@Suppress("UNUSED_PARAMETER") arg20: Long = 20,
|
||||
val arg21: Long = 21,
|
||||
val arg22: Long = 22,
|
||||
val arg23: Long = 23,
|
||||
val arg24: Long = 24,
|
||||
val arg25: Long = 25,
|
||||
val arg26: Long = 26,
|
||||
val arg27: Long = 27,
|
||||
val arg28: Long = 28,
|
||||
val arg29: Long = 29,
|
||||
val arg30: Long = 30,
|
||||
val arg31: Long = 31,
|
||||
val arg32: Long = 32,
|
||||
val arg33: Long = 33,
|
||||
val arg34: Long = 34,
|
||||
val arg35: Long = 35,
|
||||
val arg36: Long = 36,
|
||||
val arg37: Long = 37,
|
||||
val arg38: Long = 38,
|
||||
@Transient val arg39: Long = 39,
|
||||
val arg40: Long = 40,
|
||||
val arg41: Long = 41,
|
||||
val arg42: Long = 42,
|
||||
val arg43: Long = 43,
|
||||
val arg44: Long = 44,
|
||||
val arg45: Long = 45,
|
||||
val arg46: Long = 46,
|
||||
val arg47: Long = 47,
|
||||
val arg48: Long = 48,
|
||||
val arg49: Long = 49,
|
||||
val arg50: Long = 50,
|
||||
val arg51: Long = 51,
|
||||
val arg52: Long = 52,
|
||||
@Transient val arg53: Long = 53,
|
||||
val arg54: Long = 54,
|
||||
val arg55: Long = 55,
|
||||
val arg56: Long = 56,
|
||||
val arg57: Long = 57,
|
||||
val arg58: Long = 58,
|
||||
val arg59: Long = 59,
|
||||
val arg60: Long = 60,
|
||||
val arg61: Long = 61,
|
||||
val arg62: Long = 62,
|
||||
val arg63: Long = 63,
|
||||
val arg64: Long = 64,
|
||||
val arg65: Long = 65
|
||||
val arg0: Long = 0,
|
||||
val arg1: Long = 1,
|
||||
val arg2: Long = 2,
|
||||
val arg3: Long = 3,
|
||||
val arg4: Long = 4,
|
||||
val arg5: Long = 5,
|
||||
val arg6: Long = 6,
|
||||
val arg7: Long = 7,
|
||||
val arg8: Long = 8,
|
||||
val arg9: Long = 9,
|
||||
val arg10: Long = 10,
|
||||
val arg11: Long,
|
||||
val arg12: Long = 12,
|
||||
val arg13: Long = 13,
|
||||
val arg14: Long = 14,
|
||||
val arg15: Long = 15,
|
||||
val arg16: Long = 16,
|
||||
val arg17: Long = 17,
|
||||
val arg18: Long = 18,
|
||||
val arg19: Long = 19,
|
||||
@Suppress("UNUSED_PARAMETER") arg20: Long = 20,
|
||||
val arg21: Long = 21,
|
||||
val arg22: Long = 22,
|
||||
val arg23: Long = 23,
|
||||
val arg24: Long = 24,
|
||||
val arg25: Long = 25,
|
||||
val arg26: Long = 26,
|
||||
val arg27: Long = 27,
|
||||
val arg28: Long = 28,
|
||||
val arg29: Long = 29,
|
||||
val arg30: Long = 30,
|
||||
val arg31: Long = 31,
|
||||
val arg32: Long = 32,
|
||||
val arg33: Long = 33,
|
||||
val arg34: Long = 34,
|
||||
val arg35: Long = 35,
|
||||
val arg36: Long = 36,
|
||||
val arg37: Long = 37,
|
||||
val arg38: Long = 38,
|
||||
@Transient val arg39: Long = 39,
|
||||
val arg40: Long = 40,
|
||||
val arg41: Long = 41,
|
||||
val arg42: Long = 42,
|
||||
val arg43: Long = 43,
|
||||
val arg44: Long = 44,
|
||||
val arg45: Long = 45,
|
||||
val arg46: Long = 46,
|
||||
val arg47: Long = 47,
|
||||
val arg48: Long = 48,
|
||||
val arg49: Long = 49,
|
||||
val arg50: Long = 50,
|
||||
val arg51: Long = 51,
|
||||
val arg52: Long = 52,
|
||||
@Transient val arg53: Long = 53,
|
||||
val arg54: Long = 54,
|
||||
val arg55: Long = 55,
|
||||
val arg56: Long = 56,
|
||||
val arg57: Long = 57,
|
||||
val arg58: Long = 58,
|
||||
val arg59: Long = 59,
|
||||
val arg60: Long = 60,
|
||||
val arg61: Long = 61,
|
||||
val arg62: Long = 62,
|
||||
val arg63: Long = 63,
|
||||
val arg64: Long = 64,
|
||||
val arg65: Long = 65
|
||||
)
|
||||
|
@@ -140,7 +140,7 @@ class KotlinJsonAdapterTest {
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class KotlinJsonAdapterTest {
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
@@ -201,7 +201,7 @@ class KotlinJsonAdapterTest {
|
||||
try {
|
||||
jsonAdapter.fromJson("""{"a":4,"b":null,"b":6}""")
|
||||
fail()
|
||||
} catch(expected: JsonDataException) {
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'b' at $.b")
|
||||
}
|
||||
}
|
||||
@@ -210,9 +210,9 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun constructorParameterWithQualifier() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<ConstructorParameterWithQualifier>()
|
||||
|
||||
val encoded = ConstructorParameterWithQualifier("Android", "Banana")
|
||||
@@ -227,9 +227,9 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun propertyWithQualifier() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(UppercaseJsonAdapter())
|
||||
.build()
|
||||
val jsonAdapter = moshi.adapter<PropertyWithQualifier>()
|
||||
|
||||
val encoded = PropertyWithQualifier()
|
||||
@@ -315,9 +315,11 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<RequiredTransientConstructorParameter>()
|
||||
fail()
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage("No default value for transient constructor parameter #0 " +
|
||||
assertThat(expected).hasMessage(
|
||||
"No default value for transient constructor parameter #0 " +
|
||||
"a of fun <init>(kotlin.Int): " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest.RequiredTransientConstructorParameter")
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest.RequiredTransientConstructorParameter"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,8 +359,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<ConstructorParameterWithSameNameAsPropertyButDifferentType>()
|
||||
fail()
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage("'a' has a constructor parameter of type " +
|
||||
"kotlin.Int but a property of type kotlin.String.")
|
||||
assertThat(expected).hasMessage(
|
||||
"'a' has a constructor parameter of type " +
|
||||
"kotlin.Int but a property of type kotlin.String."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -443,7 +447,8 @@ class KotlinJsonAdapterTest {
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
"Platform class kotlin.Triple in kotlin.Triple<java.lang.Object, java.lang.Object, java.lang.Object> requires explicit JsonAdapter to be registered")
|
||||
"Platform class kotlin.Triple in kotlin.Triple<java.lang.Object, java.lang.Object, java.lang.Object> requires explicit JsonAdapter to be registered"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -547,7 +552,7 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
|
||||
class GetterOnly(var a: Int, var b: Int) {
|
||||
val total : Int
|
||||
val total: Int
|
||||
get() = a + b
|
||||
}
|
||||
|
||||
@@ -572,7 +577,7 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
|
||||
class GetterAndSetter(var a: Int, var b: Int) {
|
||||
var total : Int
|
||||
var total: Int
|
||||
get() = a + b
|
||||
set(value) {
|
||||
b = value - a
|
||||
@@ -584,10 +589,11 @@ class KotlinJsonAdapterTest {
|
||||
try {
|
||||
moshi.adapter<NonPropertyConstructorParameter>()
|
||||
fail()
|
||||
} catch(expected: IllegalArgumentException) {
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage(
|
||||
"No property for required constructor parameter #0 a of fun <init>(" +
|
||||
"kotlin.Int, kotlin.Int): ${NonPropertyConstructorParameter::class.qualifiedName}")
|
||||
"No property for required constructor parameter #0 a of fun <init>(" +
|
||||
"kotlin.Int, kotlin.Int): ${NonPropertyConstructorParameter::class.qualifiedName}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,8 +618,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<Interface>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("No JsonAdapter for interface " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$Interface (with no annotations)")
|
||||
assertThat(e).hasMessage(
|
||||
"No JsonAdapter for interface " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$Interface (with no annotations)"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -625,8 +633,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<AbstractClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("Cannot serialize abstract class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$AbstractClass")
|
||||
assertThat(e).hasMessage(
|
||||
"Cannot serialize abstract class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$AbstractClass"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -638,8 +648,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<InnerClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("Cannot serialize inner class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$InnerClass")
|
||||
assertThat(e).hasMessage(
|
||||
"Cannot serialize inner class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$InnerClass"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -652,8 +664,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<LocalClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("Cannot serialize local class or object expression " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$localClassesNotSupported\$LocalClass")
|
||||
assertThat(e).hasMessage(
|
||||
"Cannot serialize local class or object expression " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$localClassesNotSupported\$LocalClass"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,8 +677,10 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<ObjectDeclaration>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("Cannot serialize object declaration " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$ObjectDeclaration")
|
||||
assertThat(e).hasMessage(
|
||||
"Cannot serialize object declaration " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$ObjectDeclaration"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -681,9 +697,11 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter(expression.javaClass)
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("Cannot serialize local class or object expression " +
|
||||
assertThat(e).hasMessage(
|
||||
"Cannot serialize local class or object expression " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$objectExpressionsNotSupported" +
|
||||
"\$expression$1")
|
||||
"\$expression$1"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -692,14 +710,16 @@ class KotlinJsonAdapterTest {
|
||||
val jsonAdapter = moshi.adapter<ManyProperties32>()
|
||||
|
||||
val encoded = ManyProperties32(
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132)
|
||||
val json = ("""
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132
|
||||
)
|
||||
val json = (
|
||||
"""
|
||||
|{
|
||||
|"v01":101,"v02":102,"v03":103,"v04":104,"v05":105,
|
||||
|"v06":106,"v07":107,"v08":108,"v09":109,"v10":110,
|
||||
@@ -709,7 +729,8 @@ class KotlinJsonAdapterTest {
|
||||
|"v26":126,"v27":127,"v28":128,"v29":129,"v30":130,
|
||||
|"v31":131,"v32":132
|
||||
|}
|
||||
|""").trimMargin().replace("\n", "")
|
||||
|"""
|
||||
).trimMargin().replace("\n", "")
|
||||
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo(json)
|
||||
|
||||
@@ -719,27 +740,55 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
|
||||
class ManyProperties32(
|
||||
var v01: Int, var v02: Int, var v03: Int, var v04: Int, var v05: Int,
|
||||
var v06: Int, var v07: Int, var v08: Int, var v09: Int, var v10: Int,
|
||||
var v11: Int, var v12: Int, var v13: Int, var v14: Int, var v15: Int,
|
||||
var v16: Int, var v17: Int, var v18: Int, var v19: Int, var v20: Int,
|
||||
var v21: Int, var v22: Int, var v23: Int, var v24: Int, var v25: Int,
|
||||
var v26: Int, var v27: Int, var v28: Int, var v29: Int, var v30: Int,
|
||||
var v31: Int, var v32: Int)
|
||||
var v01: Int,
|
||||
var v02: Int,
|
||||
var v03: Int,
|
||||
var v04: Int,
|
||||
var v05: Int,
|
||||
var v06: Int,
|
||||
var v07: Int,
|
||||
var v08: Int,
|
||||
var v09: Int,
|
||||
var v10: Int,
|
||||
var v11: Int,
|
||||
var v12: Int,
|
||||
var v13: Int,
|
||||
var v14: Int,
|
||||
var v15: Int,
|
||||
var v16: Int,
|
||||
var v17: Int,
|
||||
var v18: Int,
|
||||
var v19: Int,
|
||||
var v20: Int,
|
||||
var v21: Int,
|
||||
var v22: Int,
|
||||
var v23: Int,
|
||||
var v24: Int,
|
||||
var v25: Int,
|
||||
var v26: Int,
|
||||
var v27: Int,
|
||||
var v28: Int,
|
||||
var v29: Int,
|
||||
var v30: Int,
|
||||
var v31: Int,
|
||||
var v32: Int
|
||||
)
|
||||
|
||||
@Test fun manyProperties33() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val jsonAdapter = moshi.adapter<ManyProperties33>()
|
||||
|
||||
val encoded = ManyProperties33(
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132, 133)
|
||||
val json = ("""
|
||||
101, 102, 103, 104, 105,
|
||||
106, 107, 108, 109, 110,
|
||||
111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120,
|
||||
121, 122, 123, 124, 125,
|
||||
126, 127, 128, 129, 130,
|
||||
131, 132, 133
|
||||
)
|
||||
val json = (
|
||||
"""
|
||||
|{
|
||||
|"v01":101,"v02":102,"v03":103,"v04":104,"v05":105,
|
||||
|"v06":106,"v07":107,"v08":108,"v09":109,"v10":110,
|
||||
@@ -749,7 +798,8 @@ class KotlinJsonAdapterTest {
|
||||
|"v26":126,"v27":127,"v28":128,"v29":129,"v30":130,
|
||||
|"v31":131,"v32":132,"v33":133
|
||||
|}
|
||||
|""").trimMargin().replace("\n", "")
|
||||
|"""
|
||||
).trimMargin().replace("\n", "")
|
||||
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo(json)
|
||||
|
||||
@@ -760,21 +810,52 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
|
||||
class ManyProperties33(
|
||||
var v01: Int, var v02: Int, var v03: Int, var v04: Int, var v05: Int,
|
||||
var v06: Int, var v07: Int, var v08: Int, var v09: Int, var v10: Int,
|
||||
var v11: Int, var v12: Int, var v13: Int, var v14: Int, var v15: Int,
|
||||
var v16: Int, var v17: Int, var v18: Int, var v19: Int, var v20: Int,
|
||||
var v21: Int, var v22: Int, var v23: Int, var v24: Int, var v25: Int,
|
||||
var v26: Int, var v27: Int, var v28: Int, var v29: Int, var v30: Int,
|
||||
var v31: Int, var v32: Int, var v33: Int)
|
||||
var v01: Int,
|
||||
var v02: Int,
|
||||
var v03: Int,
|
||||
var v04: Int,
|
||||
var v05: Int,
|
||||
var v06: Int,
|
||||
var v07: Int,
|
||||
var v08: Int,
|
||||
var v09: Int,
|
||||
var v10: Int,
|
||||
var v11: Int,
|
||||
var v12: Int,
|
||||
var v13: Int,
|
||||
var v14: Int,
|
||||
var v15: Int,
|
||||
var v16: Int,
|
||||
var v17: Int,
|
||||
var v18: Int,
|
||||
var v19: Int,
|
||||
var v20: Int,
|
||||
var v21: Int,
|
||||
var v22: Int,
|
||||
var v23: Int,
|
||||
var v24: Int,
|
||||
var v25: Int,
|
||||
var v26: Int,
|
||||
var v27: Int,
|
||||
var v28: Int,
|
||||
var v29: Int,
|
||||
var v30: Int,
|
||||
var v31: Int,
|
||||
var v32: Int,
|
||||
var v33: Int
|
||||
)
|
||||
|
||||
data class Box<out T>(val data: T)
|
||||
|
||||
@Test fun genericTypes() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val stringBoxAdapter = moshi.adapter<Box<String>>(
|
||||
Types.newParameterizedTypeWithOwner(KotlinJsonAdapterTest::class.java, Box::class.java,
|
||||
String::class.java))
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
KotlinJsonAdapterTest::class.java,
|
||||
Box::class.java,
|
||||
String::class.java
|
||||
)
|
||||
)
|
||||
assertThat(stringBoxAdapter.fromJson("""{"data":"hello"}""")).isEqualTo(Box("hello"))
|
||||
assertThat(stringBoxAdapter.toJson(Box("hello"))).isEqualTo("""{"data":"hello"}""")
|
||||
}
|
||||
@@ -784,18 +865,19 @@ class KotlinJsonAdapterTest {
|
||||
@Test fun nestedGenericTypes() {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val type = Types.newParameterizedTypeWithOwner(
|
||||
KotlinJsonAdapterTest::class.java,
|
||||
NestedGenerics::class.java,
|
||||
String::class.java,
|
||||
Int::class.javaObjectType,
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
KotlinJsonAdapterTest::class.java,
|
||||
NestedGenerics::class.java,
|
||||
String::class.java,
|
||||
Int::class.javaObjectType,
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
KotlinJsonAdapterTest::class.java,
|
||||
Box::class.java,
|
||||
String::class.java
|
||||
)
|
||||
Box::class.java,
|
||||
String::class.java
|
||||
)
|
||||
)
|
||||
val adapter = moshi.adapter<NestedGenerics<String, Int, Box<String>>>(type).indent(" ")
|
||||
val json = """
|
||||
val json =
|
||||
"""
|
||||
|{
|
||||
| "value": {
|
||||
| "hello": {
|
||||
@@ -818,16 +900,18 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mixingReflectionAndCodegen() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
val generatedAdapter = moshi.adapter<UsesGeneratedAdapter>()
|
||||
val reflectionAdapter = moshi.adapter<UsesReflectionAdapter>()
|
||||
|
||||
assertThat(generatedAdapter.toString())
|
||||
.isEqualTo("GeneratedJsonAdapter(KotlinJsonAdapterTest.UsesGeneratedAdapter).nullSafe()")
|
||||
.isEqualTo("GeneratedJsonAdapter(KotlinJsonAdapterTest.UsesGeneratedAdapter).nullSafe()")
|
||||
assertThat(reflectionAdapter.toString())
|
||||
.isEqualTo("KotlinJsonAdapter(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest" +
|
||||
".UsesReflectionAdapter).nullSafe()")
|
||||
.isEqualTo(
|
||||
"KotlinJsonAdapter(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest" +
|
||||
".UsesReflectionAdapter).nullSafe()"
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -841,10 +925,10 @@ class KotlinJsonAdapterTest {
|
||||
annotation class Uppercase
|
||||
|
||||
class UppercaseJsonAdapter {
|
||||
@ToJson fun toJson(@Uppercase s: String) : String {
|
||||
@ToJson fun toJson(@Uppercase s: String): String {
|
||||
return s.toUpperCase(Locale.US)
|
||||
}
|
||||
@FromJson @Uppercase fun fromJson(s: String) : String {
|
||||
@FromJson @Uppercase fun fromJson(s: String): String {
|
||||
return s.toLowerCase(Locale.US)
|
||||
}
|
||||
}
|
||||
@@ -853,9 +937,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun nullablePrimitivesUseBoxedPrimitiveAdapters() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(JsonAdapter.Factory { type, _, _ ->
|
||||
.add(
|
||||
JsonAdapter.Factory { type, _, _ ->
|
||||
if (Boolean::class.javaObjectType == type) {
|
||||
return@Factory object: JsonAdapter<Boolean?>() {
|
||||
return@Factory object : JsonAdapter<Boolean?>() {
|
||||
override fun fromJson(reader: JsonReader): Boolean? {
|
||||
if (reader.peek() != JsonReader.Token.BOOLEAN) {
|
||||
reader.skipValue()
|
||||
@@ -870,19 +955,20 @@ class KotlinJsonAdapterTest {
|
||||
}
|
||||
}
|
||||
null
|
||||
})
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
}
|
||||
)
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
val adapter = moshi.adapter<HasNullableBoolean>().serializeNulls()
|
||||
assertThat(adapter.fromJson("""{"boolean":"not a boolean"}"""))
|
||||
.isEqualTo(HasNullableBoolean(null))
|
||||
.isEqualTo(HasNullableBoolean(null))
|
||||
assertThat(adapter.toJson(HasNullableBoolean(null))).isEqualTo("""{"boolean":null}""")
|
||||
}
|
||||
|
||||
@Test fun adaptersAreNullSafe() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
|
||||
// TODO in CR: We had to mark this as nullable, vs before the jsonadapter factory would always run
|
||||
val adapter = moshi.adapter<HasNullableBoolean?>()
|
||||
@@ -904,9 +990,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToStandardReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToStandardReflection::class.java,
|
||||
"""{"map":{"key":"value"}}""",
|
||||
MapOfStringToStandardReflection(mapOf("key" to "value")))
|
||||
MapOfStringToStandardReflection::class.java,
|
||||
"""{"map":{"key":"value"}}""",
|
||||
MapOfStringToStandardReflection(mapOf("key" to "value"))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -914,9 +1001,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToStandardCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToStandardCodegen::class.java,
|
||||
"""{"map":{"key":"value"}}""",
|
||||
MapOfStringToStandardCodegen(mapOf("key" to "value")))
|
||||
MapOfStringToStandardCodegen::class.java,
|
||||
"""{"map":{"key":"value"}}""",
|
||||
MapOfStringToStandardCodegen(mapOf("key" to "value"))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -925,9 +1013,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToEnumReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToEnumReflection::class.java,
|
||||
"""{"map":{"key":"A"}}""",
|
||||
MapOfStringToEnumReflection(mapOf("key" to KotlinEnum.A)))
|
||||
MapOfStringToEnumReflection::class.java,
|
||||
"""{"map":{"key":"A"}}""",
|
||||
MapOfStringToEnumReflection(mapOf("key" to KotlinEnum.A))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -935,9 +1024,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToEnumCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToEnumCodegen::class.java,
|
||||
"""{"map":{"key":"A"}}""",
|
||||
MapOfStringToEnumCodegen(mapOf("key" to KotlinEnum.A)))
|
||||
MapOfStringToEnumCodegen::class.java,
|
||||
"""{"map":{"key":"A"}}""",
|
||||
MapOfStringToEnumCodegen(mapOf("key" to KotlinEnum.A))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -946,9 +1036,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToCollectionReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToCollectionReflection::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToCollectionReflection(mapOf("key" to listOf())))
|
||||
MapOfStringToCollectionReflection::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToCollectionReflection(mapOf("key" to listOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -956,9 +1047,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToCollectionCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToCollectionCodegen::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToCollectionCodegen(mapOf("key" to listOf())))
|
||||
MapOfStringToCollectionCodegen::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToCollectionCodegen(mapOf("key" to listOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -967,9 +1059,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToMapReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToMapReflection::class.java,
|
||||
"""{"map":{"key":{}}}""",
|
||||
MapOfStringToMapReflection(mapOf("key" to mapOf())))
|
||||
MapOfStringToMapReflection::class.java,
|
||||
"""{"map":{"key":{}}}""",
|
||||
MapOfStringToMapReflection(mapOf("key" to mapOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -977,9 +1070,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToMapCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToMapCodegen::class.java,
|
||||
"""{"map":{"key":{}}}""",
|
||||
MapOfStringToMapCodegen(mapOf("key" to mapOf())))
|
||||
MapOfStringToMapCodegen::class.java,
|
||||
"""{"map":{"key":{}}}""",
|
||||
MapOfStringToMapCodegen(mapOf("key" to mapOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -988,9 +1082,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToArrayReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToArrayReflection::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToArrayReflection(mapOf("key" to arrayOf())))
|
||||
MapOfStringToArrayReflection::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToArrayReflection(mapOf("key" to arrayOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -998,9 +1093,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToArrayCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToArrayCodegen::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToArrayCodegen(mapOf("key" to arrayOf())))
|
||||
MapOfStringToArrayCodegen::class.java,
|
||||
"""{"map":{"key":[]}}""",
|
||||
MapOfStringToArrayCodegen(mapOf("key" to arrayOf()))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -1009,9 +1105,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToClassReflectionWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToClassReflection::class.java,
|
||||
"""{"map":{"key":{"a":19,"b":42}}}""",
|
||||
MapOfStringToClassReflection(mapOf("key" to ConstructorParameters(19, 42))))
|
||||
MapOfStringToClassReflection::class.java,
|
||||
"""{"map":{"key":{"a":19,"b":42}}}""",
|
||||
MapOfStringToClassReflection(mapOf("key" to ConstructorParameters(19, 42)))
|
||||
)
|
||||
}
|
||||
|
||||
@JvmSuppressWildcards(suppress = false)
|
||||
@@ -1019,9 +1116,10 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun mapOfStringToClassCodegenWildcards() {
|
||||
mapWildcardsParameterizedTest(
|
||||
MapOfStringToClassCodegen::class.java,
|
||||
"""{"map":{"key":{"a":19,"b":42}}}""",
|
||||
MapOfStringToClassCodegen(mapOf("key" to ConstructorParameters(19, 42))))
|
||||
MapOfStringToClassCodegen::class.java,
|
||||
"""{"map":{"key":{"a":19,"b":42}}}""",
|
||||
MapOfStringToClassCodegen(mapOf("key" to ConstructorParameters(19, 42)))
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@@ -1030,8 +1128,8 @@ class KotlinJsonAdapterTest {
|
||||
|
||||
@Test fun sealedClassesAreRejected() {
|
||||
val moshi = Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
|
||||
try {
|
||||
moshi.adapter<SealedClass>()
|
||||
|
Reference in New Issue
Block a user