Example usage for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

List of usage examples for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

Introduction

In this page you can find the example usage for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES.

Prototype

FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

To view the source code for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES.

Click Source Link

Document

Using this naming policy with Gson will modify the Java Field name from its camel cased form to a lower case field name where each word is separated by an underscore (_).

Usage

From source file:com.sangupta.twittercli.command.Timeline.java

License:Apache License

@Override
public void doCommand() {
    WebRequest request = WebInvoker.getWebRequest("https://api.twitter.com/1.1/statuses/home_timeline.json",
            WebRequestMethod.GET);// www.j a va 2 s  . c  om
    user.signRequest(request);
    WebResponse response = WebInvoker.executeSilently(request);
    TwitterStatusUpdate[] updates = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .fromJson(response.getContent(), TwitterStatusUpdate[].class);

    if (AssertUtils.isEmpty(updates)) {
        System.out.println("No tweets found");
        return;
    }

    System.out.println();
    for (TwitterStatusUpdate update : updates) {
        System.out.println("  @" + update.user.getScreenName());
        System.out.println("  " + update.text);
        System.out.println("  " + update.createdAt);
        System.out.println();
    }
}

From source file:com.sangupta.twittercli.command.Unfollow.java

License:Apache License

@Override
public void doCommand() {
    for (String user : users) {
        WebRequest request = WebInvoker.getWebRequest(
                "https://api.twitter.com/1.1/friendships/destroy.json?screen_name=" + getScreenName(user),
                WebRequestMethod.POST);/*from  w w  w . j a  v a2  s.  com*/
        super.user.signRequest(request);
        WebResponse response = WebInvoker.executeSilently(request);
        if (response == null || !response.isSuccess()) {
            System.out.println("Unable to unfollow user: " + user);
        }

        TwitterUserProfile profile = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .fromJson(response.getContent(), TwitterUserProfile.class);
        System.out.println("Successfully unfollowed user: " + profile.getName() + " (" + user + ")");
    }
}

From source file:com.sangupta.twittercli.command.Update.java

License:Apache License

@Override
public void doCommand() {
    if (AssertUtils.isEmpty(messages)) {
        System.out.println("No message to update as status... exiting!");
        return;// w ww  . j  a  va  2s.c om
    }

    StringBuilder builder = new StringBuilder(1024);
    for (int index = 0; index < messages.size(); index++) {
        if (index > 0) {
            builder.append(' ');
        }
        builder.append(messages.get(index));
    }
    String message = builder.toString();

    WebRequest request = WebInvoker.getWebRequest(
            "https://api.twitter.com/1.1/statuses/update.json?status=" + UriUtils.encodeURIComponent(message),
            WebRequestMethod.POST);
    user.signRequest(request);
    WebResponse response = WebInvoker.executeSilently(request);

    TwitterStatusUpdate update = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .fromJson(response.getContent(), TwitterStatusUpdate.class);
    System.out.println(
            "Tweet successfully posted with ID " + update.idStr + " by @" + update.user.getScreenName());
}

From source file:com.sangupta.twittercli.command.Users.java

License:Apache License

@Override
public void doCommand() {
    if (AssertUtils.isEmpty(users)) {
        System.out.println("Atleast one user must be specified... exiting!");
        return;//from w  ww  . ja v a2  s  . co  m
    }

    String userStr = "";
    if (users.size() > 1) {
        for (int index = 0; index < users.size(); index++) {
            if (index > 0) {
                userStr += ",";
            }
            userStr += users.get(index).trim();
        }
    } else {
        userStr = users.get(0);
    }

    WebRequest request = WebInvoker.getWebRequest(
            "https://api.twitter.com/1.1/users/lookup.json?screen_name=" + userStr, WebRequestMethod.GET);
    user.signRequest(request);
    WebResponse response = WebInvoker.executeSilently(request);
    TwitterUserProfile[] profiles = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .fromJson(response.getContent(), TwitterUserProfile[].class);

    for (TwitterUserProfile profile : profiles) {
        System.out.println("ID: " + profile.getId());
        System.out.println("Since: " + profile.getCreatedAt());
        System.out.println("Screen Name: " + profile.getScreenName());
        System.out.println("Name: " + profile.getName());
        System.out.println("Followers: " + profile.getFollowersCount());
        System.out.println("Following: " + profile.getFriendsCount());
        System.out.println("Listed: " + profile.getListedCount());
        System.out.println("Favorites: " + profile.getFavouritesCount());
        System.out.println("Tweets: " + profile.getStatusesCount());
        System.out.println("Email: " + profile.getEmail());
        System.out.println("Bio: " + profile.getDescription());
        System.out.println("Location: " + profile.getLocation());
        System.out.println("URL: " + profile.getProfileLink());
        System.out.println();
    }
}

