Don't mangle property names that contain a dollar sign (#1446)

Without this fix the new test fails like this:

    value of: toJson(...)
    expected: {"$a":"apple","$b":"banana"}
    but was : {"${'$'}a":"apple","$b":"banana"}
This commit is contained in:
Jesse Wilson
2021-12-05 21:07:27 -05:00
committed by GitHub
parent fb5dd08168
commit bc441ad7f3
3 changed files with 18 additions and 12 deletions

View File

@@ -743,6 +743,22 @@ class DualKotlinTest {
this.b = b
}
}
@Test fun propertyNameHasDollarSign() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter<PropertyWithDollarSign>()
val value = PropertyWithDollarSign("apple", "banana")
val json = """{"${'$'}a":"apple","${'$'}b":"banana"}"""
assertThat(jsonAdapter.toJson(value)).isEqualTo(json)
assertThat(jsonAdapter.fromJson(json)).isEqualTo(value)
}
@JsonClass(generateAdapter = true)
data class PropertyWithDollarSign(
val `$a`: String,
@Json(name = "\$b") val b: String
)
}
typealias TypeAlias = Int