Skip to content

Commit

Permalink
Add ability to serialize anonymous class and proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
nmorel authored and Nicolas Morel committed Aug 8, 2014
1 parent a443fc1 commit 656da80
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand Down Expand Up @@ -93,7 +94,10 @@ private InternalSerializer<T> getSerializer( JsonWriter writer, T value, JsonSer
}
SubtypeSerializer subtypeSerializer = subtypeClassToSerializer.get( value.getClass() );
if ( null == subtypeSerializer ) {
throw ctx.traceError( value, "Cannot find serializer for class " + value.getClass(), writer );
if (ctx.getLogger().isLoggable( Level.FINE)) {
ctx.getLogger().fine("Cannot find serializer for class " + value.getClass() + ". Fallback to the serializer of " + getSerializedType());
}
return this;
}
return subtypeSerializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ protected void writeClassBody( SourceWriter source, BeanInfo beanInfo, Immutable
source.println();
}

if ( !properties.isEmpty() ) {
// no need to generate properties for non instantiable class
if ( !properties.isEmpty() && beanInfo.getCreatorMethod().isPresent() ) {
generateInitPropertiesMethods( source, beanInfo, properties );
source.println();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public final class PropertyProcessor {
public static ImmutableMap<String, PropertyInfo> findAllProperties( RebindConfiguration configuration, TreeLogger logger,
JacksonTypeOracle typeOracle,
BeanInfo beanInfo ) throws UnableToCompleteException {
if ( null != beanInfo.getType().isInterface() || beanInfo.getType().isAbstract() ) {
// no properties on interface and abstract class
return ImmutableMap.of();
}

// we first parse the bean to retrieve all the properties
ImmutableMap<String, PropertyAccessors> fieldsMap = PropertyParser.findPropertyAccessors( configuration, logger, beanInfo );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.github.nmorel.gwtjackson.client.advanced.GenericsGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.InheritanceGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.PrivateAccessGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.ProxyAndAnonymousClassSerializationGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.WildcardGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.identity.ObjectIdDeserializationGwtTest;
import com.github.nmorel.gwtjackson.client.advanced.identity.ObjectIdGwtTest;
Expand Down Expand Up @@ -272,6 +273,7 @@ public static Test suite() {
suite.addTestSuite( JsonFormatGwtTest.class );

// Advanced use cases
suite.addTestSuite( ProxyAndAnonymousClassSerializationGwtTest.class );
suite.addTestSuite( PrivateAccessGwtTest.class );
suite.addTestSuite( InheritanceGwtTest.class );
suite.addTestSuite( GenericsGwtTest.class );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.github.nmorel.gwtjackson.client.advanced;

import com.github.nmorel.gwtjackson.client.GwtJacksonTestCase;
import com.github.nmorel.gwtjackson.client.ObjectWriter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.ProxyFor;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.RequestFactory;
import com.google.web.bindery.requestfactory.shared.Service;
import com.google.web.bindery.requestfactory.shared.ValueProxy;

/**
*/
public class ProxyAndAnonymousClassSerializationGwtTest extends GwtJacksonTestCase {

// RequestFactory proxy

public static class RfBean {

private int id;

private String name;

public int getId() {
return id;
}

public void setId( int id ) {
this.id = id;
}

public String getName() {
return name;
}

public void setName( String name ) {
this.name = name;
}
}

@ProxyFor( RfBean.class )
public static interface RfBeanProxy extends ValueProxy {

int getId();

void setId( int id );

String getName();

void setName( String name );
}

public static class RfBeanService {

public void save( RfBean bean ) {

}
}

@Service( RfBeanService.class )
public static interface ServiceContext extends RequestContext {

Request<Void> save( RfBeanProxy bean );
}

public static interface TestRequestFactory extends RequestFactory {

ServiceContext serviceContext();
}

public static interface RfBeanProxyWriter extends ObjectWriter<RfBeanProxy> {}

public void testSerializationProxy() {
TestRequestFactory requestFactory = GWT.create( TestRequestFactory.class );
requestFactory.initialize( new SimpleEventBus() );

ServiceContext serviceContext = requestFactory.serviceContext();
RfBeanProxy proxy = serviceContext.create( RfBeanProxy.class );
proxy.setId( 54 );
proxy.setName( "Toto" );

RfBeanProxyWriter writer = GWT.create( RfBeanProxyWriter.class );
String json = writer.write( proxy );
assertEquals( "{\"id\":54,\"name\":\"Toto\"}", json );
}

//####### Anonymous class

public static abstract class AbstractBean {

public abstract int getId();

public abstract String getName();
}

public static interface AbstractBeanWriter extends ObjectWriter<AbstractBean> {}

public void testSerializationAnonymousClass() {
AbstractBeanWriter writer = GWT.create( AbstractBeanWriter.class );
String json = writer.write( new AbstractBean() {
@Override
public int getId() {
return 54;
}

@Override
public String getName() {
return "Toto";
}
} );
assertEquals( "{\"id\":54,\"name\":\"Toto\"}", json );
}

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

<module>
<inherits name="com.github.nmorel.gwtjackson.GwtJacksonSharedTest" />
<inherits name='com.google.web.bindery.requestfactory.RequestFactory' />

<extend-configuration-property name="gwtjackson.configuration.extension"
value="com.github.nmorel.gwtjackson.client.mixins.MixInConfiguration" />
Expand Down

0 comments on commit 656da80

Please sign in to comment.