Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

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

License:Open Source License

@Override
public TeamStats 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 (MatchesKey.equals(key)) {
            matches = new ExternalRef();
            matches.deserialize(value, typeOfT, context);
        } else if (TeamsUrlKey.equals(key))
            teamsUrl = value.getAsString();
        else if (NameKey.equals(key))
            name = value.getAsString();/*from  w  w  w .  j ava2s  .c om*/
        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.TournamentHrefWrapper.java

License:Open Source License

@Override
public TournamentHrefWrapper 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 (HrefKey.equals(key))
            tournamentUrl = value.getAsString();
        else if (NameKey.equals(key))
            name = value.getAsString();/* w w  w .ja v a 2s.  com*/
        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 ww .j  a v  a 2 s  .co  m
                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.ccreanga.bitbucket.rest.client.http.responseparsers.ParserUtil.java

License:Apache License

public static String optionalJsonString(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return null;
    return element.getAsString();
}

From source file:com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks.java

License:Apache License

/**
 * Parse list of Error's from output./*from   ww w .java 2  s.  c  om*/
 *
 * @param output json containing errors hash
 * @return List of Error's or empty list if none could be found
 */
public static List<Error> getErrors(String output) {
    JsonElement element = parser.parse(output);
    JsonObject object = element.getAsJsonObject();
    JsonArray errorsArray = object.get("errors").getAsJsonArray();

    List<Error> errors = Lists.newArrayList();
    Iterator<JsonElement> it = errorsArray.iterator();
    while (it.hasNext()) {
        JsonObject obj = it.next().getAsJsonObject();
        JsonElement context = obj.get("context");
        JsonElement message = obj.get("message");
        JsonElement exceptionName = obj.get("exceptionName");
        Error error = Error.create(!context.isJsonNull() ? context.getAsString() : null,
                !message.isJsonNull() ? message.getAsString() : null,
                !exceptionName.isJsonNull() ? exceptionName.getAsString() : null);
        errors.add(error);
    }

    return errors;
}

From source file:com.chatwing.whitelabel.managers.ApiManagerImpl.java

License:Apache License

protected String appendParams(Object params) {
    StringBuilder builder = new StringBuilder();
    builder.append("?");
    Gson gson = new Gson();
    JsonObject gsonObject = gson.toJsonTree(params).getAsJsonObject();
    for (Map.Entry<String, JsonElement> entry : gsonObject.entrySet()) {
        JsonElement value = entry.getValue();
        if (value != null && value.isJsonPrimitive()) {
            try {
                builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                builder.append("=");
                builder.append(URLEncoder.encode(value.getAsString(), "UTF-8"));
                builder.append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();/*from   www. j  a v  a  2s .co m*/
            }

        }
    }
    return builder.toString();
}

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

License:Apache License

/**
 * {@inheritDoc}/*w  w w .  j  a  v  a2 s .  c  o  m*/
 */
@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.getAsString();
    }

    Map<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.citrus.sdk.payment.MerchantPaymentOption.java

License:Apache License

public static MerchantPaymentOption getMerchantPaymentOptions(JsonObject merchantPaymentOptionsObj,
        Map<String, PGHealth> pgHealthMap) {
    MerchantPaymentOption merchantPaymentOption;
    Set<CardScheme> debitCardSchemeSet = null;
    Set<CardScheme> creditCardSchemeSet = null;
    ArrayList<NetbankingOption> netbankingOptionList = null;

    JsonArray bankArray = merchantPaymentOptionsObj.getAsJsonArray("netBanking");
    JsonArray creditCardArray = merchantPaymentOptionsObj.getAsJsonArray("creditCard");
    JsonArray debitCardArray = merchantPaymentOptionsObj.getAsJsonArray("debitCard");

    int size = -1;
    // Parse credit card scheme
    size = creditCardArray.size();//  ww  w . j av  a 2  s . c o m
    for (int i = 0; i < size; i++) {
        JsonElement element = creditCardArray.get(i);
        String cardScheme = element.getAsString();

        if (creditCardSchemeSet == null) {
            creditCardSchemeSet = new HashSet<>();
        }

        creditCardSchemeSet.add(getCardScheme(cardScheme));
    }

    // Parse debit card scheme
    size = debitCardArray.size();
    for (int i = 0; i < size; i++) {
        JsonElement element = debitCardArray.get(i);
        String cardScheme = element.getAsString();

        if (debitCardSchemeSet == null) {
            debitCardSchemeSet = new HashSet<>();
        }

        debitCardSchemeSet.add(getCardScheme(cardScheme));
    }

    // Parse netbanking options
    size = bankArray.size();
    for (int i = 0; i < size; i++) {
        JsonElement element = bankArray.get(i);
        if (element.isJsonObject()) {
            JsonObject bankOption = element.getAsJsonObject();

            element = bankOption.get("bankName");
            String bankName = element.getAsString();

            element = bankOption.get("issuerCode");
            String issuerCode = element.getAsString();

            if (!TextUtils.isEmpty(bankName) && !TextUtils.isEmpty(issuerCode)) {
                NetbankingOption netbankingOption = new NetbankingOption(bankName, issuerCode);

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

                if (netbankingOptionList == null) {
                    netbankingOptionList = new ArrayList<>();
                }

                netbankingOptionList.add(netbankingOption);
            }
        }
    }

    merchantPaymentOption = new MerchantPaymentOption(creditCardSchemeSet, debitCardSchemeSet,
            netbankingOptionList);

    return merchantPaymentOption;
}

From source file:com.claresco.tinman.json.JsonUtility.java

License:Open Source License

/**
 * Description:/*from  w  w  w .j  av  a  2s  .  com*/
 *      Convert JsonElement to JsonObject, i.e. Mapping
 * Params:
 *
 */
protected static JsonObject convertJsonElementToJsonObject(JsonElement element) {
    if (element == null) {
        throw new XapiBadJsonObjectException("element is null");
    }

    if (isNotJsonObject(element)) {
        throw new XapiBadJsonObjectException(element.getAsString() + " is not an object");
    }
    return element.getAsJsonObject();
}

From source file:com.claresco.tinman.json.JsonUtility.java

License:Open Source License

/**
 * Description://  ww  w  .  jav  a 2 s.  co  m
 *      Check if the element equals certain string
 *
 * Params:
 *
 */
protected static Boolean elementEqualString(JsonElement theElement, String theString) {
    return theElement.getAsString().equals(theString);
}