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.microsoft.windowsazure.mobileservices.table.serialization.JsonEntityParser.java

License:Open Source License

/**
 * Changes returned JSon object's id property name to match with type's id
 * property name.//w  w w .j  a v  a2  s  . c  o m
 *
 * @param element
 * @param propertyName
 */
private static void changeIdPropertyName(JsonObject element, String propertyName) {
    // If the property name is id or if there's no id defined, then return
    // without performing changes
    if (propertyName.equals("id") || propertyName.length() == 0)
        return;

    if (element.has("id")) {

        JsonElement idElement = element.get("id");

        String value = idElement.isJsonNull() ? null : idElement.getAsString();
        element.remove("id");

        // Create a new id property using the given property name
        element.addProperty(propertyName, value);
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceSyncContext.java

License:Open Source License

private void processElements(String tableName, JsonArray elements) throws Throwable {
    if (elements != null) {

        List<JsonObject> updatedJsonObjects = new ArrayList<JsonObject>();
        List<String> deletedIds = new ArrayList<String>();

        for (JsonElement element : elements) {

            JsonObject jsonObject = element.getAsJsonObject();
            JsonElement elementId = jsonObject.get(MobileServiceSystemColumns.Id);

            if (elementId == null) {
                continue;
            }/* w  ww  . j  a v  a 2 s  .  c  o  m*/

            if (isDeleted(jsonObject)) {
                deletedIds.add(elementId.getAsString());
            } else {
                updatedJsonObjects.add(jsonObject);
            }
        }

        if (deletedIds.size() > 0) {
            this.mStore.delete(tableName, deletedIds.toArray(new String[deletedIds.size()]));
        }

        if (updatedJsonObjects.size() > 0) {
            this.mStore.upsert(tableName, updatedJsonObjects.toArray(new JsonObject[updatedJsonObjects.size()]),
                    true);
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceSyncTable.java

License:Open Source License

/**
 * Insert an item into the local table and enqueue the operation to be
 * synchronized on context push./*w w  w.  j  a  v a2  s.  co  m*/
 *
 * @param item the item to be inserted
 * @return A ListenableFuture that is done when the item has been inserted,
 * returning a copy of the inserted item including id.
 */
public ListenableFuture<E> insert(E item) {
    final SettableFuture<E> future = SettableFuture.create();

    final JsonObject json = mClient.getGsonBuilder().create().toJsonTree(item).getAsJsonObject();

    JsonElement idJsonObject = json.get("id");

    if (idJsonObject != null && !idJsonObject.isJsonNull()) {
        String itemId = idJsonObject.getAsString();

        ListenableFuture<JsonObject> lookUpInternalFuture = mInternalTable.lookUp(itemId);

        Futures.addCallback(lookUpInternalFuture, new FutureCallback<JsonObject>() {
            @Override
            public void onFailure(Throwable throwable) {
                future.setException(throwable);
            }

            @Override
            public void onSuccess(JsonObject result) {

                if (result != null) {
                    future.set(parseResults(result).get(0));
                    return;
                }

                insertInternal(json, future);
            }
        });
    } else {
        insertInternal(json, future);
    }

    return future;
}

From source file:com.microsoftopentechnologies.auth.utils.JsonUtils.java

License:Apache License

public static String getJsonStringProp(JsonObject obj, String propName) {
    JsonElement element = obj.get(propName);
    if (element != null) {
        return element.getAsString();
    }/* w w  w.  j a  v a2s .  co m*/

    return "";
}

From source file:com.microsoftopentechnologies.auth.utils.JsonUtils.java

License:Apache License

public static String getAsString(JsonElement element) {
    if (element != null) {
        return element.getAsString();
    }/*  ww  w .ja  v a  2 s  . c  om*/

    return "";
}

From source file:com.microsoftopentechnologies.intellij.helpers.aadauth.AuthenticationContext.java

License:Apache License

private String getJsonStringProp(JsonObject obj, String propName) {
    JsonElement element = obj.get(propName);
    if (element != null) {
        return element.getAsString();
    }//from w ww.ja  v a2s .  c o  m

    return "";
}

From source file:com.money.manager.ex.investment.prices.FixerService.java

License:Open Source License

private SecurityPriceModel getSecurityPriceFor(Map.Entry<String, JsonElement> quote, String dateString) {
    SecurityPriceModel priceModel = new SecurityPriceModel();
    priceModel.symbol = quote.getKey();/* w w  w  . j  a v  a  2  s.co  m*/

    UIHelper ui = new UIHelper(getContext());

    // Price

    JsonElement priceElement = quote.getValue();
    if (priceElement == JsonNull.INSTANCE) {
        ui.showToast(
                getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol);
        return null;
    }
    String priceString = priceElement.getAsString();
    if (!NumericHelper.isNumeric(priceString)) {
        ui.showToast(
                getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol);
        return null;
    }

    Money retrievedPrice = MoneyFactory.fromString(priceString);
    // invert the price. Round to 10 decimals.
    BigDecimal invertedPrice = BigDecimal.ONE.divide(retrievedPrice.toBigDecimal(), 10, RoundingMode.HALF_EVEN);
    priceModel.price = MoneyFactory.fromBigDecimal(invertedPrice);

    // Date

    Date date = new MmxDate(dateString).toDate();
    priceModel.date = date;

    return priceModel;
}

From source file:com.money.manager.ex.investment.yql.YqlSecurityPriceUpdaterRetrofit.java

License:Open Source License

private SecurityPriceModel getSecurityPriceFor(JsonObject quote) {
    SecurityPriceModel priceModel = new SecurityPriceModel();
    priceModel.symbol = quote.get("symbol").getAsString();

    ExceptionHandler handler = new ExceptionHandler(getContext(), this);

    // Price/*  w ww.  j a v  a 2 s . c o  m*/

    JsonElement priceElement = quote.get("LastTradePriceOnly");
    if (priceElement == JsonNull.INSTANCE) {
        handler.showMessage(
                getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol);
        return null;
    }
    String priceString = priceElement.getAsString();
    if (!NumericHelper.isNumeric(priceString)) {
        handler.showMessage(
                getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol);
        return null;
    }

    priceModel.price = readPrice(priceString, quote);

    // Date

    Date date = new MmxDate().toDate();
    JsonElement dateElement = quote.get("LastTradeDate");
    if (dateElement != JsonNull.INSTANCE) {
        // Sometimes the date is not available. For now we will use today's date.
        date = new MmxDate(dateElement.getAsString(), "MM/dd/yyyy").toDate();
    }
    priceModel.date = date;

    return priceModel;
}

From source file:com.money.manager.ex.investment.yql.YqlSecurityPriceUpdaterRetrofit.java

License:Open Source License

private Money readPrice(String priceString, JsonObject quote) {
    UIHelper ui = new UIHelper(getContext());
    Money price = MoneyFactory.fromString(priceString);

    /**/*w  w w . j  a  va 2 s . c om*/
     LSE stocks are expressed in GBp (pence), not Pounds.
     From stockspanel.cpp, line 785: if (StockQuoteCurrency == "GBp") dPrice = dPrice / 100;
     */
    JsonElement currencyElement = quote.get("Currency");

    // validation
    if (currencyElement == null || currencyElement.isJsonNull()) {
        ui.showToast(R.string.error_downloading_symbol);
        return MoneyFactory.fromDouble(0);
    }

    String currency;
    try {
        currency = currencyElement.getAsString();
    } catch (UnsupportedOperationException ex) {
        Timber.e(ex, "reading currency from downloaded price");
        currency = "";
    }
    if (currency.equals("GBp")) {
        price = price.divide(100, MoneyFactory.MAX_ALLOWED_PRECISION);
    }
    return price;
}