sdk-java icon indicating copy to clipboard operation
sdk-java copied to clipboard

JSON serialization - extensions of types OffsetDateTime and bytes[] deserialized as Strings

Open pawel-boguski-ds opened this issue 7 months ago • 1 comments

Extensions of types OffsetDateTime and bytes[] are not restored properly after serialization and deserialization using JSON format. Values are Strings.

class JsonSerializationTest {

  final CloudEventJsonSerializer serializer = new CloudEventJsonSerializer();
  final CloudEventJsonDeserializer deserializer = new CloudEventJsonDeserializer();

  @Test
  void shouldSerializeAndDeserialize() {
    CloudEvent serialized = createTestEvent();

    Buffer buffer = serializer.serialize(serialized);
    CloudEvent deserialized = deserializer.deserialize(buffer);

    assertEvent(serialized, deserialized);
  }

  private CloudEvent createTestEvent() {
    return CloudEventBuilder.v1()
        .withId("id")
        .withSource(URI.create("source"))
        .withType("type")
        .withDataContentType("datacontenttype")
        .withDataSchema(URI.create("https://data/schema"))
        .withSubject("subject")
        .withTime(OffsetDateTime.now(ZoneId.of("UTC")))
        .withExtension("extstring", "extstring-value")
        .withExtension("extboolean", true)
        .withExtension("extint", 123)
        // Not working, deserialized value is string
        // .withExtension("exttime", OffsetDateTime.now(ZoneId.of("UTC")))
        // Not working, deserialized value is string
        // .withExtension("extbytes", "bytes".getBytes(StandardCharsets.UTF_8))
        .withData("test data".getBytes(StandardCharsets.UTF_8))
        .build();
  }

  private void assertEvent(CloudEvent expected, CloudEvent actual) {
    assertNotNull(actual);
    assertEquals(expected.getId(), actual.getId());
    assertEquals(expected.getSource(), actual.getSource());
    assertEquals(expected.getSpecVersion(), actual.getSpecVersion());
    assertEquals(expected.getType(), actual.getType());
    assertEquals(expected.getDataContentType(), actual.getDataContentType());
    assertEquals(expected.getDataSchema(), actual.getDataSchema());
    assertEquals(expected.getSubject(), actual.getSubject());
    assertEquals(expected.getTime(), actual.getTime());
    assertEquals(expected.getData(), actual.getData());
    assertEquals(expected.getExtensionNames(), actual.getExtensionNames());
    expected.getExtensionNames().forEach(extName -> {
          if (expected.getExtension(extName) instanceof byte[]) {
            assertInstanceOf(byte[].class, actual.getExtension(extName),
                "Extension " + extName + " is not a byte[]");
            assertArrayEquals((byte[]) expected.getExtension(extName),
                (byte[]) actual.getExtension(extName),
                "Values not equal for extension " + extName);
          } else {
            assertEquals(expected.getExtension(extName), actual.getExtension(extName),
                "Values not equal for extension " + extName);
          }
        }
    );
  }
}
public class CloudEventJsonSerializer implements Serializer<CloudEvent> {

  @Override
  public boolean handles(Object payload) {
    return payload instanceof CloudEvent;
  }

  @Override
  public Buffer serialize(CloudEvent payload) {
    byte[] serialized = Objects.requireNonNull(
            EventFormatProvider
                .getInstance()
                .resolveFormat(ContentType.JSON),
            "JSON event format dependency missing")
        .serialize(payload);
    return Buffer.buffer(serialized);
  }
}
public class CloudEventJsonDeserializer implements Deserializer<CloudEvent> {

  @Override
  public CloudEvent deserialize(Buffer payload) {
    return Objects.requireNonNull(
            EventFormatProvider
                .getInstance()
                .resolveFormat(ContentType.JSON),
            "JSON event format dependency missing")
        .deserialize(payload.getBytes());
  }
}

pawel-boguski-ds avatar Jul 03 '25 06:07 pawel-boguski-ds

I came across this too, but have concluded that this is behaving correctly according to the spec: https://github.com/cloudevents/spec/blob/v1.0/json-format.md#22-type-system-mapping

It's only possible to preserve string, number and boolean with JSON. All other types aren't supported.

hnphan avatar Oct 15 '25 15:10 hnphan