diff --git a/src/main/java/org/eclipse/yasson/internal/ComponentMatcher.java b/src/main/java/org/eclipse/yasson/internal/ComponentMatcher.java index f8bc3b6fc..3457ef27a 100644 --- a/src/main/java/org/eclipse/yasson/internal/ComponentMatcher.java +++ b/src/main/java/org/eclipse/yasson/internal/ComponentMatcher.java @@ -334,7 +334,7 @@ DeserializerBinding introspectDeserializerBinding(Class serializerClass, JsonbSerializer instance) { final ParameterizedType serializerRuntimeType = ReflectionUtils .findParameterizedType(serializerClass, JsonbSerializer.class); - Type serBindingType = resolveTypeArg(serializerRuntimeType.getActualTypeArguments()[0], serializerClass.getClass()); + Type serBindingType = resolveTypeArg(serializerRuntimeType.getActualTypeArguments()[0], serializerClass); final ComponentBindings componentBindings = getBindingInfo(serBindingType); if (componentBindings.getSerializer() != null && componentBindings.getSerializer().getClass().equals(serializerClass)) { return componentBindings.getSerializer(); diff --git a/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java b/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java index c0e00750e..58083427e 100644 --- a/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java +++ b/src/test/java/org/eclipse/yasson/serializers/SerializersTest.java @@ -51,7 +51,9 @@ import org.eclipse.yasson.serializers.model.CrateJsonObjectDeserializer; import org.eclipse.yasson.serializers.model.CrateSerializer; import org.eclipse.yasson.serializers.model.CrateSerializerWithConversion; +import org.eclipse.yasson.serializers.model.ExplicitJsonbSerializer; import org.eclipse.yasson.serializers.model.GenericPropertyPojo; +import org.eclipse.yasson.serializers.model.ImplicitJsonbSerializer; import org.eclipse.yasson.serializers.model.NumberDeserializer; import org.eclipse.yasson.serializers.model.NumberSerializer; import org.eclipse.yasson.serializers.model.RecursiveDeserializer; @@ -710,5 +712,17 @@ public Type getOwnerType() { assertTrue(genericBeanDeserializer.called); } + + @Test + public void testImplicitJsonbSerializers() { + Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withSerializers(new ExplicitJsonbSerializer())); + String expected = "{\"value\":\"123\"}"; + Box box = new Box(); + box.boxStr = "Box"; + assertEquals(expected, jsonb.toJson(box)); + + jsonb = JsonbBuilder.create(new JsonbConfig().withSerializers(new ImplicitJsonbSerializer())); + assertEquals(expected, jsonb.toJson(box)); + } } diff --git a/src/test/java/org/eclipse/yasson/serializers/model/AbstractJsonbSerializer.java b/src/test/java/org/eclipse/yasson/serializers/model/AbstractJsonbSerializer.java new file mode 100644 index 000000000..1224bfae1 --- /dev/null +++ b/src/test/java/org/eclipse/yasson/serializers/model/AbstractJsonbSerializer.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.serializers.model; + +import jakarta.json.Json; +import jakarta.json.JsonStructure; +import jakarta.json.bind.serializer.JsonbSerializer; +import jakarta.json.bind.serializer.SerializationContext; +import jakarta.json.stream.JsonGenerator; + +/** + * Abstract serializer. + */ +public class AbstractJsonbSerializer implements JsonbSerializer { + @Override + public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) { + JsonStructure json = Json.createObjectBuilder().add("value", "123").build(); + generator.write(json); + } +} \ No newline at end of file diff --git a/src/test/java/org/eclipse/yasson/serializers/model/ExplicitJsonbSerializer.java b/src/test/java/org/eclipse/yasson/serializers/model/ExplicitJsonbSerializer.java new file mode 100644 index 000000000..296dfd73b --- /dev/null +++ b/src/test/java/org/eclipse/yasson/serializers/model/ExplicitJsonbSerializer.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.serializers.model; + +import jakarta.json.bind.serializer.JsonbSerializer; + +/** + * Explicit serializer. + */ +public class ExplicitJsonbSerializer extends AbstractJsonbSerializer implements JsonbSerializer { +} diff --git a/src/test/java/org/eclipse/yasson/serializers/model/ImplicitJsonbSerializer.java b/src/test/java/org/eclipse/yasson/serializers/model/ImplicitJsonbSerializer.java new file mode 100644 index 000000000..ffbe40bd4 --- /dev/null +++ b/src/test/java/org/eclipse/yasson/serializers/model/ImplicitJsonbSerializer.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.serializers.model; + +/** + * Implicit serializer. + */ +public class ImplicitJsonbSerializer extends AbstractJsonbSerializer { +}