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.nispok.imgurdroid.services.Imgur.java

License:Apache License

private static RestAdapter getRestAdapter() {

    if (restAdapter == null) {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();//w w w  .  j  a va2  s .  com
        restAdapter = new RestAdapter.Builder().setEndpoint(BuildConfig.IMGUR_API_BASE_URL)
                .setRequestInterceptor(new ImgurRequestInterceptor()).setLogLevel(BuildConfig.RETROFIT_LOGGING)
                .setConverter(new GsonConverter(gson)).build();
    }

    return restAdapter;
}

From source file:com.planyourexchange.rest.service.ServerService.java

License:Open Source License

public ServerService(String serviceUrl, final String userName, final String password,
        SharedPreferences sharedPreferences) {

    final TokenManager tokenManager = new TokenManager(sharedPreferences);

    // -- Initializing gson factory with joda converters and underscores default policy
    GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    Converters.registerLocalDate(builder);
    Converters.registerLocalTime(builder);

    // -- For Token Authentication Purposes
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setAuthenticator(tokenManager);

    RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new GsonConverter(builder.create())).setEndpoint(serviceUrl)
            .setRequestInterceptor(tokenManager).setClient(new OkClient(okHttpClient)).build();

    this.serverApi = restAdapter.create(ServerApi.class);

    // -- Call login to get a new Token only WHEN it is needed
    tokenManager.setTokenAction(new TokenManager.TokenAction() {
        @Override//from www  .  j  a v  a 2 s. c om
        public void newToken() {
            // -- Getting Authentication Token
            serverApi.login(userName, password, tokenManager);
        }
    });
}

From source file:com.popdeem.sdk.core.deserializer.PDBrandDeserializer.java

License:Open Source License

@Override
public PDBrand deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();

    Gson gson = new GsonBuilder().registerTypeAdapter(PDLocation.class, new PDLocation.PDLocationDeserializer())
            .registerTypeAdapter(long.class, new PDLongDeserializer())
            .registerTypeAdapter(int.class, new PDIntDeserializer())
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    return gson.fromJson(jsonObject, PDBrand.class);
}

From source file:com.popdeem.sdk.core.deserializer.PDBrandsDeserializer.java

License:Open Source License

@Override
public ArrayList<PDBrand> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject.has("brands")) {
        Gson gson = new GsonBuilder().registerTypeAdapter(PDBrand.class, new PDBrandDeserializer())
                .registerTypeAdapter(long.class, new PDLongDeserializer())
                .registerTypeAdapter(int.class, new PDIntDeserializer())
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

        JsonArray brandsArray = jsonObject.getAsJsonArray("brands");
        return gson.fromJson(brandsArray, typeOfT);
    }/* w w w .j  ava2  s .c  o m*/
    return new ArrayList<>();
}

From source file:com.popdeem.sdk.core.deserializer.PDFeedDeserializer.java

License:Open Source License

@Override
public PDFeed deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    PDFeed feed = gson.fromJson(json, typeOfT);

    JsonObject feedObject = json.getAsJsonObject();
    // Brand// w ww  .  ja v a 2 s  .co  m
    if (feedObject.has("brand")) {
        JsonObject brandObject = feedObject.getAsJsonObject("brand");
        feed.setBrandName(brandObject.get("name").getAsString());
        feed.setBrandLogoUrlString(brandObject.get("logo").getAsString());
    }

    // Reward
    if (feedObject.has("reward")) {
        JsonObject brandObject = feedObject.getAsJsonObject("reward");
        //            feed.setRewardTypeString(brandObject.get("type").getAsString());
        feed.setDescriptionString(brandObject.get("description").getAsString());
    }

    // Social Account
    if (feedObject.has("social_account")) {
        JsonObject socialObject = feedObject.getAsJsonObject("social_account");
        if (socialObject.has("profile_picture")) {
            feed.setUserProfilePicUrlString(socialObject.get("profile_picture").getAsString());
        }

        if (socialObject.has("user")) {
            JsonObject userObject = socialObject.getAsJsonObject("user");
            feed.setUserId(userObject.get("id").getAsInt());
            if (userObject.has("first_name") && !userObject.get("first_name").isJsonNull()) {
                feed.setUserFirstName(userObject.get("first_name").getAsString());
            }
            if (userObject.has("last_name") && !userObject.get("last_name").isJsonNull()) {
                feed.setUserLastName(userObject.get("last_name").getAsString());
            }
        }
    }

    //        feed.setTimeAgo(feedObject.get("time_ago").getAsString());
    //        feed.setImageUrlString(feedObject.get("picture=").getAsString());
    //        feed.setActionText(feedObject.get("text").getAsString());

    return feed;
}

