简体   繁体   中英

What are the default Jsonb serialization format for java date objects

What are the date/time formats jsonb uses by default for the various date objects of the java apis?

For instance, javascript's new Date().toJSON() produces output containing second fractions I was expecting to parse using Jsonb to a ZonedDateTime field, but second fractions are not supported by the default serializer.

Do I have to specify a custom format to map javascript Date ?

TheJsonb spec specifies the default mapping implementations should support in section 3.5 Date:

Implementations MUST support binding of the following standard Java date/time classes:

  • java.util.Date
  • java.util.Calendar
  • java.util.GregorianCalendar
  • java.util.TimeZone
  • java.util.SimpleTimeZone
  • java.time.Instant
  • java.time.Duration
  • java.time.Period
  • java.time.LocalDate
  • java.time.LocalTime
  • java.time.LocalDateTime
  • java.time.ZonedDateTime
  • java.time.ZoneId
  • java.time.ZoneOffset
  • java.time.OffsetDateTime
  • java.time.OffsetTime

A javaindeed article lists the various formatters used for them along with example output:

{
  "date":               "2016-07-27T23:07:41",
  "calendar":           "2016-07-27T23:07:41.782+02:00[Europe/Copenhagen]",
  "gregorianCalendar":  "2016-07-27T23:07:41.801+02:00[Europe/Copenhagen]",
  "timeZone":           "Europe/Copenhagen",
  "simpleTimeZone":     "Europe/Paris",
  "instant":            "2016-07-27T21:07:41.806Z",
  "duration":           "PT96H",
  "period":             "P3M",
  "localDate":          "2016-07-27",
  "localTime":          "23:07:41.807",
  "localDateTime":      "2016-07-27T23:07:41.807",
  "zonedDateTime":      "2016-07-27T23:07:41.807+02:00[Europe/Copenhagen]",
  "zoneId":             "Europe/Copenhagen",
  "zoneOffset":         "+03:30",
  "offsetDateTime":     "2016-07-27T23:07:41.807+02:00",
  "offsetTime":         "23:07:41.807+02:00",
}

In my case, a java.time.Instant instance would be deserialized correctly from javascript's Date.toJSON() output string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM