List of usage examples for com.google.gson ExclusionStrategy ExclusionStrategy
ExclusionStrategy
From source file:com.thoughtworks.go.server.service.PipelineService.java
License:Apache License
private ExclusionStrategy getGsonExclusionStrategy() { return new ExclusionStrategy() { @Override//from w ww . j a v a2 s.c om public boolean shouldSkipField(FieldAttributes f) { return f.getName().equals("materialConfig") || f.getName().equals("parents") || f.getName().equals("children"); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }; }
From source file:com.tsc9526.monalisa.tools.json.MelpJson.java
License:Open Source License
public static GsonBuilder createGsonBuilder() { return new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() { public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) { if (src == src.longValue()) { return new JsonPrimitive(src.longValue()); }// www. j a v a 2 s. c o m return new JsonPrimitive(src); } }).setExclusionStrategies(new ExclusionStrategy() { public boolean shouldSkipField(FieldAttributes fa) { return fa.getName().startsWith("$"); } public boolean shouldSkipClass(Class<?> clazz) { return false; } }).setDateFormat(Conversion.DEFAULT_DATETIME_FORMAT); }
From source file:com.xse.optstack.persconftool.base.persistence.PersConfExport.java
License:Open Source License
private boolean persistGroup(final File resourceFolder, final EApplicationGroup group) { final boolean persistStatus = true; final File groupFolder = new File( resourceFolder.getAbsolutePath() + File.separator + group.getType().getLiteral().toLowerCase()); try {/*from w w w .ja va 2s . c om*/ groupFolder.mkdir(); } catch (final SecurityException e) { Logger.error(Activator.PLUGIN_ID, "Not allowed to create group folder: " + groupFolder.getAbsolutePath(), e); return false; } final Set<String> appNames = new HashSet<>(); // ################ Installation Rules final Gson installRulesJson = PersConfExport.createGsonBuilder().create(); final JsonObject installRulesObj = new JsonObject(); for (final EApplication eApplication : group.getApplications()) { if (eApplication.getInstallRule() != null) { installRulesObj.add(eApplication.getName(), new JsonPrimitive( PersConfDefinitions.INSTALL_RULE_PREFIX + eApplication.getInstallRule().getLiteral())); } } try (final FileWriter fileWriter = new FileWriter(new File( groupFolder.getAbsolutePath() + File.separator + PersConfDefinitions.INSTALL_RULES_FILE))) { fileWriter.write(installRulesJson.toJson(installRulesObj)); } catch (final IOException e) { Logger.error(Activator.PLUGIN_ID, "Error writing install rules configuration file!", e); } // ################ Ownership Configuration final Gson ownershipConfigurationJson = PersConfExport.createGsonBuilder().create(); final JsonObject ownershipConfigurationObj = new JsonObject(); for (final EApplication eApplication : group.getApplications()) { final JsonObject appElement = new JsonObject(); boolean saveApplication = false; if (eApplication.getUserName() == null || eApplication.getUserName().equals("")) appElement.add(PersConfDefinitions.USERNAME_KEY_NAME, new JsonPrimitive("")); else { appElement.add(PersConfDefinitions.USERNAME_KEY_NAME, new JsonPrimitive(eApplication.getUserName())); saveApplication = true; } if (eApplication.getGroupName() == null || eApplication.getGroupName().equals("")) appElement.add(PersConfDefinitions.GROUPNAME_KEY_NAME, new JsonPrimitive("")); else { appElement.add(PersConfDefinitions.GROUPNAME_KEY_NAME, new JsonPrimitive(eApplication.getGroupName())); saveApplication = true; } if (eApplication.getUserId() == null) appElement.add(PersConfDefinitions.USERID_KEY_NAME, new JsonPrimitive("")); else { appElement.add(PersConfDefinitions.USERID_KEY_NAME, new JsonPrimitive(String.valueOf(eApplication.getUserId()))); saveApplication = true; } if (eApplication.getGroupId() == null) appElement.add(PersConfDefinitions.GROUPID_KEY_NAME, new JsonPrimitive("")); else { appElement.add(PersConfDefinitions.GROUPID_KEY_NAME, new JsonPrimitive(String.valueOf(eApplication.getGroupId()))); saveApplication = true; } if (saveApplication) ownershipConfigurationObj.add(eApplication.getName(), appElement); } try (final FileWriter fileWriter = new FileWriter(new File(groupFolder.getAbsolutePath() + File.separator + PersConfDefinitions.OWNERSHIP_CONFIGURATION_FILE))) { fileWriter.write(ownershipConfigurationJson.toJson(ownershipConfigurationObj)); } catch (final IOException e) { Logger.error(Activator.PLUGIN_ID, "Error writing ownership configuration file!", e); } for (final EApplication eApplication : group.getApplications()) { final File applicationFolder = new File(groupFolder + File.separator + eApplication.getName()); applicationFolder.mkdir(); this.exportCHeader(eApplication); // ################ Resource Configuration final GsonBuilder resourceConfigJsonBuilder = PersConfExport.createGsonBuilder(); resourceConfigJsonBuilder.registerTypeAdapter(EApplicationImpl.class, new ResouceConfigurationApplicationTypeAdapter()); resourceConfigJsonBuilder.registerTypeAdapter(EConfigurationImpl.class, new ResourceConfigurationConfigurationTypeAdapter()); resourceConfigJsonBuilder.registerTypeAdapter(EPolicy.class, new ResouceConfigurationTypeAdapter()); resourceConfigJsonBuilder.registerTypeAdapter(EStorage.class, new ResouceConfigurationTypeAdapter()); resourceConfigJsonBuilder.registerTypeAdapter(EDefaultDataType.class, new ResouceConfigurationTypeAdapter()); resourceConfigJsonBuilder.setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(final FieldAttributes fieldAttributes) { return false; } @Override public boolean shouldSkipClass(final Class<?> clazz) { return clazz.isAssignableFrom(EDefaultData.class); } }); final Gson gson = resourceConfigJsonBuilder.create(); try (final FileWriter fileWriter = new FileWriter(new File(applicationFolder.getAbsolutePath() + File.separator + PersConfDefinitions.RESOURCE_CONFIGURATION_FILE))) { fileWriter.write(gson.toJson(eApplication)); } catch (final IOException e) { Logger.error(Activator.PLUGIN_ID, "Error writing resource configuration file!", e); } // ################ Install Exceptions final Gson installExceptionsGson = PersConfExport.createGsonBuilder().create(); final JsonObject installExceptionsObj = new JsonObject(); for (final EResource resource : eApplication.getResources()) { if (resource.getInstallException() != null) { installExceptionsObj.add(resource.getName(), new JsonPrimitive(PersConfDefinitions.INSTALL_EXCEPTION_PREFIX + resource.getInstallException().getLiteral())); } } try (final FileWriter fileWriter = new FileWriter(new File(applicationFolder.getAbsolutePath() + File.separator + PersConfDefinitions.INSTALL_EXCEPTIONS_FILE))) { fileWriter.write(installExceptionsGson.toJson(installExceptionsObj)); } catch (final IOException e) { Logger.error(Activator.PLUGIN_ID, "Error writing install exceptions configuration file!", e); } final File fileFolder = new File(applicationFolder + File.separator + PersConfDefinitions.FILE_FOLDER); fileFolder.mkdir(); final File configDefaultFileFolder = new File( fileFolder + File.separator + PersConfDefinitions.CONFIGURABLE_DEFAULT_DATA_FOLDER); configDefaultFileFolder.mkdir(); final File defaultFileFolder = new File( fileFolder + File.separator + PersConfDefinitions.DEFAULT_DATA_FOLDER); defaultFileFolder.mkdir(); // ################ Default Data final File keyFolder = new File(applicationFolder + File.separator + PersConfDefinitions.KEY_FOLDER); keyFolder.mkdir(); // ################ Factory Default // boolean hasDefaultKeyData = false; // for (final EResource eResource : eApplication.getResources()) { // if (DefaultDataApplicationTypeAdapter.hasDefaultDataValue(eResource.getConfiguration().getFactoryDefault())) { // hasDefaultKeyData = true; // break; // } // } PersConfExport.copyDefaultDataFiles(defaultFileFolder, eApplication, EConfiguration::getFactoryDefault); final File factoryDefaultsFile = new File( keyFolder.getAbsolutePath() + File.separator + PersConfDefinitions.FACTORY_DEFAULTS_FILE); final GsonBuilder factoryDefaultGsonBuilder = PersConfExport.createGsonBuilder(); factoryDefaultGsonBuilder.registerTypeAdapter(EApplicationImpl.class, new DefaultDataApplicationTypeAdapter(true)); factoryDefaultGsonBuilder.registerTypeAdapter(EDefaultDataImpl.class, new DefaultDataTypeAdapter()); final Gson factoryDefaultGson = factoryDefaultGsonBuilder.create(); try (final FileWriter fileWriter = new FileWriter(factoryDefaultsFile)) { fileWriter.write(factoryDefaultGson.toJson(eApplication)); } catch (final IOException e) { e.printStackTrace(); } // ################ Configurable Default // hasDefaultKeyData = false; // for (final EResource eResource : eApplication.getResources()) { // if (DefaultDataApplicationTypeAdapter.hasDefaultDataValue(eResource.getConfiguration().getConfigDefault())) { // hasDefaultKeyData = true; // break; // } // } PersConfExport.copyDefaultDataFiles(configDefaultFileFolder, eApplication, EConfiguration::getConfigDefault); final File configDefaultsFile = new File( keyFolder.getAbsolutePath() + File.separator + PersConfDefinitions.CONFIGURABLE_DEFAULTS_FILE); final GsonBuilder configurableDefaultGsonBuilder = PersConfExport.createGsonBuilder(); configurableDefaultGsonBuilder.registerTypeAdapter(EApplicationImpl.class, new DefaultDataApplicationTypeAdapter(false)); configurableDefaultGsonBuilder.registerTypeAdapter(EDefaultDataImpl.class, new DefaultDataTypeAdapter()); final Gson configurableDefaultGson = configurableDefaultGsonBuilder.create(); try (final FileWriter fileWriter = new FileWriter(configDefaultsFile)) { fileWriter.write(configurableDefaultGson.toJson(eApplication)); } catch (final IOException e) { e.printStackTrace(); } appNames.add(eApplication.getName()); } final File[] groupFiles = groupFolder.listFiles((FileFilter) DirectoryFileFilter.INSTANCE); // remove all application folders not part of the current configuration anymore if (groupFiles != null) { for (final File appFolder : groupFiles) { if (!appNames.contains(appFolder.getName())) { try { FileUtils.deleteDirectory(appFolder); } catch (final IOException e) { Logger.error(Activator.PLUGIN_ID, "Unable to delete removed application directory: " + appFolder.getName(), e); } } } } return persistStatus; }
From source file:com.yalin.fidoclient.op.ASMMessageHandler.java
License:Apache License
private static ExclusionStrategy getExclusionStrategy() { return new ExclusionStrategy() { @Override// w ww . jav a 2s .c o m public boolean shouldSkipField(FieldAttributes f) { return f.getName().equals(ASMRequest.authenticatorIndexName); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }; }
From source file:de.chaosfisch.services.ExportPostProcessor.java
License:Open Source License
@Override public Upload process(final Upload upload) { if (!config.getBoolean(ExportPostProcessor.JSON_LOGFILES, false)) { return upload; }/*from w ww . j a v a 2 s . c o m*/ LOGGER.info("Running export postprocessor"); final Gson gson = new GsonBuilder().setPrettyPrinting() .addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(final FieldAttributes f) { return f.getDeclaringClass().isAssignableFrom(Account.class); } @Override public boolean shouldSkipClass(final Class<?> clazz) { return clazz.isAssignableFrom(Account.class); } }).registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).serializeNulls().create(); try { final Upload copy = gson.fromJson(gson.toJson(upload), Upload.class); try { Files.createDirectories(Paths.get(ApplicationData.DATA_DIR + "/uploads/")); Files.write( Paths.get(String.format("%s/uploads/%s.json", ApplicationData.DATA_DIR, copy.getVideoid())), gson.toJson(copy).getBytes(Charsets.UTF_8)); } catch (final IOException e) { LOGGER.warn("Couldn't write json log.", e); } LOGGER.info("Finished export postprocessor"); } catch (final Exception e) { e.printStackTrace(); } return upload; }
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/*w w w . j av a2 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 www . ja 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:edu.mayo.cts2.framework.core.json.JsonConverter.java
License:Open Source License
/** * Builds the gson.// w ww .ja v a 2 s .co m * * @return the gson */ protected Gson buildGson() { GsonBuilder gson = new GsonBuilder(); gson.setDateFormat(ISO_DATE_FORMAT); gson.setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getName().equals(CHOICE_VALUE); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); gson.registerTypeAdapter(List.class, new EmptyCollectionSerializer()); gson.registerTypeAdapter(TsAnyType.class, new TsAnyTypeSerializer()); gson.registerTypeAdapter(Date.class, new DateTypeAdapter()); gson.registerTypeAdapterFactory(new ChangeableTypeAdapterFactory()); gson.registerTypeAdapterFactory(new ChangeableResourceTypeAdapterFactory()); gson.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field field) { String fieldName = field.getName(); char[] array = fieldName.toCharArray(); if (array[0] == '_') { array = ArrayUtils.remove(array, 0); } String name = new String(array); if (name.endsWith(LIST_SUFFIX)) { name = StringUtils.removeEnd(name, LIST_SUFFIX); } return name; } }); return gson.create(); }
From source file:edu.utd.cs6301.shas.controller.ACSettingServlet.java
public void init() throws ServletException { gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { public boolean shouldSkipClass(Class<?> clazz) { return (clazz == Airconditioning.class); }//from ww w.j a v a 2 s .c o m /** * Custom field exclusion goes here */ public boolean shouldSkipField(FieldAttributes f) { return false; } }) /** * Use serializeNulls method if you want To serialize null * values By default, Gson does not serialize null values */ .serializeNulls().create(); }
From source file:edu.utd.cs6301.shas.controller.AirConditioningServlet.java
public void init() throws ServletException { gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { public boolean shouldSkipClass(Class<?> clazz) { return (clazz == Acsetting.class); }//from w w w .ja va 2 s .c om /** * Custom field exclusion goes here */ public boolean shouldSkipField(FieldAttributes f) { return false; } }) /** * Use serializeNulls method if you want To serialize null * values By default, Gson does not serialize null values */ .serializeNulls().create(); }