From d29f8876ef473acd3e07fcf469a2c1aa2b2070fa Mon Sep 17 00:00:00 2001 From: JFronny Date: Thu, 25 Apr 2024 14:21:18 +0200 Subject: [PATCH] fix(serialize-databind): actually implement TypeAdapterRuntimeTypeWrapper --- .../TypeAdapterRuntimeTypeWrapper.java | 20 ++++++++-- .../databind/test/MapAdapterTest.java | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 commons-serialize-databind/src/test/java/io/gitlab/jfronny/commons/serialize/databind/test/MapAdapterTest.java diff --git a/commons-serialize-databind/src/main/java/io/gitlab/jfronny/commons/serialize/databind/impl/adapter/TypeAdapterRuntimeTypeWrapper.java b/commons-serialize-databind/src/main/java/io/gitlab/jfronny/commons/serialize/databind/impl/adapter/TypeAdapterRuntimeTypeWrapper.java index cd32375..ce0383b 100644 --- a/commons-serialize-databind/src/main/java/io/gitlab/jfronny/commons/serialize/databind/impl/adapter/TypeAdapterRuntimeTypeWrapper.java +++ b/commons-serialize-databind/src/main/java/io/gitlab/jfronny/commons/serialize/databind/impl/adapter/TypeAdapterRuntimeTypeWrapper.java @@ -1,11 +1,14 @@ package io.gitlab.jfronny.commons.serialize.databind.impl.adapter; +import io.gitlab.jfronny.commons.serialize.MalformedDataException; import io.gitlab.jfronny.commons.serialize.databind.ObjectMapper; import io.gitlab.jfronny.commons.serialize.databind.api.TypeAdapter; import io.gitlab.jfronny.commons.serialize.SerializeReader; import io.gitlab.jfronny.commons.serialize.SerializeWriter; +import io.gitlab.jfronny.commons.serialize.databind.api.TypeToken; import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; public class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { private final ObjectMapper context; @@ -19,12 +22,21 @@ public class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { } @Override - public > void serialize(T value, Writer writer) throws TEx { - + public > void serialize(T value, Writer writer) throws TEx, MalformedDataException { + TypeAdapter chosen = delegate; + Type runtimeType = type; + if (value != null && (runtimeType instanceof Class || runtimeType instanceof TypeVariable)) { + runtimeType = value.getClass(); + } + if (runtimeType != type) { + TypeAdapter runtimeTypeAdapter = (TypeAdapter) context.getAdapter(TypeToken.get(runtimeType)); + if (runtimeTypeAdapter != null) chosen = runtimeTypeAdapter; + } + chosen.serialize(value, writer); } @Override - public > T deserialize(Reader reader) throws TEx { - return null; + public > T deserialize(Reader reader) throws TEx, MalformedDataException { + return delegate.deserialize(reader); } } diff --git a/commons-serialize-databind/src/test/java/io/gitlab/jfronny/commons/serialize/databind/test/MapAdapterTest.java b/commons-serialize-databind/src/test/java/io/gitlab/jfronny/commons/serialize/databind/test/MapAdapterTest.java new file mode 100644 index 0000000..a144085 --- /dev/null +++ b/commons-serialize-databind/src/test/java/io/gitlab/jfronny/commons/serialize/databind/test/MapAdapterTest.java @@ -0,0 +1,38 @@ +package io.gitlab.jfronny.commons.serialize.databind.test; + +import io.gitlab.jfronny.commons.serialize.MalformedDataException; +import io.gitlab.jfronny.commons.serialize.databind.ObjectMapper; +import io.gitlab.jfronny.commons.serialize.databind.api.TypeAdapter; +import io.gitlab.jfronny.commons.serialize.databind.api.TypeToken; +import io.gitlab.jfronny.commons.serialize.emulated.DataElement; +import io.gitlab.jfronny.commons.serialize.emulated.EmulatedReader; +import io.gitlab.jfronny.commons.serialize.emulated.EmulatedWriter; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MapAdapterTest { + @Test + public void simpleMapTEst() throws MalformedDataException { + DataElement.Object de = new DataElement.Object(); + de.members().put("key", new DataElement.Primitive.String("value")); + de.members().put("key2", new DataElement.Primitive.String("value2")); + de.members().put("key3", new DataElement.Primitive.String("value3")); + ObjectMapper om = new ObjectMapper(); + TypeAdapter> adapter = om.getAdapter(new TypeToken<>() {}); + Map map; + try (EmulatedReader er = new EmulatedReader(de)) { + map = adapter.deserialize(er); + } + assertEquals(3, map.size()); + assertEquals("value", map.get("key")); + assertEquals("value2", map.get("key2")); + assertEquals("value3", map.get("key3")); + try (EmulatedWriter ew = new EmulatedWriter()) { + adapter.serialize(map, ew); + assertEquals(de, ew.get()); + } + } +}