List of usage examples for com.google.gson GsonBuilder setExclusionStrategies
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)
From source file:com.softwaremagico.tm.json.CharacterJsonManager.java
License:Open Source License
public static String toJson(CharacterPlayer characterPlayer) { if (characterPlayer != null) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy()).create(); gsonBuilder.registerTypeAdapter(IValue.class, new IValueSerializer<IValue>()); gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter()); gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter()); gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter()); gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter()); gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter()); gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter()); gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter()); gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter()); gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter()); gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter()); // final Gson gson = new // GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); Gson gson = gsonBuilder.create(); String jsonText = gson.toJson(characterPlayer); return jsonText; }//from w ww.j a v a 2s. c om return null; }
From source file:com.softwaremagico.tm.json.PartyJsonManager.java
License:Open Source License
public static String toJson(Party party) { if (party != null) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy()).create(); gsonBuilder.registerTypeAdapter(IValue.class, new IValueSerializer<IValue>()); gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter()); gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter()); gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter()); gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter()); gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter()); gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter()); gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter()); gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter()); gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter()); gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter()); // final Gson gson = new // GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); Gson gson = gsonBuilder.create(); String jsonText = gson.toJson(party); return jsonText; }/*from w w w. ja v a 2 s. c o m*/ return null; }
From source file:com.xse.optstack.persconftool.base.persistence.PersConfExport.java
License:Open Source License
private static GsonBuilder createGsonBuilder() { final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setExclusionStrategies(new EmfExclusionStrategy()); gsonBuilder.setPrettyPrinting();// ww w.j av a 2 s.c om return gsonBuilder; }
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 {// w w w. j av a2s . 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:edu.mayo.cts2.framework.core.json.JsonConverter.java
License:Open Source License
/** * Builds the gson./* www.j a v a 2 s . com*/ * * @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:ee.ria.xroad.common.util.JsonUtils.java
License:Open Source License
/** * Get Gson with custom serializer./*from ww w . j a va 2 s.c o 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:fi.craplab.roameo.data.JsonExporter.java
License:Open Source License
private Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting();/*w w w.j ava2 s .co m*/ gsonBuilder.excludeFieldsWithoutExposeAnnotation(); if (!mExportPhoneNumbers) { gsonBuilder.setExclusionStrategies(new PhoneNumberExclusionStrategy()); } return gsonBuilder.create(); }
From source file:fi.vm.sade.organisaatio.business.impl.OrganisaatioTarjonta.java
License:EUPL
private void initGson() { GsonBuilder gsonBuilder = new GsonBuilder(); // Jtetn oid pois sill KoulutusHakutulosV1RDTO sislt kaksi oid kentt gsonBuilder.setExclusionStrategies(new ExclusionStrategy() { @Override/* w ww . j av a 2s . c om*/ public boolean shouldSkipField(FieldAttributes fieldAttributes) { return "oid".equals(fieldAttributes.getName()); } @Override public boolean shouldSkipClass(Class<?> arg0) { return false; } }); // Rekisteridn adapteri, jolla hoidetaan date tyyppi long arvona gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); } }); gson = gsonBuilder.create(); }
From source file:gov.nasa.arc.geocam.talk.service.TalkJsonConverter.java
License:Open Source License
@Override public String serialize(GeoCamTalkMessage message) { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat("MM/dd/yy HH:mm:ss"); builder.setExclusionStrategies(new TalkMessageExclusionStrategy()); Gson gson = builder.create();//from www. ja va 2 s .c om String ret = gson.toJson(message); return ret; }
From source file:io.barracks.ota.client.Utils.java
License:Apache License
public static Gson getRobolectricGson(GsonBuilder builder) { return builder.setExclusionStrategies(new ExclusionStrategy() { @Override//w ww .j a v a 2 s . com public boolean shouldSkipField(FieldAttributes f) { return "__robo_data__".equals(f.getName()); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }).create(); }