mirror of https://github.com/apache/jclouds.git
added ability to set field exclusion policies for json serialization
This commit is contained in:
parent
0a31ef2c96
commit
2aea1355a1
|
@ -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<Date> {
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -82,6 +85,26 @@ public class JsonTest {
|
|||
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 {
|
||||
FOO, BAR;
|
||||
|
|
Loading…
Reference in New Issue