Example usage for com.google.gson FieldAttributes getAnnotation

List of usage examples for com.google.gson FieldAttributes getAnnotation

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes getAnnotation.

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotation) 

Source Link

Document

Return the T annotation object from this field if it exist; otherwise returns null .

Usage

From source file:com.tomeokin.lspush.data.support.GsonStrategy.java

License:Apache License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return f.getAnnotation(GsonIgnore.class) != null;
}

From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.commands.ShowCommand.java

License:Apache License

private Gson getGson() {
    return new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override/*from  ww  w . jav  a 2  s .  c o  m*/
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(Hide.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> arg0) {
            return false;
        }
    }).create();
}

From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.restful.strategies.JsonPresentateStrategy.java

License:Apache License

@Override
public String presentate(Object presentateObjects) {
    // make new gson object to support multithreading environment
    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
        @Override//from ww  w .j  a  v  a  2  s .c o  m
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(Hide.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> arg0) {
            return false;
        }
    }).setPrettyPrinting().serializeNulls().create();

    return gson.toJson(presentateObjects);
}

From source file:ee.ria.xroad.common.util.JsonUtils.java

License:Open Source License

/**
 * Get Gson with custom serializer./*from   w  ww .j a  va2s. co  m*/
 * @return Gson instance with custom serializer.
 */
public static Gson getSerializer() {
    GsonBuilder builder = new GsonBuilder();
    builder.disableHtmlEscaping();
    builder.registerTypeAdapter(ClientId.class, new ClientIdSerializer());

    builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(Exclude.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    });

    return builder.create();
}

From source file:hd3gtv.tools.GsonIgnoreStrategy.java

License:Open Source License

public boolean shouldSkipField(FieldAttributes f) {
    if (DEBUG) {/*from  ww  w  .  j  av a2 s .co m*/
        System.out.println(f.getName());
        System.out.println(f.getAnnotation(GsonIgnore.class));
    }
    return f.getAnnotation(GsonIgnore.class) != null;
}

From source file:in.bookmylab.AnnotationExclusionStrategy.java

License:MIT License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return f.getAnnotation(JsonDeserializeOnly.class) != null;
}

From source file:io.github.proxyprint.kitchen.utils.gson.AnnotationExclusionStrategy.java

License:Apache License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return f.getAnnotation(Exclude.class) != null;
}

From source file:io.vertigo.vega.rest.engine.GoogleJsonEngine.java

License:Apache License

private Gson createGson() {
    return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").setPrettyPrinting()
            //TODO  registerTypeAdapter(String.class, new EmptyStringAsNull<>())// add "" <=> null
            //.serializeNulls()//On veut voir les null
            .registerTypeAdapter(UiObject.class, new UiObjectDeserializer<>())
            .registerTypeAdapter(UiListDelta.class, new UiListDeltaDeserializer<>())
            .registerTypeAdapter(UiList.class, new UiListDeserializer<>())
            //.registerTypeAdapter(UiObjectExtended.class, new UiObjectExtendedDeserializer<>())
            /*.registerTypeAdapter(DtObjectExtended.class, new JsonSerializer<DtObjectExtended<?>>() {
               @Override//w w w . j  a v  a2  s . c o m
               public JsonElement serialize(final DtObjectExtended<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
                  final JsonObject jsonObject = new JsonObject();
                  final JsonObject jsonInnerObject = (JsonObject) context.serialize(src.getInnerObject());
                  for (final Entry<String, JsonElement> entry : jsonInnerObject.entrySet()) {
                     jsonObject.add(entry.getKey(), entry.getValue());
                  }
                  for (final Entry<String, Serializable> entry : src.entrySet()) {
                     jsonObject.add(entry.getKey(), context.serialize(entry.getValue()));
                  }
                  return jsonObject;
               }
            })*/
            .registerTypeAdapter(FacetedQueryResult.class, new JsonSerializer<FacetedQueryResult>() {
                @Override
                public JsonElement serialize(final FacetedQueryResult facetedQueryResult, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    final JsonObject jsonObject = new JsonObject();
                    //1- add result list as data
                    final JsonArray jsonData = (JsonArray) context.serialize(facetedQueryResult.getDtList());
                    jsonObject.add("data", jsonData);

                    //2- add facet list as facets
                    final List<Facet> facets = facetedQueryResult.getFacets();
                    final JsonArray facetList = new JsonArray();
                    for (final Facet facet : facets) {
                        final JsonObject jsonFacet = new JsonObject();

                        final Map<String, Long> maps = new HashMap<>();
                        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
                            maps.put(entry.getKey().getLabel().getDisplay(), entry.getValue());
                        }

                        final JsonObject jsonFacetValues = (JsonObject) context.serialize(maps);
                        final String facetName = facet.getDefinition().getLabel().getDisplay();
                        jsonFacet.add(facetName, jsonFacetValues);
                        facetList.add(jsonFacet);
                    }
                    jsonObject.add("facets", context.serialize(facetList));
                    return jsonObject;
                }
            }).registerTypeAdapter(ComponentInfo.class, new JsonSerializer<ComponentInfo>() {
                @Override
                public JsonElement serialize(final ComponentInfo componentInfo, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    final JsonObject jsonObject = new JsonObject();
                    jsonObject.add(componentInfo.getTitle(), context.serialize(componentInfo.getValue()));
                    return jsonObject;
                }
            }).registerTypeAdapter(List.class, new JsonSerializer<List>() {

                @Override
                public JsonElement serialize(final List src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isEmpty()) {
                        return null;
                    }
                    return context.serialize(src);
                }
            }).registerTypeAdapter(Map.class, new JsonSerializer<Map>() {

                @Override
                public JsonElement serialize(final Map src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isEmpty()) {
                        return null;
                    }
                    return context.serialize(src);
                }
            }).registerTypeAdapter(DefinitionReference.class, new JsonSerializer<DefinitionReference>() {

                @Override
                public JsonElement serialize(final DefinitionReference src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    return context.serialize(src.get().getName());
                }
            }).registerTypeAdapter(Option.class, new JsonSerializer<Option>() {

                @Override
                public JsonElement serialize(final Option src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isDefined()) {
                        return context.serialize(src.get());
                    }
                    return null; //rien
                }
            }).registerTypeAdapter(Class.class, new JsonSerializer<Class>() {

                @Override
                public JsonElement serialize(final Class src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    return new JsonPrimitive(src.getName());
                }
            }).addSerializationExclusionStrategy(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(final FieldAttributes arg0) {
                    if (arg0.getAnnotation(JsonExclude.class) != null) {
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean shouldSkipClass(final Class<?> arg0) {
                    return false;
                }
            }).create();
}

From source file:meta.FieldExcluderForGson.java

License:Open Source License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return (f.getName().equals("id") || f.getName().startsWith("_")
            || f.getAnnotation(ManyToOne.class) != null);
}

From source file:net.ljcomputing.gson.strategy.ExcludeFromJsonAnnotationExclusionStrategy.java

License:Apache License

/**
 * @see com.google.gson.ExclusionStrategy
 * #shouldSkipField(com.google.gson.FieldAttributes)
 *///from w w w. j av a 2 s .  co  m
@Override
public boolean shouldSkipField(final FieldAttributes fieldAttributes) {
    return fieldAttributes.getAnnotation(ExcludeFromJson.class) != null;
}