Move exclusion strategy to its own file

This commit is contained in:
Colin McDonald 2016-03-21 19:59:06 -04:00
parent 4802efcfee
commit f627ba9358
2 changed files with 20 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import com.mongodb.ServerAddress;
import lombok.Getter; import lombok.Getter;
import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.model.*;
import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies; import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies;
import net.frozenorb.apiv3.weirdStuff.FollowAnnotationExclusionStrategy;
import net.frozenorb.apiv3.weirdStuff.ObjectIdTypeAdapter; import net.frozenorb.apiv3.weirdStuff.ObjectIdTypeAdapter;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Datastore;
@ -23,19 +24,7 @@ import static spark.Spark.*;
public final class APIv3 { public final class APIv3 {
@Getter private static Datastore datastore; @Getter private static Datastore datastore;
private final Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).setExclusionStrategies(new ExclusionStrategy() { private final Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).setExclusionStrategies(new FollowAnnotationExclusionStrategy()).create();
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
return fieldAttributes.getAnnotation(ExcludeFromReplies.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> aClass) {
return false;
}
}).create();
APIv3() { APIv3() {
setupDatabase(); setupDatabase();

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.weirdStuff;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public final class FollowAnnotationExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
return fieldAttributes.getAnnotation(ExcludeFromReplies.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> aClass) {
return false;
}
}