Skip to content

Serialize ZonedDateTime to RFC3339 instead of ISO-8601 #200

@zhxnlai

Description

@zhxnlai

Problem

According to the spec for JSON event format, timestamp should be mapped to the RFC3339 format.

However, in the Java SDK, CloudEvent V1 models the time attribute using ZonedDateTime, which follows the ISO-8601 standard and gets mapped to ISO-8601 by jackson.

I recently ran into an edge case where the Go SDK fails to parse the timestamps serialized by the Java SDK.

Here is how to reproduce it:

val event = CloudEventBuilder.v1()
   .withTime(ZonedDateTime.now())
   // ...
   .build()

val bytes = EventFormatProvider.getInstance()
  .resolveFormat(JsonFormat.CONTENT_TYPE)!!
  .serialize(event)

The serialized timestamp includes the timezone name, which is not specified in RFC3339.

{
  "specversion":"1.0",
  // ...
  "time":"2020-07-16T13:53:54.545218America/Los_Angeles"
}

As the result, the Go SDK fails to deserialize it:

failed to unmarshall JSON event: cannot convert "2020-07-16T13:53:54.545218America/Los_Angeles" to time.Time: not in RFC3339 format

Temporary solution

Do not include ZoneId in your ZonedDateTime.

val event = CloudEventBuilder.v1()
   .withTime(OffsetDateTime.now().toZonedDateTime())
   // ...
   .build()

This is compatible with RFC3339:

{
  "specversion":"1.0",
  // ...
  "time":"2020-07-16T13:53:54.545218-07:00"
}

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions