1

If a have an anonymous class like:

val a = object {
   val is_something = "some value"
   val something = "other value"
}

and call

println(ObjectMapper().writeValueAsString(a))

the result would be

"{"something":"other value"}"

And it's like this for all variables that begin with "is_". Why?

Correction, it doesn't ignore it. It takes off the "is" and moves the variable to the end of the string. So here the result would be

"{"something":"other value","_something":"some value"}"

Still, why does it do that?

  • this is some weird kind of Java – vladtkachuk Jul 01 '22 at 12:55
  • Does this answer your question? [Jackson renames primitive boolean field by removing 'is'](https://stackoverflow.com/questions/32270422/jackson-renames-primitive-boolean-field-by-removing-is) – vladtkachuk Jul 01 '22 at 13:23
  • Not fully. I still don't get why does it add a duplicate property without the "is" at the end –  Jul 01 '22 at 14:21
  • it does not add a duplicate, you have two properties - `is_something` and `something`, so both are serialised, but `is_something` has a weird behavior as described in the answer – vladtkachuk Jul 04 '22 at 06:49
  • No, I was getting is_something, something and _something at the end, so it is dupicated –  Jul 04 '22 at 07:18

2 Answers2

0

The issue was solved by adding @JsonProperty on top of "is_something" and capitalizing it. So the object would look like

val a = object {
   @JsonProperty 
   val Is_something = "some value"
   val something = "other value"
}

Still have no idea what caused the problem

  • 1
    it is just a matter of googling correcty :) here is your explanation: https://stackoverflow.com/questions/32270422/jackson-renames-primitive-boolean-field-by-removing-is – vladtkachuk Jul 01 '22 at 13:21
0

Jackson apparently scans the class that you pass in for getters and setters and then use those methods for serialization and deserialization.

"Get", "Set", "is" are eliminated and what remains in those methods will be used as Json field.

Hence your "is_something" is changed into "_something" and so on.

Asgar
  • 1,920
  • 2
  • 8
  • 17