First two characters of property name must be lowercase due to Java restrictions
See original GitHub issueI discovered today by chance that an old restriction from Java means that you cannot have uppercase characters in the first two characters of a JSON/Kotlin property.
Consider this self-contained test case:
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Assert
import org.junit.Test
class DataClassTest2 {
data class DataClass1(val zName: String)
data class DataClass2(val zname: String)
private val jsonData1 = """
{
"zName": "my name"
}
"""
private val jsonData2 = """
{
"zname": "my name"
}
"""
@Test
fun testJsonParsing1() {
val mapper = jacksonObjectMapper()
val dataClass1 = mapper.readValue<DataClass1>(jsonData1)
Assert.assertEquals("my name", dataClass1.zName)
}
@Test
fun testJsonParsing2() {
val mapper = jacksonObjectMapper()
val dataClass2 = mapper.readValue<DataClass2>(jsonData2)
Assert.assertEquals("my name", dataClass2.zname)
}
}
The first test fails and the second passes.
This is because of this and by reference this.
However in the world of Kotlin, we are several steps removed from Java beans or indeed the underlying setters/getters that Kotlin automatically generates, and this behaviour doesn’t make a great deal of sense. It was only by accident that I discovered it.
Please consider whether there’s an opportunity to bypass this issue via the Kotlin module.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (5 by maintainers)
Top Results From Across the Web
java - Why does PropertyDescriptor return a property name ...
Since the first two characters are upper-case, the first character will not be lower-cased. As such, the property name that will be derived...
Read more >9. Naming Conventions - Oracle
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital...
Read more >Character Properties, Case Mappings & Names FAQ - Unicode
Character Properties, Case Mappings and Names. ... Case folding in Unicode is primarily based on the lowercase mapping, but includes additional changes to ......
Read more >Google JavaScript Style Guide
File names must be all lowercase and may include underscores ( _ ) or dashes ( - ), but no additional punctuation. Follow...
Read more >Grammar and types - JavaScript - MDN Web Docs - Mozilla
JavaScript borrows most of its syntax from Java, C, and C++, ... The names of variables, called identifiers, conform to certain rules.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Use the
@JsonProperty
annotation to the accessor methods.For example:
@get:JsonProperty("ID") val id: String = ""
This can be useful: https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations