List of usage examples for com.google.gson GsonBuilder setFieldNamingPolicy
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention)
From source file:ar.com.wolox.wolmo.networking.di.modules.GsonModule.java
License:Open Source License
@Provides static GsonBuilder provideGsonBuilder(@Named("newInstance") GsonBuilder gsonBuilder, @NonNull FieldNamingPolicy namingPolicy, @Nullable GsonTypeAdapter... typeAdapters) { gsonBuilder.setFieldNamingPolicy(namingPolicy); if (typeAdapters != null && typeAdapters.length > 0) { for (GsonTypeAdapter typeAdapter : typeAdapters) { gsonBuilder.registerTypeAdapter(typeAdapter.getType(), typeAdapter.getTypeAdapter()); }/*from w ww.java 2 s . c o m*/ } else { gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer()); } return gsonBuilder; }
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 . j a v a 2s .co m*/ 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:cc.kave.commons.utils.json.JsonUtils.java
License:Apache License
private static GsonBuilder createBuilder() { GsonBuilder gb = new GsonBuilder(); // add support for new Java 8 date/time framework gb.registerTypeHierarchyAdapter(LocalDateTime.class, new LocalDateTimeConverter()); Converters.registerAll(gb);//from w ww.j a v a 2s. co m gb.registerTypeAdapter(Duration.class, new DurationConverter()); GsonUtil.addTypeAdapters(gb); registerNames(gb); registerSSTHierarchy(gb); registerEventHierarchy(gb); gb.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); gb.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); return gb; }
From source file:com.addhen.voto.sdk.BaseApiBuilder.java
License:Apache License
private void initializeGson() { GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); builder.registerTypeAdapter(Date.class, new DateDeserializer()); mGson = builder.create();//from w ww . j a va 2s .c o m }
From source file:com.aliakseipilko.flightdutytracker.dagger.modules.GsonModule.java
License:Open Source License
@Provides public Gson provideGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); return gsonBuilder.create(); }
From source file:com.aliakseipilko.flightdutytracker.presenter.impl.CreateFlightPresenter.java
License:Open Source License
public CreateFlightPresenter(CreateFlightFragment view) { this.view = view; repository = new FlightRepository(); //This should be injected with Dagger, but its retarded GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); codeConverter = new AirportCodeConverter(view.getContext().getApplicationContext(), gsonBuilder.create()); }
From source file:com.aliakseipilko.flightdutytracker.presenter.impl.EditFlightPresenter.java
License:Open Source License
public EditFlightPresenter(EditFlightFragment view) { this.view = view; repository = new FlightRepository(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY); codeConverter = new AirportCodeConverter(view.getContext().getApplicationContext(), gsonBuilder.create()); }
From source file:com.bing.maps.rest.services.impl.BaseBingMapsApiQuery.java
License:Apache License
/** * Gets the gson builder.// ww w.j ava 2 s.c o m * * @return the gson builder */ protected GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat(ApplicationConstants.RFC822DATEFORMAT); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); builder.registerTypeAdapter(LocationType.class, new JsonDeserializer<LocationType>() { @Override public LocationType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return LocationType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(AddressComponentType.class, new JsonDeserializer<AddressComponentType>() { @Override public AddressComponentType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return AddressComponentType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(TravelMode.class, new JsonDeserializer<TravelMode>() { @Override public TravelMode deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return TravelMode.fromValue(arg0.getAsString()); } }); return builder; }
From source file:com.dabay6.android.apps.carlog.app.InitializationIntentService.java
License:Open Source License
/** * @return/*from ww w .java 2 s. c om*/ */ private Gson buildGson() { final GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); return builder.create(); }
From source file:com.example.repository_pattern_demo.di.HttpModule.java
License:Apache License
@Provides @Singleton//from w w w.jav a2s . c o m Gson providesGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(TypeAdapterFactoryImpl.create()); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gsonBuilder.create(); }