Skip to content

Instantly share code, notes, and snippets.

@Shujito
Created November 21, 2014 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shujito/d04f1bba3c65df87dc3f to your computer and use it in GitHub Desktop.
Save Shujito/d04f1bba3c65df87dc3f to your computer and use it in GitHub Desktop.
/**
* Estrategia de exclusion para {@link Gson} para saltarse campos que no sean serializables
* @author shujito
*/
public class ExcludeFieldsWithoutSerializedName implements ExclusionStrategy
{
@Override
public boolean shouldSkipClass(Class<?> clss)
{
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes)
{
// get all annotations, skip these without @SerializedName
Collection<Annotation> annotations = fieldAttributes.getAnnotations();
for (Annotation a : annotations)
{
if (a instanceof SerializedName)
{
return false;
}
}
return true;
}
}
public class SomeApplication
{
/* ... app code ... */
@Override
public void onCreate()
{
super.onCreate();
Log.e(TAG, "Force log to cat");
instance = this;
Configuration configuration = new Configuration.Builder(this)
.setDatabaseName(this.getPackageName())
.setDatabaseVersion(1)
.create();
ActiveAndroid.initialize(configuration);
ExcludeFieldsWithoutSerializedName efwosn = new ExcludeFieldsWithoutSerializedName();
Ion.getDefault(this)
.configure()
.setGson(new GsonBuilder()
.addDeserializationExclusionStrategy(efwosn)
.addSerializationExclusionStrategy(efwosn)
.create());
}
/* ... app code ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment