List of usage examples for com.google.gson GsonBuilder setLenient
public GsonBuilder setLenient()
From source file:ca.oson.json.Oson.java
License:Open Source License
public Gson getGson() { if (gson == null) { GsonBuilder gsonBuilder = new GsonBuilder(); switch (getDefaultType()) { case ALWAYS: gsonBuilder.serializeNulls(); break; case NON_NULL: break; case NON_EMPTY: break; case DEFAULT: gsonBuilder.serializeNulls(); break; default:/*from w w w. ja v a 2 s . com*/ gsonBuilder.serializeNulls(); break; } switch (getFieldNaming()) { case FIELD: // original field name: someField_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; case LOWER: // somefield_name -> some_field_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UPPER: //SOMEFIELD_NAME -> SomeFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); break; case CAMELCASE: // someFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; case UPPER_CAMELCASE: // SomeFieldName gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); break; case UNDERSCORE_CAMELCASE: // some_Field_Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_UPPER_CAMELCASE: // Some_Field_Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_LOWER: // some_field_name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case UNDERSCORE_UPPER: // SOME_FIELD_NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); break; case SPACE_CAMELCASE: // some Field Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_UPPER_CAMELCASE: // Some Field Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_LOWER: // some field name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case SPACE_UPPER: // SOME FIELD NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES); break; case DASH_CAMELCASE: // some-Field-Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_UPPER_CAMELCASE: // Some-Field-Name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_LOWER: // some-field-name gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; case DASH_UPPER: // SOME-FIELD-NAME gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES); break; default: gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); break; } if (getPrettyPrinting() && getIndentation() > 0) { gsonBuilder.setPrettyPrinting(); } gsonBuilder.setDateFormat(options.getSimpleDateFormat()); Set<FieldMapper> mappers = getFieldMappers(); Map<Class, ClassMapper> classMappers = getClassMappers(); List<ExclusionStrategy> strategies = new ArrayList<>(); if (mappers != null) { gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field f) { String fieldName = f.getName(); String serializedName = java2Json(f); if (!fieldName.equalsIgnoreCase(serializedName)) { // if null returned, this field is ignored return serializedName; } return json2Java(f); } }); for (FieldMapper mapper : mappers) { if (mapper.java == null || mapper.json == null || mapper.ignore) { strategies.add(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { String name = f.getName(); Class cls = f.getClass(); if (mapper.java == null) { if (mapper.json.equals(name)) { if (mapper.getType() == null || cls.equals(mapper.getType())) { return true; } } } else if (mapper.json == null || mapper.ignore) { if (mapper.java.equals(name)) { if (mapper.getType() == null || cls.equals(mapper.getType())) { return true; } } } return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }); } } } if (classMappers != null) { for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) { ClassMapper mapper = entry.getValue(); if (mapper.getType() == null) { mapper.setType(entry.getKey()); } if (mapper.ignore != null && mapper.ignore) { strategies.add(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { if (clazz.equals(mapper.getType())) { return true; } return false; } }); } } for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) { ClassMapper mapper = entry.getValue(); if (!mapper.ignore && mapper.constructor != null) { gsonBuilder.registerTypeAdapter(entry.getKey(), mapper.constructor); } } } int size = strategies.size(); if (size > 0) { gsonBuilder.setExclusionStrategies(strategies.toArray(new ExclusionStrategy[size])); } Double version = getVersion(); if (version != null) { gsonBuilder.setVersion(version); } if (isUseGsonExpose()) { gsonBuilder.excludeFieldsWithoutExposeAnnotation(); } if (!DefaultValue.isDefault(getPatterns())) { gsonBuilder.setLenient(); } gson = gsonBuilder.create(); } return gson; }
From source file:net.redstoneore.legacyfactions.Factions.java
License:Open Source License
public GsonBuilder getGsonBuilder() { if (this.gsonBuilder == null) { Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() { }.getType();//from w ww. j a va 2 s .co m Type mapLocalityToStringSetType = new TypeToken<Map<Locality, Set<String>>>() { }.getType(); GsonBuilder builder = new GsonBuilder().setPrettyPrinting(); try { builder.setLenient(); } catch (NoSuchMethodError e) { // older minecraft plugins don't have this in their version of Gson } this.gsonBuilder = builder.disableHtmlEscaping() .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) .registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter()) .registerTypeAdapter(mapFLocToStringSetType, new MapFlocationSetAdapter()) .registerTypeAdapter(mapLocalityToStringSetType, new MapLocalitySetAdapter()) .registerTypeAdapter(CrossColour.class, new CrossColourAdapter()) .registerTypeAdapter(CrossEntityType.class, new CrossEntityTypeAdapter()) ; } return this.gsonBuilder; }