From 2aea1355a1c074785c47fcefcd461c0d16737fd4 Mon Sep 17 00:00:00 2001 From: adriancole Date: Tue, 22 Jan 2013 12:26:54 -0800 Subject: [PATCH] added ability to set field exclusion policies for json serialization --- .../org/jclouds/json/config/GsonModule.java | 22 ++++++++++++++++-- .../test/java/org/jclouds/json/JsonTest.java | 23 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/json/config/GsonModule.java b/core/src/main/java/org/jclouds/json/config/GsonModule.java index 608a0e0a49..c2f09cea41 100644 --- a/core/src/main/java/org/jclouds/json/config/GsonModule.java +++ b/core/src/main/java/org/jclouds/json/config/GsonModule.java @@ -59,6 +59,8 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.primitives.Bytes; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; import com.google.gson.FieldNamingStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -88,12 +90,14 @@ public class GsonModule extends AbstractModule { ByteArrayAdapter byteArrayAdapter, PropertiesAdapter propertiesAdapter, JsonAdapterBindings bindings, OptionalTypeAdapterFactory optional, SetTypeAdapterFactory set, MapTypeAdapterFactory map, MultimapTypeAdapterFactory multimap, IterableTypeAdapterFactory iterable, - CollectionTypeAdapterFactory collection, FluentIterableTypeAdapterFactory fluentIterable) throws Exception { + CollectionTypeAdapterFactory collection, FluentIterableTypeAdapterFactory fluentIterable, + DefaultExclusionStrategy exclusionStrategy) { FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(ImmutableSet.of( new ExtractSerializedName(), new ExtractNamed())); - GsonBuilder builder = new GsonBuilder().setFieldNamingStrategy(serializationPolicy); + GsonBuilder builder = new GsonBuilder().setFieldNamingStrategy(serializationPolicy) + .setExclusionStrategies(exclusionStrategy); // simple (type adapters) builder.registerTypeAdapter(Properties.class, propertiesAdapter.nullSafe()); @@ -130,6 +134,20 @@ public class GsonModule extends AbstractModule { return builder.create(); } + @ImplementedBy(NoExclusions.class) + public static interface DefaultExclusionStrategy extends ExclusionStrategy { + } + + public static class NoExclusions implements DefaultExclusionStrategy { + public boolean shouldSkipField(FieldAttributes f) { + return false; + } + + public boolean shouldSkipClass(Class clazz) { + return false; + } + } + @ImplementedBy(CDateAdapter.class) public abstract static class DateAdapter extends TypeAdapter { diff --git a/core/src/test/java/org/jclouds/json/JsonTest.java b/core/src/test/java/org/jclouds/json/JsonTest.java index 8cee0e4e57..7d76a8508f 100644 --- a/core/src/test/java/org/jclouds/json/JsonTest.java +++ b/core/src/test/java/org/jclouds/json/JsonTest.java @@ -24,11 +24,14 @@ import java.util.Map; import java.util.Properties; import org.jclouds.json.config.GsonModule; +import org.jclouds.json.config.GsonModule.DefaultExclusionStrategy; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; +import com.google.gson.FieldAttributes; +import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.TypeLiteral; @@ -81,6 +84,26 @@ public class JsonTest { assertEquals(obj2, obj); assertEquals(json.toJson(obj2), json.toJson(obj)); } + + static class ExcludeStringValue implements DefaultExclusionStrategy { + public boolean shouldSkipClass(Class clazz) { + return false; + } + + public boolean shouldSkipField(FieldAttributes f) { + return f.getName().equals("stringValue"); + } + } + + public void testExcluder() { + Json excluder = Guice.createInjector(new GsonModule(), new AbstractModule() { + protected void configure() { + bind(DefaultExclusionStrategy.class).to(ExcludeStringValue.class); + } + }).getInstance(Json.class); + ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1); + assertEquals(excluder.toJson(obj), "{\"intValue\":1}"); + } private static class EnumInside { private static enum Test {