question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

First two characters of property name must be lowercase due to Java restrictions

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
akosmenyhertcommented, Aug 30, 2018

Use the @JsonProperty annotation to the accessor methods.

For example: @get:JsonProperty("ID") val id: String = ""

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found