fix(serialize-databind): actually implement TypeAdapterRuntimeTypeWrapper
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
451ac11851
commit
d29f8876ef
@ -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<T> extends TypeAdapter<T> {
|
||||
private final ObjectMapper context;
|
||||
@ -19,12 +22,21 @@ public class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serialize(T value, Writer writer) throws TEx {
|
||||
|
||||
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serialize(T value, Writer writer) throws TEx, MalformedDataException {
|
||||
TypeAdapter<T> chosen = delegate;
|
||||
Type runtimeType = type;
|
||||
if (value != null && (runtimeType instanceof Class<?> || runtimeType instanceof TypeVariable<?>)) {
|
||||
runtimeType = value.getClass();
|
||||
}
|
||||
if (runtimeType != type) {
|
||||
TypeAdapter<T> runtimeTypeAdapter = (TypeAdapter<T>) context.getAdapter(TypeToken.get(runtimeType));
|
||||
if (runtimeTypeAdapter != null) chosen = runtimeTypeAdapter;
|
||||
}
|
||||
chosen.serialize(value, writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> T deserialize(Reader reader) throws TEx {
|
||||
return null;
|
||||
public <TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> T deserialize(Reader reader) throws TEx, MalformedDataException {
|
||||
return delegate.deserialize(reader);
|
||||
}
|
||||
}
|
||||
|
@ -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<Map<String, String>> adapter = om.getAdapter(new TypeToken<>() {});
|
||||
Map<String, String> 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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user