Improve the way Json.name is fetched for codegen (#1059)

* Improve the way Json.name is fetched for codegen

* Remove the comment for jsonName

* Add a test for json names with quotation marks

* Fix the broken tests
This commit is contained in:
Nicklas Ansman Giertz
2020-01-10 21:32:23 -05:00
committed by Zac Sweers
parent 365e955381
commit cc4b5b3ad2
3 changed files with 28 additions and 9 deletions

View File

@@ -87,6 +87,26 @@ class GeneratedAdaptersTest {
@JsonClass(generateAdapter = true)
data class JsonAnnotationWithDollarSign(@Json(name = "\$foo") val bar: String)
@Test
fun jsonAnnotationWithQuotationMark() {
val adapter = moshi.adapter<JsonAnnotationWithQuotationMark>()
// Read
val json = """{"\"foo\"": "bar"}"""
val instance = adapter.fromJson(json)!!
assertThat(instance.bar).isEqualTo("bar")
// Write
val expectedJson = """{"\"foo\"":"baz"}"""
assertThat(adapter.toJson(
JsonAnnotationWithQuotationMark("baz"))).isEqualTo(expectedJson)
}
@JsonClass(generateAdapter = true)
data class JsonAnnotationWithQuotationMark(@Json(name = "\"foo\"") val bar: String)
@Test
fun defaultValues() {
val adapter = moshi.adapter<DefaultValues>()