org.jclouds.json.internal
Class DeserializationConstructorAndReflectiveTypeAdapterFactory
java.lang.Object
org.jclouds.json.internal.DeserializationConstructorAndReflectiveTypeAdapterFactory
- All Implemented Interfaces:
- com.google.gson.TypeAdapterFactory
public final class DeserializationConstructorAndReflectiveTypeAdapterFactory
- extends Object
- implements com.google.gson.TypeAdapterFactory
Creates type adapters for types handled in the following ways:
- Deserialization
If there's an annotation designating a parameterized constructor, invoke that for fields correlating to named
parameter annotations. Otherwise, use ConstructorConstructor, and set fields via reflection.
Notes: primitive constructor params are set to the Java defaults (0 or false) if not present; and the empty object
({}) is treated as a null if the constructor for the object throws an NPE.
- Serialization
Serialize based on reflective access to fields, delegating to ReflectiveTypeAdaptor.
Example: Using javax inject to select a constructor and corresponding named parameters
import NamingStrategies.*;
serializationStrategy = new AnnotationOrNameFieldNamingStrategy(
new ExtractSerializedName(), new ExtractNamed());
deserializationStrategy = new AnnotationConstructorNamingStrategy(
ImmutableSet.of(javax.inject.Inject.class),
ImmutableSet.of(new ExtractNamed()));
factory = new DeserializationConstructorAndReflectiveTypeAdapterFactory(new ConstructorConstructor(),
serializationStrategy, Excluder.DEFAULT, deserializationStrategy);
gson = new GsonBuilder(serializationStrategy).registerTypeAdapterFactory(factory).create();
The above would work fine on the following class, which has no gson-specific annotations:
private static class ImmutableAndVerifiedInCtor {
final int foo;
@Named("_bar")
final int bar;
@Inject
ImmutableAndVerifiedInCtor(@Named("foo") int foo, @Named("_bar") int bar) {
if (foo < 0)
throw new IllegalArgumentException("negative!");
this.foo = foo;
this.bar = bar;
}
}
- Author:
- Adrian Cole, Adam Lowe
|
Method Summary |
<T> com.google.gson.TypeAdapter<T> |
|
create(com.google.gson.Gson gson,
com.google.gson.reflect.TypeToken<T> type)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DeserializationConstructorAndReflectiveTypeAdapterFactory
public DeserializationConstructorAndReflectiveTypeAdapterFactory(com.google.gson.internal.ConstructorConstructor constructorConstructor,
com.google.gson.FieldNamingStrategy serializationFieldNamingPolicy,
com.google.gson.internal.Excluder excluder,
NamingStrategies.AnnotationConstructorNamingStrategy deserializationFieldNamingPolicy)
- See Also:
ReflectiveTypeAdapterFactory
create
public <T> com.google.gson.TypeAdapter<T> create(com.google.gson.Gson gson,
com.google.gson.reflect.TypeToken<T> type)
- Specified by:
create in interface com.google.gson.TypeAdapterFactory
Copyright © 2009-2013 jclouds. All Rights Reserved.