Example usage for com.google.gson JsonElement getAsInt

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

Introduction

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

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:com.heroiclabs.sdk.android.util.json.DatastoreItemPermissionsReadJsonAdapter.java

License:Apache License

@Override
public DatastoreItem.Permissions.Read deserialize(final @NonNull JsonElement json, final @NonNull Type typeOfT,
        final @NonNull JsonDeserializationContext context) throws JsonParseException {
    final int key = json.getAsInt();
    return DatastoreItem.Permissions.Read.fromKey(key);
}

From source file:com.heroiclabs.sdk.android.util.json.DatastoreItemPermissionsWriteJsonAdapter.java

License:Apache License

@Override
public DatastoreItem.Permissions.Write deserialize(final @NonNull JsonElement json, final @NonNull Type typeOfT,
        final @NonNull JsonDeserializationContext context) throws JsonParseException {
    final int key = json.getAsInt();
    return DatastoreItem.Permissions.Write.fromKey(key);
}

From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java

License:Open Source License

static void appendWindowPolicy(String policyName, JsonElement config, String timeUnit, StringBuilder sb) {
    switch (policyName) {
    case POLICY_COUNT:
        sb.append("count(");
        sb.append(config.getAsInt());
        sb.append(")");
        break;//from  w  w  w. j a  v  a 2  s  .  c o m
    case POLICY_DELTA:
        break;
    case POLICY_NONE:
        break;
    case POLICY_PUNCTUATION:
        break;
    case POLICY_TIME: {
        TimeUnit unit = TimeUnit.valueOf(timeUnit.toString());
        long time = config.getAsLong();
        double secs;
        switch (unit) {
        case DAYS:
        case HOURS:
        case MINUTES:
        case SECONDS:
            secs = unit.toSeconds(time);
            break;
        case MILLISECONDS:
            secs = ((double) time) / 1000.0;
            break;

        case MICROSECONDS:
            secs = ((double) time) / 1000_000.0;
            break;

        case NANOSECONDS:
            secs = ((double) time) / 1000_000_000.0;
            break;

        default:
            throw new IllegalStateException();
        }
        sb.append("time(");
        sb.append(secs);
        sb.append(")");
        break;
    }
    default:
        break;

    }
}

From source file:com.ibm.util.merge.json.ProviderDeserializer.java

License:Apache License

@Override
public AbstractProvider deserialize(JsonElement json, Type provider, JsonDeserializationContext context) {
    JsonElement jsonType = json.getAsJsonObject().get("type");
    if (jsonType == null) {
        return null;
    }//from  ww  w. j  a  v  a  2 s.co m
    int myType = jsonType.getAsInt();
    switch (myType) {
    case Providers.TYPE_CSV:
        return context.deserialize(json, ProviderCsv.class);
    case Providers.TYPE_SQL:
        return context.deserialize(json, ProviderSql.class);
    case Providers.TYPE_TAG:
        return context.deserialize(json, ProviderTag.class);
    case Providers.TYPE_HTML:
        return context.deserialize(json, ProviderHtml.class);
    default:
        return null;
    }
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private ArrayList<Integer> getLineNumbers(Collection<String> imports, String tokens) {
    ArrayList<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);/*  w  ww .jav  a2 s  .c o m*/
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();

    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String importName = jObject.getAsJsonPrimitive(IMPORT_NAME).getAsString();
        if (imports.contains(importName)) {
            JsonArray lineNumbersArray = jObject.getAsJsonArray(LINE_NUMBERS);
            for (JsonElement lineNumber : lineNumbersArray) {
                lineNumbers.add(lineNumber.getAsInt());
            }
        }
    }
    return lineNumbers;
}

From source file:com.impetus.client.couchdb.CouchDBClient.java

License:Apache License

/**
 * Sets the aggregated values in result.
 * //  w w  w.j  a  va  2  s  . c om
 * @param results
 *            the results
 * @param interpreter
 *            the interpreter
 * @param array
 *            the array
 */
private void setAggregatedValuesInResult(List results, CouchDBQueryInterpreter interpreter, JsonArray array) {
    for (JsonElement json : array) {
        JsonElement value = json.getAsJsonObject().get("value");
        if (interpreter.getAggregationType().equals(CouchDBConstants.COUNT))
            results.add(value.getAsInt());
        else
            results.add(value.getAsDouble());
    }
}

From source file:com.keydap.sparrow.SparrowClient.java

License:Apache License