From source file:com.sangupta.twittercli.command.WhoIs.java

License:Apache License

@Override
public void doCommand() {
    if (AssertUtils.isEmpty(users)) {
        System.out.println("Atleast one user must be specified... exiting!");
        return;/*w w  w .  j a  v  a 2 s.  co m*/
    }

    if (users.size() > 1) {
        System.out.println("Currently only one user is supported");
        return;
    }

    WebRequest request = WebInvoker.getWebRequest(
            "https://api.twitter.com/1.1/users/show.json?screen_name=" + users.get(0), WebRequestMethod.GET);
    user.signRequest(request);
    WebResponse response = WebInvoker.executeSilently(request);
    TwitterUserProfile profile = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .fromJson(response.getContent(), TwitterUserProfile.class);

    System.out.println("ID: " + profile.getId());
    System.out.println("Since: " + profile.getCreatedAt());
    System.out.println("Screen Name: " + profile.getScreenName());
    System.out.println("Name: " + profile.getName());
    System.out.println("Followers: " + profile.getFollowersCount());
    System.out.println("Following: " + profile.getFriendsCount());
    System.out.println("Listed: " + profile.getListedCount());
    System.out.println("Favorites: " + profile.getFavouritesCount());
    System.out.println("Tweets: " + profile.getStatusesCount());
    System.out.println("Email: " + profile.getEmail());
    System.out.println("Bio: " + profile.getDescription());
    System.out.println("Location: " + profile.getLocation());
    System.out.println("URL: " + profile.getProfileLink());
}

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

License:Apache License

/**
 * Constructs a new factory./*from  w w  w.j av  a 2s. c  o  m*/
 *
 * @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.secretparty.app.SecretPartyApplication.java

License:Open Source License

@Override
public void onCreate() {
    super.onCreate();
    this.thematicRepository = new ThematicRepository(this);
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(Party.class, new PartyDeserializer(thematicRepository)).create();

    RestAdapter restAdapter = new RestAdapter.Builder().setConverter(new GsonConverter(gson))
            .setServer(getString(R.string.server)).build();

    apiService = restAdapter.create(APIService.class);
}

From source file:com.sociablue.nanodegree_p1.managers.NetworkManager.java

License:Apache License

public NetworkManager() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(Date.class, new DateTypeAdapter()).create();

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(MdbConstants.BASE_URL)
            .setConverter(new GsonConverter(gson)).build();

    MdbService = restAdapter.create(MDB.class);

}

From source file:com.strategicgains.restexpress.serialization.json.DefaultJsonProcessor.java

License:Apache License

public DefaultJsonProcessor() {
    super();/* w  w  w.  ja  v a2s. co m*/
    gson = new GsonBuilder().disableHtmlEscaping()
            .registerTypeAdapter(Date.class, new GsonTimestampSerializer())
            .setDateFormat(DateAdapterConstants.TIMESTAMP_OUTPUT_FORMAT)
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
}

From source file:com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtensionConverterV1.java

License:Apache License

@Override
public Collection<AgentMetadata> deleteAndDisableAgentRequestBody(String requestBody) {
    Type AGENT_METADATA_LIST_TYPE = new TypeToken<ArrayList<AgentMetadata>>() {
    }.getType();//from www. j  ava2s . c o  m
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    return gson.fromJson(requestBody, AGENT_METADATA_LIST_TYPE);
}