Skip to content

Commit

Permalink
Implicitly defined serializers fixed (#510)
Browse files Browse the repository at this point in the history
Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed Aug 12, 2021
1 parent 1a6c6ee commit 8bb4dfe
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ DeserializerBinding introspectDeserializerBinding(Class<? extends JsonbDeseriali
SerializerBinding introspectSerializerBinding(Class<? extends JsonbSerializer> 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();
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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<T> implements JsonbSerializer<T> {
@Override
public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
JsonStructure json = Json.createObjectBuilder().add("value", "123").build();
generator.write(json);
}
}
Original file line number Diff line number Diff line change
@@ -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<Box> implements JsonbSerializer<Box> {
}
Original file line number Diff line number Diff line change
@@ -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<Box> {
}

0 comments on commit 8bb4dfe

Please sign in to comment.