Skip to content

Commit

Permalink
Closes owlike#17
Browse files Browse the repository at this point in the history
  • Loading branch information
EugenCepoi committed Sep 22, 2014
1 parent b2b3176 commit e703f6d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
30 changes: 29 additions & 1 deletion genson/src/main/java/com/owlike/genson/GensonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class GensonBuilder {
private Converter<Object> nullConverter;
private DateFormat dateFormat = SimpleDateFormat.getDateInstance();
private boolean useDateAsTimestamp;
private boolean classMetadataWithStaticType = true;

// for the moment we don't allow to override
private BeanViewDescriptorProvider beanViewDescriptorProvider;
Expand Down Expand Up @@ -605,11 +606,38 @@ public GensonBuilder useByteAsInt(boolean enable) {
return this;
}

/**
* If set to true, Genson will throw a JsonBindingException when it encounters a property in the incoming json that does not match
* a property in the class.
* False by default.
* @param enable
* @return
*/
public GensonBuilder failOnMissingProperty(boolean enable) {
this.failOnMissingProperty = enable;
return this;
}

/**
* If set to false, during serialization class metadata will not be serialized for types that are the same as the static type.
* Ex:
*
* <pre>
* class Person {
* public Address address;
* }
* </pre>
*
* Here if the concrete instance of address is Address then this type will not be serialized as metadata, but if they differ then
* it is serialized. By default this option is true, all types are serialized.
* @param enable
* @return
*/
public GensonBuilder useClassMetadataWithStaticType(boolean enable) {
this.classMetadataWithStaticType = enable;
return this;
}

/**
* Creates an instance of Genson. You may use this method as many times you want. It wont
* change the state of the builder, in sense that the returned instance will have always the
Expand Down Expand Up @@ -720,7 +748,7 @@ protected Factory<Converter<?>> createConverterFactory() {
.withNext(new RuntimeTypeConverter.RuntimeTypeConverterFactory());

chainTail = chainTail
.withNext(new ClassMetadataConverter.ClassMetadataConverterFactory());
.withNext(new ClassMetadataConverter.ClassMetadataConverterFactory(classMetadataWithStaticType));

if (withBeanViewConverter) chainTail = chainTail
.withNext(new BeanViewConverter.BeanViewConverterFactory(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.owlike.genson.convert;

import java.io.IOException;
import java.lang.reflect.Type;

import com.owlike.genson.*;
Expand Down Expand Up @@ -40,6 +39,12 @@
*/
public class ClassMetadataConverter<T> extends Wrapper<Converter<T>> implements Converter<T> {
public static class ClassMetadataConverterFactory extends ChainedFactory {
private final boolean classMetadataWithStaticType;

public ClassMetadataConverterFactory(boolean classMetadataWithStaticType) {
this.classMetadataWithStaticType = classMetadataWithStaticType;
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
protected Converter<?> create(Type type, Genson genson, Converter<?> nextConverter) {
Expand All @@ -52,21 +57,24 @@ protected Converter<?> create(Type type, Genson genson, Converter<?> nextConvert
if (genson.isWithClassMetadata()
&& !Wrapper.toAnnotatedElement(nextConverter).isAnnotationPresent(
HandleClassMetadata.class))
return new ClassMetadataConverter(rawClass, nextConverter);
return new ClassMetadataConverter(rawClass, nextConverter, classMetadataWithStaticType);
else
return nextConverter;
}
}

private final boolean classMetadataWithStaticType;
private final Class<T> tClass;

public ClassMetadataConverter(Class<T> tClass, Converter<T> delegate) {
public ClassMetadataConverter(Class<T> tClass, Converter<T> delegate, boolean classMetadataWithStaticType) {
super(delegate);
this.tClass = tClass;
this.classMetadataWithStaticType = classMetadataWithStaticType;
}

public void serialize(T obj, ObjectWriter writer, Context ctx) throws Exception {
if (obj != null) {
if (obj != null &&
(classMetadataWithStaticType || (!classMetadataWithStaticType && !tClass.equals(obj.getClass())))) {
writer.beginNextObjectMetadata()
.writeMetadata("class", ctx.genson.aliasFor(obj.getClass()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public void testDeserializeToUnknownType() {
assertTrue(bean.value instanceof Bean);
}

@Test public void testClassMetadataShouldNotBeSerializedForStaticTypes() {
Genson genson = new GensonBuilder().useClassMetadata(true).useClassMetadataWithStaticType(false).create();

Bean bean = new Bean();

assertEquals("{\"value\":null}", genson.serialize(bean));
}

static class Bean {
Object value;
}
Expand Down

0 comments on commit e703f6d

Please sign in to comment.