From source file:com.popdeem.sdk.core.deserializer.PDMessagesDeserializer.java

License:Open Source License

@Override
public ArrayList<PDMessage> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().registerTypeAdapter(long.class, new PDLongDeserializer())
            .registerTypeAdapter(int.class, new PDIntDeserializer())
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject.has("messages")) {
        JsonArray array = jsonObject.getAsJsonArray("messages");
        return gson.fromJson(array, typeOfT);
    }/* w  w w .ja  v a 2 s .c  om*/

    return new ArrayList<>();
}

From source file:com.popdeem.sdk.core.deserializer.PDRewardDeserializer.java

License:Open Source License

@Override
public PDReward deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(PDTweetOptions.class, new PDTweetOptions.PDTweetOptionsJsonDeserializer())
            .registerTypeAdapter(long.class, new PDLongDeserializer())
            .registerTypeAdapter(int.class, new PDIntDeserializer())
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    JsonObject jsonObject = json.getAsJsonObject();
    return gson.fromJson(jsonObject, PDReward.class);
}

From source file:com.popdeem.sdk.core.deserializer.PDRewardsDeserializer.java

License:Open Source License

@Override
public ArrayList<PDReward> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().registerTypeAdapter(PDReward.class, new PDRewardDeserializer())
            .registerTypeAdapter(long.class, new PDLongDeserializer())
            .registerTypeAdapter(int.class, new PDIntDeserializer())
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    JsonObject jsonObject = json.getAsJsonObject();
    JsonArray array = jsonObject.getAsJsonArray("rewards");
    return gson.fromJson(array, typeOfT);
}

From source file:com.popdeem.sdk.core.deserializer.PDSocialAccountFacebookDeserializer.java

License:Open Source License

@Override
public PDUserFacebook deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    PDUserFacebook socialAccountFacebook = gson.fromJson(json, typeOfT);

    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject.has("score")) {
        JsonObject scoreObject = jsonObject.getAsJsonObject("score");

        // Total Score
        if (scoreObject.has("total_score")) {
            JsonObject totalScoreObject = scoreObject.getAsJsonObject("total_score");
            if (totalScoreObject.has("value")) {
                socialAccountFacebook.setTotalScore(totalScoreObject.get("value").getAsString());
            }/*from ww w  .j a  va 2  s  . c om*/
        }

        // influence score
        if (scoreObject.has("influence_score")) {
            JsonObject influenceScoreObject = scoreObject.getAsJsonObject("influence_score");
            if (influenceScoreObject.has("reach_score_value")) {
                socialAccountFacebook
                        .setInfluenceReachScore(influenceScoreObject.get("reach_score_value").getAsString());
            }
            if (influenceScoreObject.has("engagement_score_value")) {
                socialAccountFacebook.setInfluenceEngagementScore(
                        influenceScoreObject.get("engagement_score_value").getAsString());
            }
            if (influenceScoreObject.has("frequency_score_value")) {
                socialAccountFacebook.setInfluenceFrequencyScore(
                        influenceScoreObject.get("frequency_score_value").getAsString());
            }
        }

        // advocacy score
        if (scoreObject.has("advocacy_score")) {
            JsonObject advocacyScoreObject = scoreObject.getAsJsonObject("advocacy_score");
            if (advocacyScoreObject.has("value")) {
                socialAccountFacebook.setAdvocacyScore(advocacyScoreObject.get("value").getAsInt());
            }
        }
    }

    return socialAccountFacebook;
}

From source file:com.popdeem.sdk.core.deserializer.PDSocialMediaFriendDeserializer.java

License:Open Source License

@Override
public PDSocialMediaFriend deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

    JsonObject jsonObject = json.getAsJsonObject();
    PDSocialMediaFriend friend = gson.fromJson(jsonObject, PDSocialMediaFriend.class);
    if (jsonObject.has("picture")) {
        JsonObject pictureObject = jsonObject.getAsJsonObject("picture");
        if (pictureObject.has("data")) {
            JsonObject pictureDataObject = pictureObject.getAsJsonObject("data");
            friend.setImageUrl(pictureDataObject.get("url").getAsString());
        }//from   w  w w. j  a  va  2 s  .  co  m
    }
    return friend;
}