List of usage examples for com.google.gson FieldAttributes getName
public String getName()
From source file:br.com.caelum.vraptor.serialization.gson.Exclusions.java
License:Open Source License
@Override public boolean shouldSkipField(FieldAttributes f) { SkipSerialization annotation = f.getAnnotation(SkipSerialization.class); if (annotation != null) return true; String fieldName = f.getName(); Class<?> definedIn = f.getDeclaringClass(); for (Entry<String, Class<?>> include : serializee.getIncludes().entries()) { if (isCompatiblePath(include, definedIn, fieldName)) { return false; }//from w w w . j a v a2 s .com } for (Entry<String, Class<?>> exclude : serializee.getExcludes().entries()) { if (isCompatiblePath(exclude, definedIn, fieldName)) { return true; } } Field field = reflectionProvider.getField(definedIn, fieldName); return !serializee.isRecursive() && !shouldSerializeField(field.getType()); }
From source file:cn.teamlab.wg.framework.struts2.json.JsonSerializerExclusionStrategy.java
License:Apache License
@Override public boolean shouldSkipField(FieldAttributes f) { if (f.getAnnotation(DontExpose.class) != null) { return true; }// w w w .j a v a2 s .co m if (fieldsToExclude == null || fieldsToExclude.isEmpty()) { return false; } for (String fieldToExclude : fieldsToExclude) { if (fieldToExclude == null || fieldToExclude.lastIndexOf('.') == -1) { continue; } String[] split = fieldToExclude.split("\\."); if (split != null && split.length > 1) { String fieldClassName = (split[split.length - 2]).toLowerCase(); String fieldAttrName = (split[split.length - 1]).toLowerCase(); String className = (className(f)).toLowerCase(); String classAttrName = (f.getName()).toLowerCase(); if (className.equals(fieldClassName) && classAttrName.equals(fieldAttrName)) { return true; } } } return fieldsToExclude.contains(f.getName()); }
From source file:com.cloudcontrolled.api.client.json.JsonDeserializer.java
License:Apache License
/** * /*w w w. j a va 2s . c o m*/ */ private void initializeGson() { gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { public boolean shouldSkipField(FieldAttributes fieldAttributes) { if (fieldAttributes.getName().equalsIgnoreCase("serialVersionUUID")) { return true; } return false; } public boolean shouldSkipClass(Class<?> arg0) { return false; } }).create(); }
From source file:com.collaide.fileuploader.models.repositorty.RepoItems.java
public RepoItems() { gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { @Override//ww w .j ava 2 s . co m public boolean shouldSkipField(FieldAttributes fa) { return fa.getName().equals("id") && id == -1; } @Override public boolean shouldSkipClass(Class<?> type) { return false; } }).create(); }
From source file:com.devbliss.doctest.utils.JSONHelper.java
License:Apache License
/** * /*from w w w.ja va2 s .c o m*/ * Converts the given POJO and will skip the given fields while doing so. * If prettyPrint is true, the output will be nicely formatted. * * @param obj * @param excludedFields * @param prettyPrint * @return */ public String toJsonAndSkipCertainFields(Object obj, final List<String> excludedFields, boolean prettyPrint) { ExclusionStrategy strategy = new ExclusionStrategy() { public boolean shouldSkipField(FieldAttributes f) { if (excludedFields.contains(f.getName())) { return true; } return false; } public boolean shouldSkipClass(Class<?> clazz) { return false; } }; GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy) .addDeserializationExclusionStrategy(strategy); if (prettyPrint) builder.setPrettyPrinting(); return builder.create().toJson(obj); }
From source file:com.division.jsonrpc.api.SuperclassExclusionStrategy.java
License:Apache License
public boolean shouldSkipField(FieldAttributes fieldAttributes) { String fieldName = fieldAttributes.getName(); Class<?> theClass = fieldAttributes.getDeclaringClass(); return isFieldInSuperclass(theClass, fieldName); }
From source file:com.gilecode.yagson.strategy.ExcludeFieldsInClassesByNames.java
License:Apache License
@Override public boolean shouldSkipField(FieldAttributes f) { if (skipFieldNames.contains(f.getName())) { for (Class<?> declaringSuperClass : declaringSuperClasses) { if (declaringSuperClass != null && declaringSuperClass.isAssignableFrom(f.getDeclaringClass())) { return true; }/*from ww w .j ava 2 s . co m*/ } } return false; }
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 .j a v a2 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)); }/*from w w w.j ava2 s . c o m*/ 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.gst.infrastructure.core.api.ParameterListExclusionStrategy.java
License:Apache License
@Override public boolean shouldSkipField(final FieldAttributes f) { return this.parameterNamesToSkip.contains(f.getName()); }