Example usage for com.google.gson JsonElement toString

List of usage examples for com.google.gson JsonElement toString

Introduction

In this page you can find the example usage for com.google.gson JsonElement toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:com.ccc.crest.core.cache.crest.tournament.Bans.java

License:Open Source License

@Override
public Bans deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (SelfKey.equals(key)) {
            self = new ExternalRef();
            self.deserialize(value, typeOfT, context);
        } else if (RedKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException(
                        "Expected " + RedKey + " array received json element " + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                Ban child = new Ban();
                redTeam.add(child);/*from  w w  w .  j ava  2 s.  c  o  m*/
                child.deserialize(childElement, typeOfT, context);
            }
        } else if (BlueKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException(
                        "Expected " + BlueKey + " array received json element " + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                Ban child = new Ban();
                blueTeam.add(child);
                child.deserialize(childElement, typeOfT, context);
            }
        } else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.tournament.Series.java

License:Open Source License

@Override
public Series deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (SeriesUrlKey.equals(key)) {
            seriesUrl = new ExternalRef();
            seriesUrl.deserialize(value, typeOfT, context);
        } else if (TypeKey.equals(key))
            type = value.getAsString();//from   w  w  w. j av a 2s.  c o m
        else if (NameKey.equals(key))
            name = value.getAsString();
        else if (TeamStatsKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException("Expected " + TeamStatsKey + " array received json element "
                        + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                TeamStats child = new TeamStats();
                teamStats.add(child);
                child.deserialize(childElement, typeOfT, context);
            }
        } else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.tournament.SeriesElement.java

License:Open Source License

@Override
public SeriesElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (TotalCountStrKey.equals(key))
            totalCountStr = value.getAsString();
        else if (ItemsKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException(
                        "Expected " + ItemsKey + " array received json element " + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                TeamInfo child = new TeamInfo();
                teamInfos.add(child);/*from   ww  w.j  av a2  s.  c  o m*/
                child.deserialize(childElement, typeOfT, context);
            }
        } else if (PageCountKey.equals(key))
            pageCount = value.getAsLong();
        else if (PageCountStrKey.equals(key))
            pageCountStr = value.getAsString();
        else if (TotalCountKey.equals(key))
            totalCount = value.getAsLong();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.tournament.Tournaments.java

License:Open Source License

@Override
public Tournaments deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (TotalCountStringKey.equals(key))
            totalCountStr = value.getAsString();
        else if (ItemsKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException(
                        "Expected " + ItemsKey + " array received json element " + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                Tournament child = new Tournament();
                tournaments.add(child);/*from w w  w .  ja va  2s. com*/
                child.deserialize(childElement, typeOfT, context);
            }
        } else if (PageCountKey.equals(key))
            pageCount = value.getAsLong();
        else if (PageCountStringKey.equals(key))
            pageCountStr = value.getAsString();
        else if (TotalCountKey.equals(key))
            totalCount = value.getAsLong();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.chiwanpark.flume.plugins.JSONHandler.java

License:Apache License

/**
 * {@inheritDoc}/*from   w  w w  .j  ava  2 s  .  com*/
 */
@Override
public Event getEvent(String message) throws Exception {
    /*
     * Gson throws Exception if the data is not parseable to JSON.
     * Need not catch it since the source will catch it and return error.
     */
    JsonObject json = parser.parse(message).getAsJsonObject();

    String body = "";
    JsonElement bodyElm = json.get("body");
    if (bodyElm != null) {
        body = bodyElm.toString();
    }

    HashMap<String, String> headers = null;
    if (json.has("headers")) {
        headers = new HashMap<String, String>();
        for (Map.Entry<String, JsonElement> header : json.get("headers").getAsJsonObject().entrySet()) {
            if (header.getValue().isJsonPrimitive()) {
                headers.put(header.getKey(), header.getValue().getAsString());
            } else {
                // If a header is not a json primitive (it contains subfields),
                // we have to use toString() to get it as valid json.
                headers.put(header.getKey(), header.getValue().toString());
            }
        }
    }

    return EventBuilder.withBody(body.getBytes(charset), headers);
}

