Example usage for com.google.gson GsonBuilder setFieldNamingPolicy

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

Introduction

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

Prototype

public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) 

Source Link

Document

Configures Gson to apply a specific naming policy to an object's field during serialization and deserialization.

Usage

From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java

License:Apache License

/**
 * Constructs a new factory./*from   w w w  . j  ava 2  s  . com*/
 *
 * @param typeKey the key which the object to parse can be nested under. It will usually be the
 *        name of the object's type:
 *
 *        <pre>
 *            { "typeKey": { "field1": "test" } }
 * </pre>
 *
 *        When requesting a single object or a list of objects, the object will be nested under
 *        this type key.
 * @param type the type of object to load
 * @param wrapped if true, the input JSON must be wrapped with a JSON object that has a typeKey,
 *        as mentioned above.
 */
public GsonModelFactory(@NonNull final String typeKey, @NonNull final Class<T> type, final boolean wrapped) {
    mType = type;
    mTypeKey = typeKey;

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.registerTypeAdapter(MonetaryValue.class, new MonetaryValueTypeAdapter());
    gsonBuilder.registerTypeAdapter(Uri.class, new UriTypeAdapter());
    gsonBuilder.registerTypeAdapterFactory(new RequiredFieldTypeAdapterFactory());

    if (wrapped) {
        gsonBuilder.registerTypeAdapterFactory(new WrappedModelTypeAdapterFactory(type, typeKey));
    }

    onBuildFactory(gsonBuilder);

    mGson = NullUtils.nonNullContract(gsonBuilder.create());
}

From source file:com.urswolfer.gerrit.client.rest.gson.GsonFactory.java

License:Apache License

public static Gson create() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateDeserializer());
    builder.registerTypeAdapter(Date.class, new DateSerializer());
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
}

From source file:com.yucun.presentation.di.modules.ApplicationModule.java

License:Apache License

@Provides
@Singleton/*from   w w w . ja  v a2  s  . c  om*/
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return gsonBuilder.create();
}

From source file:gov.whitehouse.utils.GsonUtils.java

License:MIT License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls whether nulls should be serialized
 * @return created gson, never null/*from   w  w w  .j a  va2  s  . co m*/
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateFormatter());
    builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls) {
        builder.serializeNulls();
    }
    return builder.create();
}

From source file:io.weba.processor.flink.event.gson.factory.EventFactory.java

License:Open Source License

public Gson create() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer());
    gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer());
    gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer());
    gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer());
    gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer());
    gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer());
    gsonBuilder.serializeNulls();//from w w  w.  j  ava2 s.c  o m
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    gsonBuilder.enableComplexMapKeySerialization();

    return gsonBuilder.create();
}

From source file:mashup.fm.github.BaseService.java

License:Apache License

/**
 * Gets the gson builder.//  w  ww.j a  v  a  2  s. c o  m
 * 
 * @return the gson builder
 */
private static GsonBuilder getGsonBuilder() {
    // Init Builder
    GsonBuilder builder = new GsonBuilder();

    // This is the code that will try to parse dates
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            try {
                if (arg0.getAsString().indexOf(' ') == 0) {
                    // 2011-03-23T05:14:20-07:00
                    DateFormat df = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-mm:mm");
                    return df.parse(arg0.getAsString());
                }
                DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
                return df.parse(arg0.getAsString());

            } catch (Throwable t) {
                Logger.warn(ExceptionUtil.getStackTrace(t));
                return null;
            }
        }
    });

    // We use camel case and the api returns lower case with underscore -
    // fieldId versus field_id
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    // Return Builder
    return builder;
}

From source file:org.apache.atlas.services.AtlasPatchHandler.java

License:Apache License

public static Gson initializeGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Multiplicity.class, new MultiplicityDeserializer());
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    Gson gson = gsonBuilder.create();/* www . j av a  2s.co m*/

    return gson;
}

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);
        }//from  w w w  . j  av  a  2  s  .c o  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.hadoop.yarn.server.resourcemanager.security.HopsworksRMAppSecurityActions.java

License:Apache License

public HopsworksRMAppSecurityActions() throws MalformedURLException, GeneralSecurityException {
    ACCEPTABLE_HTTP_RESPONSES.add(HttpStatus.SC_OK);
    ACCEPTABLE_HTTP_RESPONSES.add(HttpStatus.SC_NO_CONTENT);
    authHeader = new AtomicReference<>();
    GsonBuilder parserBuilder = new GsonBuilder();
    parserBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    parserBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    jsonParser = parserBuilder.create();

    tokenRenewer = Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder().setNameFormat("JWT renewer thread").setDaemon(true).build());
}

From source file:org.eclipse.egit.github.core.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls/*from w w  w.  ja v  a  2s  .  com*/
 *            whether nulls should be serialized
 *
 * @return created gson, never null
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateFormatter());
    builder.registerTypeAdapter(Event.class, new EventFormatter());
    builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls)
        builder.serializeNulls();
    return builder.create();
}