Skip to content

Commit

Permalink
Allow adapters/serializers to be used on known root types
Browse files Browse the repository at this point in the history
  • Loading branch information
aguibert committed Mar 28, 2020
1 parent 6e6fcca commit 5926926
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Queue;
import java.util.Set;

Expand Down Expand Up @@ -70,6 +73,7 @@
import org.eclipse.yasson.internal.model.customization.ClassCustomizationBuilder;
import org.eclipse.yasson.internal.properties.MessageKeys;
import org.eclipse.yasson.internal.properties.Messages;
import org.eclipse.yasson.internal.serializer.DefaultSerializers;
import org.eclipse.yasson.internal.serializer.JsonbDateFormatter;
import org.eclipse.yasson.internal.serializer.JsonbNumberFormatter;

Expand Down Expand Up @@ -351,6 +355,13 @@ public boolean isClassNillable(JsonbAnnotatedElement<Class<?>> clazzElement) {
if (jsonbNillable != null) {
return jsonbNillable.value();
}
Class<?> clazz = clazzElement.getElement();
if (clazz == Optional.class
|| clazz == OptionalDouble.class
|| clazz == OptionalInt.class
|| clazz == OptionalLong.class) {
return true;
}
return jsonbContext.getConfigProperties().getConfigNullable();
}

Expand Down Expand Up @@ -748,6 +759,10 @@ public Class<?> getImplementationClass(Property property) {
*/
public JsonbAnnotatedElement<Class<?>> collectAnnotations(Class<?> clazz) {
JsonbAnnotatedElement<Class<?>> classElement = new JsonbAnnotatedElement<>(clazz);

if (DefaultSerializers.getInstance().isKnownType(clazz)) {
return classElement;
}

for (Class<?> ifc : collectInterfaces(clazz)) {
addIfNotPresent(classElement, ifc.getDeclaredAnnotations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.yasson.internal.model.JsonbAnnotatedElement;
import org.eclipse.yasson.internal.model.customization.ClassCustomization;
import org.eclipse.yasson.internal.serializer.ContainerSerializerProvider;
import org.eclipse.yasson.internal.serializer.DefaultSerializers;

/**
* JSONB mappingContext. Created once per {@link jakarta.json.bind.Jsonb} instance. Represents a global scope.
Expand Down Expand Up @@ -92,7 +93,9 @@ private static Function<Class<?>, ClassModel> createParseClassModelFunction(Clas
customization,
parentClassModel,
jsonbContext.getConfigProperties().getPropertyNamingStrategy());
classParser.parseProperties(newClassModel, clsElement);
if (!DefaultSerializers.getInstance().isKnownType(aClass)) {
classParser.parseProperties(newClassModel, clsElement);
}
return newClassModel;
};
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/eclipse/yasson/internal/Marshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.yasson.internal.properties.Messages;
import org.eclipse.yasson.internal.serializer.AbstractValueTypeSerializer;
import org.eclipse.yasson.internal.serializer.ContainerSerializerProvider;
import org.eclipse.yasson.internal.serializer.DefaultSerializers;
import org.eclipse.yasson.internal.serializer.SerializerBuilder;

/**
Expand Down Expand Up @@ -159,10 +158,8 @@ JsonbSerializer<?> getRootSerializer(Class<?> rootClazz) {
.withObjectClass(rootClazz)
.withType(runtimeType);

if (!DefaultSerializers.getInstance().isKnownType(rootClazz)) {
ClassModel classModel = getMappingContext().getOrCreateClassModel(rootClazz);
serializerBuilder.withCustomization(classModel.getClassCustomization());
}
ClassModel classModel = getMappingContext().getOrCreateClassModel(rootClazz);
serializerBuilder.withCustomization(classModel.getClassCustomization());
return serializerBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz, boolean r
declaredConstructor.setAccessible(true);
}
return declaredConstructor;
} catch (NoSuchMethodException e) {
} catch (NoSuchMethodException | RuntimeException e) {
if (required) {
throw new JsonbException(Messages.getMessage(MessageKeys.NO_DEFAULT_CONSTRUCTOR, clazz), e);
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/eclipse/yasson/internal/Unmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.yasson.internal.model.ClassModel;
import org.eclipse.yasson.internal.properties.MessageKeys;
import org.eclipse.yasson.internal.properties.Messages;
import org.eclipse.yasson.internal.serializer.DefaultSerializers;
import org.eclipse.yasson.internal.serializer.DeserializerBuilder;

/**
Expand Down Expand Up @@ -58,11 +57,8 @@ private <T> T deserializeItem(Type type, JsonParser parser) {
DeserializerBuilder deserializerBuilder = new DeserializerBuilder(getJsonbContext())
.withType(type).withJsonValueType(getRootEvent(parser));
Class<?> rawType = ReflectionUtils.getRawType(type);
if (!DefaultSerializers.getInstance().isKnownType(rawType)) {
ClassModel classModel = getMappingContext().getOrCreateClassModel(rawType);
deserializerBuilder.withCustomization(classModel.getClassCustomization());
}

ClassModel classModel = getMappingContext().getOrCreateClassModel(rawType);
deserializerBuilder.withCustomization(classModel.getClassCustomization());
return (T) deserializerBuilder.build().deserialize(parser, this, type);
} catch (JsonbException e) {
LOGGER.severe(e.getMessage());
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/eclipse/yasson/adapters/AdaptersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.eclipse.yasson.adapters.model.*;
import org.eclipse.yasson.defaultmapping.generics.model.ScalarValueWrapper;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
Expand Down Expand Up @@ -554,4 +557,30 @@ public void testDifferentAdapters() {
afterJson);
assertEquals(1, throwableAdapter.callCount);
}

public static class StringAdapter implements JsonbAdapter<String, String> {
@Override
public String adaptToJson(String obj) throws Exception {
return obj.toUpperCase();
}

@Override
public String adaptFromJson(String obj) throws Exception {
return obj.toLowerCase();
}
}

/**
* Test for: https://github.com/eclipse-ee4j/yasson/issues/346
*/
@Test
public void testAdaptedRootType() {
Jsonb jsonb = JsonbBuilder.newBuilder()
.withConfig(new JsonbConfig().withAdapters(new StringAdapter()))
.build();

String original = "hello world!";
assertEquals("\"HELLO WORLD!\"", jsonb.toJson(original));
assertEquals(original, jsonb.fromJson("\"HELLO WORLD!\"", String.class));
}
}

0 comments on commit 5926926

Please sign in to comment.