From source file:com.cinchapi.concourse.server.http.JsonEndpoint.java

License:Apache License

@Override
public final String serve(HttpRequest request, HttpResponse response, AccessToken creds,
        TransactionToken transaction, String environment) throws Exception {
    Object payload = serve(request, creds, transaction, environment, response);
    JsonElement encoded;
    if (payload == null) {
        encoded = NO_DATA;/*from w  w  w.j a v  a  2  s . co m*/
    } else if (payload instanceof JsonElement) {
        encoded = (JsonElement) payload;
    } else {
        encoded = encoder.toJsonTree(payload);
    }
    return encoded.toString();
}

From source file:com.citrus.sdk.CitrusClient.java

License:Apache License

private void fetchPGHealthForAllBanks() {

    citrusBaseUrlClient.getPGHealthForAllBanks(vanity, "ALLBANKS", new retrofit.Callback<JsonElement>() {
        @Override//  w ww .  j av a  2 s  .c  om
        public void success(JsonElement jsonElement, Response response) {
            try {
                JSONObject jsonObject = new JSONObject(jsonElement.toString());
                Iterator<String> keys = jsonObject.keys();
                while (keys.hasNext()) {
                    if (pgHealthMap == null) {
                        pgHealthMap = new HashMap<String, PGHealth>();
                    }
                    String key = keys.next();
                    String health = jsonObject.optString(key);

                    pgHealthMap.put(key, PGHealth.getPGHealth(health));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failure(RetrofitError error) {
            // Logger.e("Error while fetching the health");
        }
    });

}

From source file:com.citrus.sdk.CitrusClient.java

License:Apache License

public synchronized void getMemberInfo(final String emailId, final String mobileNo,
        final Callback<MemberInfo> callback) {
    if (validate()) {
        retrofitClient.getSignUpToken(signupId, signupSecret, OAuth2GrantType.implicit.toString(),
                new retrofit.Callback<AccessToken>() {
                    @Override/*  w w  w. j a  v a2  s .c  om*/
                    public void success(AccessToken accessToken, Response response) {
                        if (accessToken != null && accessToken.getHeaderAccessToken() != null) {
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("email", emailId);
                                jsonObject.put("mobile", mobileNo);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            retrofitClient.getMemberInfo(accessToken.getHeaderAccessToken(),
                                    new TypedString(jsonObject.toString()),
                                    new retrofit.Callback<JsonElement>() {
                                        @Override
                                        public void success(JsonElement jsonElement, Response response) {
                                            MemberInfo memberInfo = MemberInfo.fromJSON(jsonElement.toString());

                                            if (memberInfo != null) {
                                                sendResponse(callback, memberInfo);
                                            } else {
                                                sendError(callback,
                                                        new CitrusError(
                                                                ResponseMessages.ERROR_MESSAGE_MEMBER_INFO,
                                                                Status.FAILED));
                                            }
                                        }

                                        @Override
                                        public void failure(RetrofitError error) {
                                            sendError(callback, error);
                                        }
                                    });
                        } else {
                            sendError(callback, new CitrusError(ResponseMessages.ERROR_MESSAGE_SIGNUP_TOKEN,
                                    Status.FAILED));
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        sendError(callback, error);
                    }
                });
    }
}

From source file:com.citrus.sdk.CitrusClient.java

License:Apache License

/**
 * Get the user saved payment options.// w  w w  .j a v  a 2 s  .c o  m
 *
 * @param callback - callback
 */
public synchronized void getWallet(final Callback<List<PaymentOption>> callback) {
    /*
     * Get the saved payment options of the user.
     */
    if (validate()) {

        oauthToken.getSignInToken(new Callback<AccessToken>() {
            @Override
            public void success(AccessToken accessToken) {

                retrofitClient.getWallet(accessToken.getHeaderAccessToken(),
                        new retrofit.Callback<JsonElement>() {
                            @Override
                            public void success(JsonElement element, Response response) {
                                if (element != null) {
                                    ArrayList<PaymentOption> walletList = new ArrayList<>();
                                    try {

                                        JSONObject jsonObject = new JSONObject(element.toString());
                                        JSONArray paymentOptions = jsonObject.optJSONArray("paymentOptions");

                                        if (paymentOptions != null) {
                                            for (int i = 0; i < paymentOptions.length(); i++) {
                                                PaymentOption option = PaymentOption
                                                        .fromJSONObject(paymentOptions.getJSONObject(i));

                                                // Check whether the merchant supports the user's payment option and then only add this payment option.
                                                if (merchantPaymentOption != null) {
                                                    Set<CardOption.CardScheme> creditCardSchemeSet = merchantPaymentOption
                                                            .getCreditCardSchemeSet();
                                                    Set<CardOption.CardScheme> debitCardSchemeSet = merchantPaymentOption
                                                            .getDebitCardSchemeSet();
                                                    List<NetbankingOption> netbankingOptionList = merchantPaymentOption
                                                            .getNetbankingOptionList();

                                                    if (option instanceof CreditCardOption
                                                            && creditCardSchemeSet != null
                                                            && creditCardSchemeSet
                                                                    .contains(((CreditCardOption) option)
                                                                            .getCardScheme())) {
                                                        walletList.add(option);
                                                    } else if (option instanceof DebitCardOption
                                                            && debitCardSchemeSet != null
                                                            && debitCardSchemeSet
                                                                    .contains(((DebitCardOption) option)
                                                                            .getCardScheme())) {
                                                        walletList.add(option);
                                                    } else if (option instanceof NetbankingOption
                                                            && netbankingOptionList != null
                                                            && netbankingOptionList.contains(option)) {
                                                        NetbankingOption netbankingOption = (NetbankingOption) option;

                                                        if (pgHealthMap != null) {
                                                            netbankingOption.setPgHealth(pgHealthMap
                                                                    .get(netbankingOption.getBankCID()));
                                                        }

                                                        walletList.add(netbankingOption);
                                                    }
                                                } else {
                                                    // If the merchant payment options are not found, save all the options.
                                                    walletList.add(option);
                                                }
                                            }
                                        }

                                        sendResponse(callback, walletList);

                                    } catch (JSONException e) {
                                        e.printStackTrace();

                                        sendError(callback, new CitrusError(
                                                ResponseMessages.ERROR_MESSAGE_INVALID_JSON, Status.FAILED));
                                    }
                                } else {
                                    sendError(callback, new CitrusError(
                                            ResponseMessages.ERROR_MESSAGE_INVALID_JSON, Status.FAILED));
                                }
                            }

                            @Override
                            public void failure(RetrofitError error) {
                                sendError(callback, new CitrusError(error.getMessage(), Status.FAILED));
                            }
                        });
            }

            @Override
            public void error(CitrusError error) {
                sendError(callback, error);
            }
        });
    }
}

From source file:com.citrus.sdk.CitrusClient.java

License:Apache License

/**
 * @param callback/*from w  ww .j a  v  a  2  s .  c  o  m*/
 */
public synchronized void getProfileInfo(final Callback<CitrusUser> callback) {
    if (validate()) {
        if (citrusUser == null) {
            getPrepaidToken(new Callback<AccessToken>() {
                @Override
                public void success(AccessToken accessToken) {
                    retrofitClient.getProfileInfo(accessToken.getHeaderAccessToken(),
                            new retrofit.Callback<JsonElement>() {
                                @Override
                                public void success(JsonElement jsonElement, Response response) {
                                    String profileInfo = jsonElement.toString();
                                    citrusUser = CitrusUser.fromJSON(profileInfo);

                                    sendResponse(callback, citrusUser);
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    sendError(callback, error);
                                }
                            });
                }

                @Override
                public void error(CitrusError error) {
                    sendError(callback, error);
                }
            });
        }
    }
}