Example usage for com.google.gson GsonBuilder addDeserializationExclusionStrategy

List of usage examples for com.google.gson GsonBuilder addDeserializationExclusionStrategy

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder addDeserializationExclusionStrategy.

Prototype

public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) 

Source Link

Document

Configures Gson to apply the passed in exclusion strategy during deserialization.

Usage

From source file:co.aurasphere.botmill.fb.internal.util.json.AttachmentDeserializer.java

License:Open Source License

/**
 * Instantiates a new AttachmentDeserializer.
 *//*w w  w  . java2  s. com*/
public AttachmentDeserializer() {
    GsonBuilder builder = new GsonBuilder();
    builder.addDeserializationExclusionStrategy(new SkipDeserializationAnnotationExclusionStrategy());
    delegateGson = builder.create();
}

From source file:co.aurasphere.botmill.kik.util.json.IncomingMessagesDeserializer.java

License:Open Source License

/**
 * Instantiates a new AttachmentDeserializer.
 *//*from   w ww . j a  v  a 2 s.  c om*/
public IncomingMessagesDeserializer() {
    GsonBuilder builder = new GsonBuilder();
    builder.addDeserializationExclusionStrategy(new SkipDeserializationAnnotationExclusionStrategy());
    delegateGson = builder.create();
}

From source file:com.google.api.ads.adwords.keywordoptimizer.api.JsonUtil.java

License:Open Source License

/**
 * Initializes Gson to convert objects to and from JSON. This method customizes a "plain" Gson by
 * adding appropriate exclusions strategies / adapters as needed in this project for a "pretty"
 * output. /*from w  ww .  j a  va 2  s .c o m*/
 */
private static Gson initGson(boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder();

    // Exclude superclasses.
    ExclusionStrategy superclassExclusionStrategy = new SuperclassExclusionStrategy();
    builder.addDeserializationExclusionStrategy(superclassExclusionStrategy);
    builder.addSerializationExclusionStrategy(superclassExclusionStrategy);

    // Exclude underscore fields in client lib objects.
    ExclusionStrategy underscoreExclusionStrategy = new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes field) {
            if (field.getName().startsWith("_")) {
                return true;
            }

            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };
    builder.addDeserializationExclusionStrategy(underscoreExclusionStrategy);
    builder.addSerializationExclusionStrategy(underscoreExclusionStrategy);

    // Render KeywordCollection as an array of KeywordInfos.
    builder.registerTypeAdapter(KeywordCollection.class, new JsonSerializer<KeywordCollection>() {
        @Override
        public JsonElement serialize(KeywordCollection src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonArray out = new JsonArray();
            for (KeywordInfo info : src.getListSortedByScore()) {
                out.add(context.serialize(info));
            }
            return out;
        }
    });
    // Render Money as a primitive.
    builder.registerTypeAdapter(Money.class, new JsonSerializer<Money>() {
        @Override
        public JsonElement serialize(Money src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonElement out = new JsonPrimitive(src.getMicroAmount() / 1000000);
            return out;
        }
    });
    // Render Keyword in a simple way.
    builder.registerTypeAdapter(Keyword.class, new JsonSerializer<Keyword>() {
        @Override
        public JsonElement serialize(Keyword src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject out = new JsonObject();
            out.addProperty("text", src.getText());
            out.addProperty("matchtype", src.getMatchType().toString());
            return out;
        }
    });
    // Render Throwable in a simple way (for all subclasses).
    builder.registerTypeHierarchyAdapter(Throwable.class, new JsonSerializer<Throwable>() {
        @Override
        public JsonElement serialize(Throwable src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject out = new JsonObject();
            out.addProperty("message", src.getMessage());
            out.addProperty("type", src.getClass().getName());

            JsonArray stack = new JsonArray();
            for (StackTraceElement stackTraceElement : src.getStackTrace()) {
                JsonObject stackElem = new JsonObject();
                stackElem.addProperty("file", stackTraceElement.getFileName());
                stackElem.addProperty("line", stackTraceElement.getLineNumber());
                stackElem.addProperty("method", stackTraceElement.getMethodName());
                stackElem.addProperty("class", stackTraceElement.getClassName());
                stack.add(stackElem);
            }
            out.add("stack", stack);

            if (src.getCause() != null) {
                out.add("cause", context.serialize(src.getCause()));
            }
            return out;
        }
    });

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }

    return builder.create();
}

From source file:com.nuevebit.miroculus.mrna.rest.util.gson.MiRNAGsonConverter.java

private GsonBuilder configureBuilder(GsonBuilder builder) {
    builder.addDeserializationExclusionStrategy(exclusionStrat);
    builder.addSerializationExclusionStrategy(exclusionStrat);
    builder.registerTypeAdapter(Author.class, new JsonSerializer<Author>() {

        @Override/*w ww . j a  v  a  2 s  . c om*/
        public JsonElement serialize(Author src, Type typeOfSrc, JsonSerializationContext context) {

            return new JsonPrimitive(src.getName());
        }
    });
    return builder;
}

From source file:ru.caramel.juniperbot.core.utils.GsonUtils.java

License:Open Source License

public static Gson create() {
    GsonBuilder builder = new GsonBuilder();
    builder.addDeserializationExclusionStrategy(new SuperclassExclusionStrategy());
    builder.addSerializationExclusionStrategy(new SuperclassExclusionStrategy());
    return builder.create();
}