Implemented support for FieldNamingPolicy with MiniGson.
This commit is contained in:
parent
d70fb90ef7
commit
f1f8b666ec
|
@ -165,7 +165,8 @@ public final class Gson {
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson(ExclusionStrategy deserializationExclusionStrategy,
|
Gson(ExclusionStrategy deserializationExclusionStrategy,
|
||||||
ExclusionStrategy serializationExclusionStrategy, FieldNamingStrategy2 fieldNamingPolicy,
|
ExclusionStrategy serializationExclusionStrategy,
|
||||||
|
final FieldNamingStrategy2 fieldNamingPolicy,
|
||||||
final MappedObjectConstructor objectConstructor, boolean serializeNulls,
|
final MappedObjectConstructor objectConstructor, boolean serializeNulls,
|
||||||
final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
||||||
final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
||||||
|
@ -191,6 +192,10 @@ public final class Gson {
|
||||||
TypeAdapter.Factory reflectiveTypeAdapterFactory =
|
TypeAdapter.Factory reflectiveTypeAdapterFactory =
|
||||||
new ReflectiveTypeAdapter.FactoryImpl() {
|
new ReflectiveTypeAdapter.FactoryImpl() {
|
||||||
@Override
|
@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) {
|
public boolean serializeField(Class<?> declaringClazz, Field f, Type declaredType) {
|
||||||
return !Gson.this.serializationExclusionStrategy.shouldSkipField(
|
return !Gson.this.serializationExclusionStrategy.shouldSkipField(
|
||||||
new FieldAttributes(declaringClazz, f, declaredType));
|
new FieldAttributes(declaringClazz, f, declaredType));
|
||||||
|
|
|
@ -15,10 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
import java.beans.DesignMode;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringReader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import com.google.gson.internal.Streams;
|
import com.google.gson.internal.Streams;
|
||||||
|
|
|
@ -16,12 +16,6 @@
|
||||||
|
|
||||||
package com.google.gson.internal.bind;
|
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.io.IOException;
|
||||||
import java.lang.reflect.AccessibleObject;
|
import java.lang.reflect.AccessibleObject;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
@ -30,6 +24,15 @@ import java.lang.reflect.Type;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
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.
|
* Adapts the fields of an object to the properties of a JSON object.
|
||||||
*/
|
*/
|
||||||
|
@ -110,9 +113,10 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static BoundField createBoundField(
|
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...
|
// 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);
|
final TypeAdapter<?> typeAdapter = context.getAdapter(fieldType);
|
||||||
@SuppressWarnings("unchecked") // the type adapter and field type always agree
|
@SuppressWarnings("unchecked") // the type adapter and field type always agree
|
||||||
@Override void write(JsonWriter writer, Object value)
|
@Override void write(JsonWriter writer, Object value)
|
||||||
|
@ -129,12 +133,17 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class FactoryImpl implements Factory {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean deserializeField(Class<?> declaringClazz, Field f, Type declaringType) {
|
public boolean deserializeField(Class<?> declaringClazz, Field f, Type declaredType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFieldName(Class<?> declaringClazz, Field f, Type declaredType) {
|
||||||
|
return f.getName();
|
||||||
|
}
|
||||||
|
|
||||||
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type) {
|
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type) {
|
||||||
Class<? super T> raw = type.getRawType();
|
Class<? super T> raw = type.getRawType();
|
||||||
|
|
||||||
|
@ -163,7 +172,8 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||||
boolean deserialize = deserializeField(raw, field, declaredType);
|
boolean deserialize = deserializeField(raw, field, declaredType);
|
||||||
if (serialize || deserialize) {
|
if (serialize || deserialize) {
|
||||||
Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
|
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);
|
result.put(boundField.name, boundField);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user