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.expensify.testframework.SubmitResultContainer.java

License:Microsoft Public License

public static String RawStringify(ResultBase result) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();//from   w w  w.j a v  a  2  s  .  co  m
    gsonBuilder.serializeNulls();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    gsonBuilder.disableHtmlEscaping();
    gsonBuilder.registerTypeHierarchyAdapter(ResultBase.class, new ResultSerializer());
    String resultJson = gson.toJson(result);
    resultJson = hackResultJsonOrder(resultJson);
    return resultJson;
}

From source file:com.expensify.testframework.utils.CommandDeserializer.java

License:Microsoft Public License

public CommandDeserializer(String commandElementName) {
    this.commandElementName = commandElementName;
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    gson = gsonBuilder.create();//from w  w w . j  a v  a  2s.  c o  m
    commandRegistry = new HashMap<String, Class<? extends CommandBase>>();
}

From source file:com.github.api.v2.services.impl.BaseGitHubService.java

License:Apache License

/**
 * Gets the gson builder.//from www  .  ja  va2 s . c  o m
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(ApplicationConstants.DATE_FORMAT);
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override
        public String translateName(Field field) {
            if (field.getType().equals(Repository.Visibility.class)) {
                return "private";
            } else if (field.getType().equals(Gist.Visibility.class)) {
                return "public";
            } else {
                return field.getName();
            }
        }

    });
    builder.registerTypeAdapter(Issue.State.class, new JsonDeserializer<Issue.State>() {
        @Override
        public Issue.State deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Issue.State.fromValue(arg0.getAsString());
        }
    });
    builder.registerTypeAdapter(Repository.Visibility.class, new JsonDeserializer<Repository.Visibility>() {
        @Override
        public Repository.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return (arg0.getAsBoolean()) ? Repository.Visibility.PRIVATE : Repository.Visibility.PUBLIC;
        }
    });
    builder.registerTypeAdapter(Gist.Visibility.class, new JsonDeserializer<Gist.Visibility>() {
        @Override
        public Gist.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return (arg0.getAsBoolean()) ? Gist.Visibility.PUBLIC : Gist.Visibility.PRIVATE;
        }
    });
    builder.registerTypeAdapter(Language.class, new JsonDeserializer<Language>() {
        @Override
        public Language deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Language.fromValue(arg0.getAsString());
        }
    });
    builder.registerTypeAdapter(Tree.Type.class, new JsonDeserializer<Tree.Type>() {
        @Override
        public Tree.Type deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Tree.Type.fromValue(arg0.getAsString());
        }
    });
    return builder;
}

From source file:com.google.code.bing.search.client.impl.BingSearchJsonClientImpl.java

License:Apache License

/**
 * Gets the gson builder./*  w  ww  . j  a va  2  s  .co m*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(ApplicationConstants.DATE_FORMAT);
    builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    //      builder.registerTypeAdapter(Issue.State.class, new JsonDeserializer<Issue.State>() {
    //         @Override
    //         public Issue.State deserialize(JsonElement arg0, Type arg1,
    //               JsonDeserializationContext arg2) throws JsonParseException {
    //            return Issue.State.fromValue(arg0.getAsString());
    //         }
    //      });
    return builder;
}

From source file:com.google.code.stackexchange.client.impl.StackExchangeApiJsonClient.java

License:Apache License

protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override/* ww w  .j a  v  a 2 s  .  c om*/
        public Date deserialize(JsonElement source, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return new Date(source.getAsLong() * 1000);
        }

    });
    builder.registerTypeAdapter(BadgeRank.class, new JsonDeserializer<BadgeRank>() {

        @Override
        public BadgeRank deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return BadgeRank.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(PostType.class, new JsonDeserializer<PostType>() {

        @Override
        public PostType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return PostType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(PostTimelineType.class, new JsonDeserializer<PostTimelineType>() {

        @Override
        public PostTimelineType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return PostTimelineType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(UserTimelineType.class, new JsonDeserializer<UserTimelineType>() {

        @Override
        public UserTimelineType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return UserTimelineType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(UserType.class, new JsonDeserializer<UserType>() {

        @Override
        public UserType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return UserType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(RevisionType.class, new JsonDeserializer<RevisionType>() {

        @Override
        public RevisionType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return RevisionType.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(TagRestriction.class, new JsonDeserializer<TagRestriction>() {

        @Override
        public TagRestriction deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return TagRestriction.fromValue(arg0.getAsString());
        }

    });
    builder.registerTypeAdapter(SiteState.class, new JsonDeserializer<SiteState>() {

        @Override
        public SiteState deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return SiteState.fromValue(arg0.getAsString());
        }
    });

    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    return builder;
}

From source file:com.googleapis.maps.services.impl.BaseGoogleMapsApiQuery.java

License:Apache License

/**
 * Gets the gson builder.//from  w w  w  .  j a v a 2  s  .  c om
 * 
 * @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.hackerati.android.user_sdk.gson.HGsonBuilder.java

License:Open Source License

public static final Gson create() {
    final GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
}

From source file:com.hacksurfer.api.client.RunIndustryRiskScoreExample.java

License:Open Source License

public static void main(String args[]) throws Exception {

    LOG.info("Running example HTTP GET against HackSurfer Industry Risk Scores /yesterday resource.");

    SimpleRestClient client = new SimpleRestClient();
    String response = client.httpGet(API_BASE_URL, IRS_YESTERDAY_RESOURCE_PATH);

    // print response
    LOG.info("Response from /yesterday resource : {}", response);

    // do something useful with it
    GsonBuilder gb = new GsonBuilder();
    gb.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    Gson gson = gb.create();//  w  w w.j  a v  a  2  s  .  c o m

    List<IndustryRiskScore> scores = gson.fromJson(response, new TypeToken<List<IndustryRiskScore>>() {
    }.getType());
    LOG.info("Found {} industry risk scores from yesterday.", scores.size());
    for (IndustryRiskScore irs : scores) {
        StringBuilder sb = new StringBuilder();
        sb.append(System.lineSeparator());
        sb.append("Industry : ");
        sb.append(irs.getIndustryDescription());
        sb.append(System.lineSeparator());
        sb.append("  id : ");
        sb.append(irs.getIndustryId());
        sb.append(System.lineSeparator());
        sb.append("  risk : ");
        sb.append(irs.getIndustryGravity());
        sb.append(System.lineSeparator());
        sb.append("  gravity : ");
        sb.append(irs.getIndustryRisk());
        sb.append(System.lineSeparator());
        sb.append("  momentum : ");
        sb.append(irs.getIndustryMomentum());
        sb.append(System.lineSeparator());

        System.out.println(sb.toString());
    }

    LOG.info("Well that was fun!");

}

From source file:com.it.xushuai.baseapp.core.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls//from   ww  w  .jav a  2 s . c  o m
 *            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();
}

From source file:com.kodokux.github.api.GithubApiUtil.java

License:Apache License

private static Gson initGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
}