List of usage examples for com.google.gson GsonBuilder addSerializationExclusionStrategy
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy)
From source file:ch.devmine.javaparser.utils.GsonFactory.java
License:Open Source License
public static Gson build() { GsonBuilder builder = new GsonBuilder(); builder.addSerializationExclusionStrategy(new ExclusionStrategy() { @Override//from w w w . jav a 2 s. co m public boolean shouldSkipField(FieldAttributes f) { return f.getAnnotation(GsonTransient.class) != null; } @Override public boolean shouldSkipClass(Class<?> c) { return c.getAnnotation(GsonTransient.class) != null; } }); return 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 w w .ja 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.google.gerrit.httpd.restapi.RestApiServlet.java
License:Apache License
private static void enablePartialGetFields(GsonBuilder gb, Multimap<String, String> config) { final Set<String> want = Sets.newHashSet(); for (String p : config.get("fields")) { Iterables.addAll(want, OptionUtil.splitOptionValue(p)); }/* w ww .j a v a 2 s. com*/ if (!want.isEmpty()) { gb.addSerializationExclusionStrategy(new ExclusionStrategy() { private final Map<String, String> names = Maps.newHashMap(); @Override public boolean shouldSkipField(FieldAttributes field) { String name = names.get(field.getName()); if (name == null) { // Names are supplied by Gson in terms of Java source. // Translate and cache the JSON lower_case_style used. try { name = FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES.translateName(// field.getDeclaringClass().getDeclaredField(field.getName())); names.put(field.getName(), name); } catch (SecurityException e) { return true; } catch (NoSuchFieldException e) { return true; } } return !want.contains(name); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); } }
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 va2 s. co m public JsonElement serialize(Author src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.getName()); } }); return builder; }
From source file:com.remediatetheflag.global.messages.MessageGenerator.java
License:Apache License
public static void sendExerciseInfoMessage(AvailableExercise exercise, HttpServletResponse response) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.addSerializationExclusionStrategy(excludedHints).excludeFieldsWithoutExposeAnnotation() .create();//from ww w .ja v a 2s.co m String json = gson.toJson(exercise); send(json, response); }
From source file:com.vmware.xenon.common.serialization.JsonMapper.java
License:Open Source License
public static GsonBuilder createDefaultGsonBuilder(boolean isCompact, boolean isSensitive) { GsonBuilder bldr = new GsonBuilder(); registerCommonGsonTypeAdapters(bldr); if (!isCompact) { bldr.setPrettyPrinting();/* www . jav a2 s . c o m*/ } bldr.disableHtmlEscaping(); if (isSensitive) { bldr.addSerializationExclusionStrategy(new SensitiveAnnotationExclusionStrategy()); } return bldr; }
From source file:fr.itinerennes.api.client.gson.ItineRennesApiGsonFactory.java
License:Open Source License
public static Gson newInstance(final boolean prettyJson) { final GsonBuilder gb = new GsonBuilder(); gb.setDateFormat(DATE_FORMAT);/*from w w w. j ava 2 s. c o m*/ gb.addSerializationExclusionStrategy(new ExcludeAnnotationStrategy()); gb.registerTypeAdapter(Time.class, new TimeTypeAdapter()); gb.registerTypeAdapter(Locale.class, new LocaleTypeAdapter()); gb.registerTypeAdapter(TimeZone.class, new TimeZoneTypeAdapter()); if (prettyJson) { gb.setPrettyPrinting(); } return gb.create(); }
From source file:org.eclipse.epp.internal.logging.aeri.ui.model.Reports.java
License:Open Source License
private static Gson createGson(boolean pretty) { GsonBuilder builder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); builder.registerTypeAdapter(UUID.class, new UuidTypeAdapter()); builder.addSerializationExclusionStrategy(new EmfFieldExclusionStrategy()); if (pretty) { builder.setPrettyPrinting();/*from w w w. j av a 2 s. com*/ } Gson gson = builder.create(); return gson; }
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(); }
From source file:uk.ac.horizon.aestheticodes.server.ExperienceParser.java
License:Open Source License
public static Gson createParser() { GsonBuilder build = new GsonBuilder(); build.registerTypeAdapter(new TypeToken<Map<String, Marker>>() { }.getType(), new MarkerMapAdapter()); build.addSerializationExclusionStrategy(new ExclusionStrategy() { @Override/*from w w w. ja v a 2s. co m*/ public boolean shouldSkipField(FieldAttributes f) { final JsonIgnore ignore = f.getAnnotation(JsonIgnore.class); return ignore != null; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); return build.create(); }