Java Code Examples for com.google.gson.GsonBuilder#excludeFieldsWithModifiers()

The following examples show how to use com.google.gson.GsonBuilder#excludeFieldsWithModifiers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GsonUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 创建过滤指定修饰符字段 GsonBuilder
 * @param builder   {@link GsonBuilder}
 * @param modifiers 需过滤不处理的字段修饰符 {@link Modifier}
 * @return {@link GsonBuilder}
 */
public static GsonBuilder createGsonExcludeFields(final GsonBuilder builder, final int... modifiers) {
    if (builder != null) {
        return builder.excludeFieldsWithModifiers(modifiers);
    }
    return null;
}
 
Example 2
Source File: HttpUtils.java    From qvod with MIT License 5 votes vote down vote up
private Gson getGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingStrategy(new AnnotateNaming());
        gsonBuilder.serializeNulls();
        gsonBuilder.excludeFieldsWithModifiers(Modifier.TRANSIENT);
        gson = gsonBuilder.create();
    }
    return gson;
}
 
Example 3
Source File: MobileServiceClient.java    From azure-mobile-apps-android-client with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a GsonBuilder with custom serializers to use with Microsoft Azure
 * Mobile Services
 *
 * @return
 */
private static GsonBuilder createMobileServiceGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    // Register custom date serializer/deserializer
    gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
    LongSerializer longSerializer = new LongSerializer();
    gsonBuilder.registerTypeAdapter(Long.class, longSerializer);
    gsonBuilder.registerTypeAdapter(long.class, longSerializer);

    gsonBuilder.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC);
    gsonBuilder.serializeNulls(); // by default, add null serialization

    return gsonBuilder;
}