From f1f8b666ec665b41446ddb3d68de115d8ce91d2f Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Wed, 3 Aug 2011 02:51:59 +0000 Subject: [PATCH] Implemented support for FieldNamingPolicy with MiniGson. --- gson/src/main/java/com/google/gson/Gson.java | 7 +++- .../gson/GsonToMiniGsonTypeAdapter.java | 3 -- .../internal/bind/ReflectiveTypeAdapter.java | 32 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 13437371..2ce58ea7 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -165,7 +165,8 @@ public final class Gson { } Gson(ExclusionStrategy deserializationExclusionStrategy, - ExclusionStrategy serializationExclusionStrategy, FieldNamingStrategy2 fieldNamingPolicy, + ExclusionStrategy serializationExclusionStrategy, + final FieldNamingStrategy2 fieldNamingPolicy, final MappedObjectConstructor objectConstructor, boolean serializeNulls, final ParameterizedTypeHandlerMap> serializers, final ParameterizedTypeHandlerMap> deserializers, @@ -191,6 +192,10 @@ public final class Gson { TypeAdapter.Factory reflectiveTypeAdapterFactory = new ReflectiveTypeAdapter.FactoryImpl() { @Override + public String getFieldName(Class declaringClazz, Field f, Type declaredType) { + return fieldNamingPolicy.translateName(new FieldAttributes(declaringClazz, f, declaredType)); + } + @Override public boolean serializeField(Class declaringClazz, Field f, Type declaredType) { return !Gson.this.serializationExclusionStrategy.shouldSkipField( new FieldAttributes(declaringClazz, f, declaredType)); diff --git a/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapter.java b/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapter.java index b7ff2054..bb55578a 100644 --- a/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapter.java @@ -15,10 +15,7 @@ */ package com.google.gson; -import java.beans.DesignMode; import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; import java.lang.reflect.Type; import com.google.gson.internal.Streams; diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java index f0164415..fe01a529 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java @@ -16,12 +16,6 @@ package com.google.gson.internal.bind; -import com.google.gson.internal.$Gson$Types; -import com.google.gson.internal.UnsafeAllocator; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; @@ -30,6 +24,15 @@ import java.lang.reflect.Type; import java.util.LinkedHashMap; import java.util.Map; +import com.google.gson.FieldAttributes; +import com.google.gson.FieldAttributesTest; +import com.google.gson.internal.$Gson$Types; +import com.google.gson.internal.UnsafeAllocator; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; + /** * Adapts the fields of an object to the properties of a JSON object. */ @@ -110,9 +113,10 @@ public final class ReflectiveTypeAdapter extends TypeAdapter { } static BoundField createBoundField( - final MiniGson context, final Field field, final TypeToken fieldType, boolean serialize, boolean deserialize) { + final MiniGson context, final Field field, final String name, + final TypeToken fieldType, boolean serialize, boolean deserialize) { // special casing primitives here saves ~5% on Android... - return new BoundField(field.getName(), serialize, deserialize) { + return new BoundField(name, serialize, deserialize) { final TypeAdapter typeAdapter = context.getAdapter(fieldType); @SuppressWarnings("unchecked") // the type adapter and field type always agree @Override void write(JsonWriter writer, Object value) @@ -129,12 +133,17 @@ public final class ReflectiveTypeAdapter extends TypeAdapter { } public static class FactoryImpl implements Factory { - public boolean serializeField(Class declaringClazz, Field f, Type declaringType) { + public boolean serializeField(Class declaringClazz, Field f, Type declaredType) { return true; } - public boolean deserializeField(Class declaringClazz, Field f, Type declaringType) { + public boolean deserializeField(Class declaringClazz, Field f, Type declaredType) { return true; } + + public String getFieldName(Class declaringClazz, Field f, Type declaredType) { + return f.getName(); + } + public TypeAdapter create(MiniGson context, TypeToken type) { Class raw = type.getRawType(); @@ -163,7 +172,8 @@ public final class ReflectiveTypeAdapter extends TypeAdapter { boolean deserialize = deserializeField(raw, field, declaredType); if (serialize || deserialize) { Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType()); - BoundField boundField = createBoundField(context, field, TypeToken.get(fieldType), serialize, deserialize); + BoundField boundField = createBoundField(context, field, getFieldName(raw, field, declaredType), + TypeToken.get(fieldType), serialize, deserialize); result.put(boundField.name, boundField); } }