Example usage for com.google.gson GsonBuilder setExclusionStrategies

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

Introduction

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

Prototype

public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) 

Source Link

Document

Configures Gson to apply a set of exclusion strategies during both serialization and deserialization.

Usage

From source file:net.es.enos.services.gson.JsonProxy.java

License:Open Source License

public JsonProxy() {
    GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalendarSerializer());
    gson.registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalendarDeserializer());
    gson.setExclusionStrategies(new JsonExclusionStrategy());
    serializer = gson.create();/*  ww w  . j  a  v a 2 s  . c  om*/
}

From source file:org.apache.camel.component.gson.GsonDataFormat.java

License:Apache License

@Override
protected void doStart() throws Exception {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        if (exclusionStrategies != null && !exclusionStrategies.isEmpty()) {
            ExclusionStrategy[] strategies = exclusionStrategies
                    .toArray(new ExclusionStrategy[exclusionStrategies.size()]);
            builder.setExclusionStrategies(strategies);
        }/* w  ww.j a  v a2s . co m*/
        if (longSerializationPolicy != null) {
            builder.setLongSerializationPolicy(longSerializationPolicy);
        }
        if (fieldNamingPolicy != null) {
            builder.setFieldNamingPolicy(fieldNamingPolicy);
        }
        if (fieldNamingStrategy != null) {
            builder.setFieldNamingStrategy(fieldNamingStrategy);
        }
        if (serializeNulls != null && serializeNulls) {
            builder.serializeNulls();
        }
        if (prettyPrinting != null && prettyPrinting) {
            builder.setPrettyPrinting();
        }
        if (dateFormatPattern != null) {
            builder.setDateFormat(dateFormatPattern);
        }
        gson = builder.create();
    }
}

From source file:org.apache.zeppelin.rest.JsonResponse.java

License:Apache License

@Override
public String toString() {
    GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(InterpreterSetting.InterpreterInfo.class,
            new InterpreterInfoSerializer());
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }/*from   ww  w .  ja  v  a 2  s.c  om*/
    gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
    Gson gson = gsonBuilder.create();
    return gson.toJson(this);
}

From source file:org.apache.zeppelin.server.JsonResponse.java

License:Apache License

@Override
public String toString() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }/*from   w  w w  .  j  a  va  2 s . com*/
    gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
    Gson gson = gsonBuilder.create();
    return gson.toJson(this);
}

From source file:org.headsupdev.agile.api.rest.Api.java

License:Open Source License

private void setupBuilder() {
    GsonBuilder builder = getBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mmZ"); // ISO 8601 date/time format
    if (respectPublishAnnotation()) {
        // Prefer to only expose the fields I have annotated
        builder.setExclusionStrategies(new MissingPublishExclusionStrategy());
    }/*from   ww w.  j  a v a2 s. co m*/

    setupJson(builder);
}

From source file:org.netxms.websvc.json.JsonTools.java

License:Open Source License

/**
 * Create JSON representation for given object
 * //w  ww.  j  av  a  2 s  . c o  m
 * @param object object to serialize
 * @return JSON code
 */
public static String jsonFromObject(Object object) {
    if (object instanceof JsonObject)
        return ((JsonObject) object).toString();
    if (object instanceof ResponseContainer)
        return ((ResponseContainer) object).toJson();

    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting(); // FIXME: remove for production
    builder.registerTypeAdapter(Date.class, new DateAdapter());
    builder.registerTypeAdapter(InetAddress.class, new InetAddressAdapter());
    builder.registerTypeAdapter(InetAddressEx.class, new InetAddressExAdapter());
    builder.registerTypeAdapter(MacAddress.class, new MacAddressAdapter());
    builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(Internal.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> c) {
            return c.isAnnotationPresent(Internal.class);
        }
    });
    return builder.create().toJson(object);
}

From source file:org.pascani.dsl.dbmapper.JsonUtility.java

License:Open Source License

