Why jackson renames my object properties?

22 views
Skip to first unread message

Ahmed Kamal

unread,
May 21, 2019, 2:20:57 PM5/21/19
to jackson-user
I have a data class like the following

data class User(val id: String,
                          val name: String,
                          val isVerified: Boolean)

When I'm trying to serialize it using io.vertx.core.json.Json.encode(user), "which is uses Jackson underneath" I expect the result to be

{
  "id": "",
  "name": "",
  "isVerified": false
}

but I got

{
  "id": "",
  "name": "",
  "verified": false
}

Tatu Saloranta

unread,
May 21, 2019, 2:51:20 PM5/21/19
to jackson-user
That does not look like Java. Is it Kotlin?

If so, I think there are a few issues filed:


that talk about this problem.

Fundamentally I think this stems from the fact that Java Bean convention considers

   boolean isSomeProperty()

to imply property "someProperty" -- similar to `getSomeProperty()`, but just for booleans/Booleans.

But same does NOT apply to fields, at least on pure Java side. However, if Kotlin creates getters perhaps logical property name is not properly resolved.

I do not know enough about Kotlin or Kotlin module to comment more unfortunately, so I would suggest you look at reported issues, discussion there.

-+ Tatu +-



--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/48889489-58d1-4b69-8fb8-58844bf1ec19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tamás Cservenák

unread,
May 21, 2019, 6:48:02 PM5/21/19
to jackso...@googlegroups.com
If kotlin, and hence vert-x kotlin integration, I don't think jackson-module-kotlin is involved at all...

As I see sources:

And it seems to me that vertx-lang-kotlin uses JSON from vertx core, that is here:



Tamás Cservenák

unread,
May 21, 2019, 6:48:03 PM5/21/19
to jackso...@googlegroups.com
Google is your friend:
https://stackoverflow.com/questions/32270422/jackson-renames-primitive-boolean-field-by-removing-is

But vert.x "wraps" Jackson, unsure are you able to configure or even reach Mapper to apply these changes.

HTH,
T

Tatu Saloranta

unread,
May 21, 2019, 7:02:42 PM5/21/19
to jackson-user
On Tue, May 21, 2019 at 3:48 PM Tamás Cservenák <ta...@cservenak.net> wrote:
>
> If kotlin, and hence vert-x kotlin integration, I don't think jackson-module-kotlin is involved at all...

Interesting -- I would not recommend use of Kotlin values without the
module but if it works (minus this issue) I guess that's nice.

But as to the problem, as per SO question, it is not actually that
`field` is renamed but combination of field implying "isSuccess" --
but not being visible to be auto-detected -- and getter implying
"success". So field is effectively ignored.

As answer suggests there are couple of ways of dealing with this;
renaming is one way, but changing introspection visibility could also
work (without needing annotations) by re-configuring two things:

1. Making all fields (including `private`) visible
2. Blocking introspection of "is-getters"

Auto-detection is covered f.ex at:

https://www.logicbig.com/tutorials/misc/jackson/json-auto-detect.html
https://www.baeldung.com/jackson-field-serializable-deserializable-or-not

but note that there are 2 different ways to change it:

1. Per-class using `@JsonAutoDetect`
2. Globally, as defaults, via `ObjectMapper.setVisibility()`

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages