jackson-module-kotlin: @JsonProperty is ignored on data class properties with names starting with "is"
Hi,
I’ve got a data class with a boolean property called isPublic
. This property needs to be called exactly like that in JSON. However, since the property name starts with is
, the prefix is stripped from the name. Adding @JsonProperty("isPublic")
does not work either:
class JacksonTest {
@Test
fun serializeTest(){
val objectMapper = ObjectMapper()
objectMapper.registerKotlinModule()
val instance = MyClass(true)
val json = objectMapper.writeValueAsString(instance)
assertEquals("""{"isPublic":true}""", json.replace("""\s+""".toRegex(), ""))
}
data class MyClass(
@JsonProperty("isPublic")
val isPublic: Boolean
)
}
… fails with an assertion error:
java.lang.AssertionError: Expected <{"isPublic":true}>, actual <{"public":true}>.
It doesn’t matter what you pass into the @JsonProperty
annotation, in the JSON output the field is always (!!) called public
.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 7
- Comments: 17 (4 by maintainers)
Links to this issue
Commits related to this issue
- Fixing is* prefix issue. Refer: 1. https://github.com/FasterXML/jackson-module-kotlin/issues/237#issuecomment-775519984 2. https://github.com/FasterXML/jackson-module-kotlin/issues/453#issuecomment-8... — committed to 5AbhishekSaxena/print-ms by 5AbhishekSaxena 2 years ago
- Update release note wrt #237 — committed to k163377/jackson-module-kotlin by k163377 a year ago
- Update release note wrt #237 — committed to FasterXML/jackson-module-kotlin by k163377 a year ago
Use @get:JsonProperty(“abc”) and @param:JsonProperty(“abc”) annotation on Boolean field instead of @JsonProperty(“abc”)
Changing @JsonProperty(“fieldname”) to @field:JsonProperty(“fieldname”) resolves the issue for data classes’ “is” (Boolean) properties.
The only workaround I’ve found is to rename the field in your (data) class, and then expose it with
@JsonProperty
like so:That worked for me, YMMV.
Hi, you are welcome)
What version of the library do you use?)
Same! Any solution? I thought that I made mistake somewhere in the configuration.
My code:
Response: