mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
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:
@@ -390,8 +390,7 @@ private fun declaredProperties(
|
|||||||
propertySpec = property,
|
propertySpec = property,
|
||||||
parameter = parameter,
|
parameter = parameter,
|
||||||
visibility = property.modifiers.visibility(),
|
visibility = property.modifiers.visibility(),
|
||||||
jsonName = parameter?.jsonName ?: property.annotations.jsonName()
|
jsonName = parameter?.jsonName ?: property.annotations.jsonName() ?: name,
|
||||||
?: name.escapeDollarSigns(),
|
|
||||||
jsonIgnore = isIgnored
|
jsonIgnore = isIgnored
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -517,10 +516,6 @@ private fun <T> AnnotationSpec.elementValue(name: String): T? {
|
|||||||
}?.value?.value as? T
|
}?.value?.value as? T
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.escapeDollarSigns(): String {
|
|
||||||
return replace("\$", "\${\'\$\'}")
|
|
||||||
}
|
|
||||||
|
|
||||||
internal val TypeElement.metadata: Metadata
|
internal val TypeElement.metadata: Metadata
|
||||||
get() {
|
get() {
|
||||||
return getAnnotation(Metadata::class.java)
|
return getAnnotation(Metadata::class.java)
|
||||||
|
@@ -243,8 +243,7 @@ private fun declaredProperties(
|
|||||||
propertySpec = propertySpec,
|
propertySpec = propertySpec,
|
||||||
parameter = parameter,
|
parameter = parameter,
|
||||||
visibility = property.getVisibility().toKModifier() ?: KModifier.PUBLIC,
|
visibility = property.getVisibility().toKModifier() ?: KModifier.PUBLIC,
|
||||||
jsonName = parameter?.jsonName ?: property.jsonName()
|
jsonName = parameter?.jsonName ?: property.jsonName() ?: name,
|
||||||
?: name.escapeDollarSigns(),
|
|
||||||
jsonIgnore = isTransient || parameter?.jsonIgnore == true || property.jsonIgnore()
|
jsonIgnore = isTransient || parameter?.jsonIgnore == true || property.jsonIgnore()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -279,7 +278,3 @@ private fun KSPropertyDeclaration.toPropertySpec(
|
|||||||
}
|
}
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.escapeDollarSigns(): String {
|
|
||||||
return replace("\$", "\${\'\$\'}")
|
|
||||||
}
|
|
||||||
|
@@ -743,6 +743,22 @@ class DualKotlinTest {
|
|||||||
this.b = b
|
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
|
typealias TypeAlias = Int
|
||||||
|
Reference in New Issue
Block a user