Skip to content

Commit

Permalink
Full adoption of JsonGenerator#writeString. Key string is now not pro…
Browse files Browse the repository at this point in the history
…pagated in models and is written directly when encountered.

Signed-off-by: Roman Grigoriadi <roman.grigoriadi@oracle.com>
  • Loading branch information
Roman Grigoriadi committed Nov 3, 2017
1 parent c835156 commit d072154
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 163 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javax.json.version>1.1</javax.json.version>
<javax.json.version>1.2-SNAPSHOT</javax.json.version>
<javax.json.bind.version>1.0</javax.json.bind.version>
<netbeans.hint.jdkPlatform>JDK_9</netbeans.hint.jdkPlatform>
</properties>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/eclipse/yasson/internal/Marshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.yasson.internal.serializer.SerializerBuilder;
import org.eclipse.yasson.internal.serializer.ContainerModel;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;
import org.eclipse.yasson.internal.model.JsonbPropertyInfo;

import javax.json.bind.JsonbException;
Expand Down Expand Up @@ -72,7 +71,7 @@ public Marshaller(JsonbContext jsonbContext) {
*/
public void marshall(Object object, JsonGenerator jsonGenerator) {
try {
final ContainerModel model = new ContainerModel(runtimeType != null ? runtimeType : object.getClass(), null, JsonContext.ROOT, null);
final ContainerModel model = new ContainerModel(runtimeType != null ? runtimeType : object.getClass(), null);
serializeRoot(object, jsonGenerator, model);
} catch (JsonbException e) {
logger.severe(e.getMessage());
Expand All @@ -90,14 +89,15 @@ public void marshall(Object object, JsonGenerator jsonGenerator) {
public <T> void serialize(String key, T object, JsonGenerator generator) {
Objects.requireNonNull(key);
Objects.requireNonNull(object);
final ContainerModel model = new ContainerModel(object.getClass(), null, JsonContext.JSON_OBJECT, key);
final ContainerModel model = new ContainerModel(object.getClass(), null);
generator.writeKey(key);
serializeRoot(object, generator, model);
}

@Override
public <T> void serialize(T object, JsonGenerator generator) {
Objects.requireNonNull(object);
final ContainerModel model = new ContainerModel(object.getClass(), null, JsonContext.JSON_ARRAY, null);
final ContainerModel model = new ContainerModel(object.getClass(), null);
serializeRoot(object, generator, model);
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/org/eclipse/yasson/internal/model/CreatorModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,4 @@ public Type getType() {
return type;
}

@Override
public String getWriteName() {
return name;
}

@Override
public JsonContext getContext() {
return JsonContext.JSON_OBJECT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,4 @@ public interface JsonBindingModel {
*/
Type getType();

/**
* Returns a name of json key that will be written by marshaller.
*
* @return name of json key
*/
String getWriteName();

/**
* Current context of json generator.
*
* @return context
*/
JsonContext getContext();

}
20 changes: 0 additions & 20 deletions src/main/java/org/eclipse/yasson/internal/model/JsonContext.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,6 @@ public int hashCode() {
return Objects.hash(propertyName);
}

@Override
public JsonContext getContext() {
return JsonContext.JSON_OBJECT;
}

/**
* Gets a name of JSON document property to read this property from.
*
Expand All @@ -362,7 +357,6 @@ public String getReadName() {
return readName;
}

@Override
public String getWriteName() {
return writeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.yasson.internal.ReflectionUtils;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.stream.JsonGenerator;
import java.lang.reflect.GenericArrayType;
Expand All @@ -37,8 +36,7 @@ public abstract class AbstractArraySerializer<T> extends AbstractContainerSerial
protected AbstractArraySerializer(SerializerBuilder builder) {
super(builder);
arrayValType = resolveArrayType();
containerModel = new ContainerModel(arrayValType, resolveContainerModelCustomization(arrayValType, builder.getJsonbContext()),
JsonContext.JSON_ARRAY);
containerModel = new ContainerModel(arrayValType, resolveContainerModelCustomization(arrayValType, builder.getJsonbContext()));
}

private Type resolveArrayType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.yasson.internal.model.ClassModel;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.bind.serializer.JsonbSerializer;
import javax.json.bind.serializer.SerializationContext;
Expand Down Expand Up @@ -52,11 +51,7 @@ public AbstractContainerSerializer(CurrentItem<?> wrapper, Type runtimeType, Cla

@Override
public final void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
if (getWrapperModel().getContext() == JsonContext.JSON_OBJECT) {
writeStart(getWrapperModel().getWriteName(), generator);
} else {
writeStart(generator);
}
writeStart(generator);
serializeInternal(obj, generator, ctx);
writeEnd(generator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.yasson.internal.JsonbContext;
import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.serializer.SerializationContext;
Expand Down Expand Up @@ -50,11 +49,7 @@ public AbstractDateTimeSerializer(JsonBindingModel model) {
public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
final JsonbContext jsonbContext = ((Marshaller) ctx).getJsonbContext();
final JsonbDateFormatter formatter = getJsonbDateFormatter(jsonbContext);
if (model.getContext() == JsonContext.JSON_OBJECT) {
generator.write(model.getWriteName(), toJson(obj, formatter, jsonbContext));
} else {
generator.write(toJson(obj, formatter, jsonbContext));
}
generator.write(toJson(obj, formatter, jsonbContext));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.bind.serializer.JsonbSerializer;
import javax.json.bind.serializer.SerializationContext;
Expand Down Expand Up @@ -49,11 +48,7 @@ public AbstractValueTypeSerializer(JsonBindingModel model) {
@Override
public void serialize(T obj, JsonGenerator generator, SerializationContext ctx) {
Marshaller marshaller = (Marshaller) ctx;
if (model.getContext() == JsonContext.JSON_OBJECT) {
serialize(obj, generator, model.getWriteName(), marshaller);
} else {
serialize(obj, generator, marshaller);
}
serialize(obj, generator, marshaller);
}

protected abstract void serialize(T obj, JsonGenerator generator, String key, Marshaller marshaller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.yasson.internal.properties.Messages;
import org.eclipse.yasson.internal.model.ClassModel;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;
import org.eclipse.yasson.internal.model.JsonbPropertyInfo;
import org.eclipse.yasson.internal.model.customization.Customization;

Expand Down Expand Up @@ -50,20 +49,6 @@ public AdaptedObjectSerializerModel(JsonBindingModel wrapperSerializerModel, Typ
this.adaptedType = adaptedType;
}

@Override
public String getWriteName() {
return wrapperSerializerModel.getWriteName();
}

/**
* Array context if root.
*/
@Override
public JsonContext getContext() {
return wrapperSerializerModel != null ?
wrapperSerializerModel.getContext() : JsonContext.JSON_ARRAY;
}

/**
* Get wrapper customization or empty if wrapper not present (root).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.ReflectionUtils;
import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.bind.serializer.JsonbSerializer;
import javax.json.bind.serializer.SerializationContext;
Expand All @@ -43,8 +42,7 @@ public class CollectionSerializer<T extends Collection> extends AbstractContaine
protected CollectionSerializer(SerializerBuilder builder) {
super(builder);
collectionValueType = getValueType();
containerModel = new ContainerModel(collectionValueType, resolveContainerModelCustomization(collectionValueType, builder.getJsonbContext()),
JsonContext.JSON_ARRAY);
containerModel = new ContainerModel(collectionValueType, resolveContainerModelCustomization(collectionValueType, builder.getJsonbContext()));
valueSerializer = resolveValueSerializer(collectionValueType, builder.getJsonbContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.yasson.internal.serializer;

import org.eclipse.yasson.internal.model.JsonBindingModel;
import org.eclipse.yasson.internal.model.JsonContext;
import org.eclipse.yasson.internal.model.customization.Customization;

import java.lang.reflect.Type;
Expand All @@ -29,10 +28,6 @@
*/
public class ContainerModel implements JsonBindingModel {

private final JsonContext context;

private final String writeName;

private final Type valueRuntimeType;

private final Customization customization;
Expand All @@ -42,41 +37,10 @@ public class ContainerModel implements JsonBindingModel {
*
* @param valueRuntimeType collection or map value type
* @param customization customization parsed from value type
* @param context Object context used for serialization
* @param writeName write name used for serialization
*/
public ContainerModel(Type valueRuntimeType, Customization customization, JsonContext context, String writeName) {
this.valueRuntimeType = valueRuntimeType;
this.customization = customization;
this.context = context;
this.writeName = writeName;
}

/**
* Construct model.
*
* @param valueRuntimeType collection or map value type
* @param customization customization parsed from value type
* @param context Object context used for serialization
*/
public ContainerModel(Type valueRuntimeType, Customization customization, JsonContext context) {
this.valueRuntimeType = valueRuntimeType;
this.customization = customization;
this.context = context;
this.writeName = null;
}

/**
* Minimal constructor.
*
* @param valueRuntimeType collection or map value type
* @param customization customization parsed from value type
*/
public ContainerModel(Type valueRuntimeType, Customization customization) {
this.valueRuntimeType = valueRuntimeType;
this.customization = customization;
this.context = null;
this.writeName = null;
}

/**
Expand All @@ -99,24 +63,4 @@ public Type getType() {
return valueRuntimeType;
}

/**
* Returns a name of JSON key that will be written by marshaller.
*
* @return name of JSON key
*/
@Override
public String getWriteName() {
return writeName;
}

/**
* Current context of json generator.
*
* @return context
*/
@Override
public JsonContext getContext() {
return context;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.ReflectionUtils;
import org.eclipse.yasson.internal.model.JsonContext;

import javax.json.bind.serializer.JsonbSerializer;
import javax.json.bind.serializer.SerializationContext;
Expand Down Expand Up @@ -50,9 +49,10 @@ protected void serializeInternal(T obj, JsonGenerator generator, SerializationCo
generator.writeNull(keysString);
return;
}
generator.writeKey(keysString);
final JsonbSerializer<?> serializer = new SerializerBuilder(marshaller.getJsonbContext()).withObjectClass(value.getClass())
.withModel(new ContainerModel(mapValueRuntimeType,
resolveContainerModelCustomization(mapValueRuntimeType, marshaller.getJsonbContext()), JsonContext.JSON_OBJECT, keysString)).build();
resolveContainerModelCustomization(mapValueRuntimeType, marshaller.getJsonbContext()))).build();
serializerCaptor(serializer, value, generator, ctx);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import javax.json.bind.serializer.SerializationContext;
import javax.json.stream.JsonGenerator;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

/**
* Serializes arbitrary object by reading its properties.
Expand Down Expand Up @@ -76,13 +80,15 @@ private void marshallProperty(T object, JsonGenerator generator, SerializationCo

if (propertyModel.isReadable()) {
final Object propertyValue = propertyModel.getValue(object);
if (propertyValue == null) {
if (propertyValue == null || isEmptyOptional(propertyValue)) {
if (propertyModel.getCustomization().isNillable()) {
generator.writeNull(propertyModel.getWriteName());
}
return;
}

generator.writeKey(propertyModel.getWriteName());

final JsonbSerializer<?> propertyCachedSerializer = propertyModel.getPropertySerializer();
if (propertyCachedSerializer != null) {
serializerCaptor(propertyCachedSerializer, propertyValue, generator, ctx);
Expand All @@ -97,4 +103,18 @@ private void marshallProperty(T object, JsonGenerator generator, SerializationCo
serializerCaptor(serializer, propertyValue, generator, ctx);
}
}

private boolean isEmptyOptional(Object object) {
if (object instanceof Optional) {
return !((Optional) object).isPresent();
} else if (object instanceof OptionalInt) {
return !((OptionalInt) object).isPresent();
} else if (object instanceof OptionalLong) {
return !((OptionalLong) object).isPresent();
} else if (object instanceof OptionalDouble) {
return !((OptionalDouble) object).isPresent();
}
return false;
}

}

0 comments on commit d072154

Please sign in to comment.