Issues with serialising LocalDateTime object in Kotlin

906 views
Skip to first unread message

Birajendu Sahu

unread,
Jan 10, 2018, 11:37:06 PM1/10/18
to jackson-user
I am using com.fasterxml.jackson.core:jackson-databind gradle plugin in my Kotlin application to serialise my data classes in to json objects.

My class looks like this:

data class MyEntityClass(
var id: Long,
var name: String,
var description: String? = null,
var updatedAt: LocalDateTime,
var createdAt: LocalDateTime
) 

When I serialise this class using  jackson object mapper I am getting a json in below format

{
  "id" : 10,
  "name" : "name10",
  "description" : "description10",
  "updatedAt" : {
    "nano" : 148000000,
    "year" : 2018,
    "month" : "JANUARY",
    "dayOfMonth" : 10,
    "dayOfWeek" : "WEDNESDAY",
    "dayOfYear" : 10,
    "hour" : 22,
    "minute" : 26,
    "second" : 54,
    "monthValue" : 1,
    "chronology" : {
      "id" : "ISO",
      "calendarType" : "iso8601"
    }
  },
  "createdAt" : {
    "nano" : 148000000,
    "year" : 2018,
    "month" : "JANUARY",
    "dayOfMonth" : 10,
    "dayOfWeek" : "WEDNESDAY",
    "dayOfYear" : 10,
    "hour" : 22,
    "minute" : 26,
    "second" : 54,
    "monthValue" : 1,
    "chronology" : {
      "id" : "ISO",
      "calendarType" : "iso8601"
    }
  }
}

How do I make updatedAt and createdAt as single string entry in resulting json.

Tatu Saloranta

unread,
Jan 11, 2018, 6:22:40 PM1/11/18
to jackson-user
On Wed, Jan 10, 2018 at 3:03 PM, Birajendu Sahu <bira...@gmail.com> wrote:
> I am using com.fasterxml.jackson.core:jackson-databind gradle plugin in my
> Kotlin application to serialise my data classes in to json objects.
>
> My class looks like this:
>
> data class MyEntityClass(
> var id: Long,
> var name: String,
> var description: String? = null,
> var updatedAt: LocalDateTime,
> var createdAt: LocalDateTime
>
> )
>
> When I serialise this class using jackson object mapper I am getting a json
> in below format

Since `LocalDateTime` is Java 8 date/time type, and since Jackson 2.x
does not require Java 8 (runs on Java 6, compiles on Java 7),
it does not provide support directly. You need to use Java 8 date/time
module from:

https://github.com/FasterXML/jackson-modules-java8/

and artifact name is `jackson-datatype-jsr310` (for Maven dependency
or to get jar).
Module is registered with

ObjectMapper mapper = ...;
mapper.registerModule(new JavaTimeModule());

Without adding this module databind just sees a weird POJO (with
getters) and serializes it that way.

-+ 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.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages