2

There is a very simple class:

class Price(
    @JsonProperty("YPRICE")
    val yprice: String? = null,

    @JsonProperty("ZPRICE")
    val zPrice: String? = null
)

And the following code to serialize to string:

val mapper = ObjectMapper().registerKotlinModule()
mapper.writeValue(System.out, Price())

The result in console is:

{"YPRICE":null,"zprice":null}

If changing the property of zPrice to zprice, then the result changes to:

{"YPRICE":null,"ZPRICE":null}

And if changing the property of yprice to yPrice, then the result changes to:

{"yprice":null,"zprice":null}

It seems that @JsonProperty does not work for the camel case properties.

Tonny Tc
  • 852
  • 1
  • 12
  • 37
  • Not sure how it is in javascript but, in java you might be able to just configure `mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);` – duppydodah Aug 02 '21 at 14:54

1 Answers1

3

You need to instruct ObjectMapper to generate JSON properties based on fields and not based on getter methods. You can use com.fasterxml.jackson.annotation.JsonAutoDetect annotation:

@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.ANY)
class Price(

Since now, in all cases you should see the same result.

Take a look at:

Richa Shah
  • 930
  • 3
  • 12
  • 27
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thanks a lot. It works. But may I know the reason for the case please? Why it shows different result for camel case and not. – Tonny Tc Aug 03 '21 at 01:26