private Gson buildGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(UUID.class, new UUIDInstanceCreator());
    builder.registerTypeAdapter(UUID.class, new UUIDSerializer());
    builder.registerTypeAdapter(UUID.class, new UUIDDeserializer());
    builder.setExclusionStrategies(new TransientExclusionStrategy());
    return builder.create();
}

From source file:org.raspinloop.config.GsonProperties.java

License:Apache License

public GsonProperties(HardwareEnumerator enumerator) {

    GsonBuilder builder = new GsonBuilder();

    Collection<HardwareProperties> boards = enumerator.buildListImplementing(BoardHardware.class);
    boolean delegateExisting = false;
    for (HardwareProperties hardwareProps : boards) {
        if (hardwareProps instanceof BoardHardwareDelegate) {
            delegateExisting = true;/* w  w  w .  ja v  a 2 s .com*/
        }
    }
    if (!delegateExisting)
        boards.add(new BoardHardwareDelegate());
    builder = registerImpl(boards, builder, BoardHardware.class);

    builder = registerImpl(enumerator.buildListImplementing(BoardExtentionHardware.class), builder,
            BoardExtentionHardware.class);
    builder = registerImpl(enumerator.buildListImplementing(UARTComponent.class), builder, UARTComponent.class);
    builder = registerImpl(enumerator.buildListImplementing(I2CComponent.class), builder, I2CComponent.class);
    builder = registerImpl(enumerator.buildListImplementing(SPIComponent.class), builder, SPIComponent.class);
    builder.registerTypeAdapter(Pin.class, new InstanceCreator<PinImpl>() {

        public PinImpl createInstance(Type type) {
            return new PinImpl();
        }
    });

    builder.setExclusionStrategies(new PreventLoop());

    gsonExt = builder.setPrettyPrinting().create();
}

From source file:org.thingsplode.synapse.serializers.gson.GsonSerializer.java

License:Apache License

public GsonSerializer(boolean prettyPrint, HashMap<Type, Object> typeaAdapters,
        List<ExclusionStrategy> exlusionStrategies) {
    GsonBuilder b = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter())
            .registerTypeAdapter(HttpResponseStatus.class, new HttpResponseStatusAdapter());
    if (typeaAdapters != null && !typeaAdapters.isEmpty()) {
        typeaAdapters.forEach((k, v) -> {
            b.registerTypeAdapter(k, v);
        });/*from   ww w  .ja v a2  s  .c o m*/
    }
    if (exlusionStrategies != null && !exlusionStrategies.isEmpty()) {
        b.setExclusionStrategies((ExclusionStrategy[]) exlusionStrategies.toArray());
    }
    if (prettyPrint) {
        b.setPrettyPrinting();
    }
    this.gson = b.create();
}

From source file:pl.com.tt.play.modules.json.JsonRenderer.java

License:Apache License

@After
@Catch(TemplateNotFoundException.class)
static void renderModelsAsJson(Throwable result) throws Throwable {
    JsonEndpoint classAnnotation = getControllerAnnotation(JsonEndpoint.class);
    JsonEndpoint methodAnnotation = getActionAnnotation(JsonEndpoint.class);

    boolean process = (methodAnnotation != null || classAnnotation != null) && isJsonRequested(result);

    if (process) {
        Map<String, Object> args = Scope.RenderArgs.current().data;
        Map<String, Object> outputObjects = new HashMap<String, Object>();
        if (methodAnnotation != null && methodAnnotation.output().length > 0) {
            String[] outputArgs = methodAnnotation.output();
            for (String argName : outputArgs) {
                Object object = args.get(argName);
                outputObjects.put(argName, object);
            }/*  ww w  .  j a  v  a  2s . co  m*/
        } else {
            for (Map.Entry<String, Object> entry : args.entrySet()) {
                if (entry.getValue() instanceof JPABase) {
                    outputObjects.put(entry.getKey(), entry.getValue());
                }
            }
        }
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeHierarchyAdapter(Collection.class, new RunTimeTypeCollectionAdapter());
        gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
        Gson gson = gsonBuilder.create();
        String json = gson.toJson(outputObjects);
        renderJSON(json);
    }

    if (result != null) {
        throw result;
    }
}