private <T> SearchResponse<T> sendSearchRequest(HttpUriRequest req, Class<T> resClas) {
    SearchResponse<T> result = new SearchResponse<T>();
    try {//from  ww w . j  a  v a 2s.c om
        LOG.debug("Sending {} request to {}", req.getMethod(), req.getURI());
        authenticator.addHeaders(req);
        HttpResponse resp = client.execute(req);
        authenticator.saveHeaders(resp);
        StatusLine sl = resp.getStatusLine();
        int code = sl.getStatusCode();

        LOG.debug("Received status code {} from the request to {}", code, req.getURI());

        HttpEntity entity = resp.getEntity();
        String json = null;
        if (entity != null) {
            json = EntityUtils.toString(entity);
        }

        result.setHttpBody(json);
        result.setHttpCode(code);
        result.setHeaders(resp.getAllHeaders());

        // if it is success there will be response body to read
        if (code == 200) {
            if (json != null) { // DELETE will have no response body, so check for null

                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                JsonElement je = obj.get("totalResults");
                if (je != null) {
                    result.setTotalResults(je.getAsInt());
                }

                je = obj.get("startIndex");
                if (je != null) {
                    result.setStartIndex(je.getAsInt());
                }

                je = obj.get("itemsPerPage");
                if (je != null) {
                    result.setItemsPerPage(je.getAsInt());
                }

                je = obj.get("Resources"); // yes, the 'R' in resources must be upper case
                if (je != null) {
                    JsonArray arr = je.getAsJsonArray();
                    Iterator<JsonElement> itr = arr.iterator();
                    List<T> resources = new ArrayList<T>();
                    while (itr.hasNext()) {
                        JsonObject r = (JsonObject) itr.next();
                        if (resClas != null) {
                            resources.add(unmarshal(r, resClas));
                        } else {
                            T rsObj = unmarshal(r);
                            if (rsObj == null) {
                                LOG.warn(
                                        "No resgistered resource class found to deserialize the resource data {}",
                                        r);
                            } else {
                                resources.add(rsObj);
                            }
                        }
                    }

                    if (!resources.isEmpty()) {
                        result.setResources(resources);
                    }
                }
            }
        } else {
            if (json != null) {
                Error error = serializer.fromJson(json, Error.class);
                result.setError(error);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        LOG.warn("", e);
        result.setHttpCode(-1);
        Error err = new Error();

        err.setDetail(e.getMessage());
        result.setError(err);
    }
    return result;
}

From source file:com.luan.thermospy.android.core.pojo.CameraControlActionDeserializer.java

License:Open Source License

@Override
public CameraControlAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    int typeInt = json.getAsInt();
    return CameraControlAction.fromInt(typeInt);
}

From source file:com.mapper.yelp.YelpQueryManager.java

License:Apache License

public void parseBusinessTag(YelpBusiness business, JsonElement element) {
    for (final Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
        final String key = entry.getKey();
        final JsonElement value = entry.getValue();

        if (key.contains(DISPLAY_PHONE_TAG)) {
            business.setPhoneNumber(value.getAsString());
        } else if (key.contains(LOCATION_TAG)) {
            // Location
            for (final Entry<String, JsonElement> centry : value.getAsJsonObject().entrySet()) {
                final String k1 = centry.getKey();
                final JsonElement v1 = centry.getValue();

                if (k1.contains(ADDRESS_TAG)) {
                    String address = "";
                    for (final JsonElement e2 : v1.getAsJsonArray()) {
                        address += e2.getAsString() + " ";
                    }/*from   ww  w.ja  v a2s .  c  om*/

                    business.setAddress(address);
                } else if (k1.contains(COORDINATE_TAG)) {
                    for (final Entry<String, JsonElement> loc : v1.getAsJsonObject().entrySet()) {
                        if (loc.getKey().contains(LATITUDE_TAG))
                            business.setLatitude(loc.getValue().getAsDouble());
                        else if (loc.getKey().contains(LONGITUDE_TAG))
                            business.setLongitude(loc.getValue().getAsDouble());
                    }
                } else if (k1.contains(CITY_TAG)) {
                    business.setCity(v1.getAsString());
                } else if (k1.contains(POSTAL_CODE_TAG)) {
                    business.setPostalCode(v1.getAsInt());
                } else if (k1.contains(STATE_TAG)) {
                    business.setState(v1.getAsString());
                }
            }
        } else if (key.contains(RATING_IMG_TAG)) {
            business.setRatingUrl(value.getAsString());
        } else if (key.contains(REVIEW_TAG)) {
            business.setReviewCount(value.getAsInt());
        } else if (key.contains(URL_TAG)) {
            business.setUrl(value.getAsString());
        } else if (key.contains(NAME_TAG)) {
            business.setName(value.getAsString());
        } else if (key.contains(IMAGE_URL_TAG)) {
            business.setImageUrl(value.getAsString());
        }
    }
}

From source file:com.metinkale.prayerapp.vakit.times.gson.BooleanSerializer.java

License:Apache License

@NonNull
@Override/*from w  ww  .j a va  2s .  c o  m*/
public Boolean deserialize(@NonNull JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    return arg0.getAsInt() == 1;
}