Example usage for com.google.gson JsonElement getAsLong

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

Introduction

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

Prototype

public long getAsLong() 

Source Link

Document

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

Usage

From source file:com.ccc.crest.core.cache.crest.schema.option.Representation.java

License:Open Source License

@Override
public Representation 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 (AcceptVersionStrKey.equals(key))
            versionStr = value.getAsString();
        else if (AcceptTypeKey.equals(key)) {
            acceptType = new AcceptType();
            acceptType.deserialize(value, typeOfT, context);

        } else if (VerbKey.equals(key))
            verb = value.getAsString();/*from  w  w w  . j  a  v a  2 s  .  co m*/
        else if (VersionKey.equals(key))
            version = value.getAsLong();
        else if (ThirdPartyKey.equals(key))
            thirdParty = value.getAsBoolean();
        else if (ContentTypeKey.equals(key)) {
            ContentType contentType = new ContentType();
            contentType.deserialize(value, 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.MatchesWon.java

License:Open Source License

@Override
public MatchesWon 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 (BlueStringKey.equals(key))
            blueTeamStr = value.getAsString();
        else if (RedKey.equals(key))
            redTeam = value.getAsLong();
        else if (RedStringKey.equals(key))
            redTeamStr = value.getAsString();
        else if (BlueKey.equals(key))
            blueTeam = value.getAsLong();
        else/*from   w w  w . ja v a 2s  .co m*/
            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);//  w  w  w .j  ava  2 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.TeamInfo.java

License:Open Source License

@Override
public TeamInfo 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 (RedTeamKey.equals(key)) {
            redTeam = new TeamWrapper();
            redTeam.deserialize(value, typeOfT, context);
        } else if (WonKey.equals(key)) {
            matchesWon = new MatchesWon();
            matchesWon.deserialize(value, typeOfT, context);
        } else if (MatchesKey.equals(key)) {
            matches = new ExternalRef();
            matches.deserialize(value, typeOfT, context);
        } else if (SelfKey.equals(key)) {
            self = new ExternalRef();
            self.deserialize(value, typeOfT, context);
        } else if (WinnerKey.equals(key)) {
            winner = new TeamWrapper();
            winner.deserialize(value, typeOfT, context);
        } else if (LoserKey.equals(key)) {
            loser = new TeamWrapper();
            loser.deserialize(value, typeOfT, context);
        } else if (LengthKey.equals(key))
            length = value.getAsLong();
        else if (LengthStrKey.equals(key))
            lengthStr = value.getAsString();
        else if (BlueTeamKey.equals(key)) {
            blueTeam = new TeamWrapper();
            blueTeam.deserialize(value, typeOfT, context);
        } else if (StructureKey.equals(key)) {
            structure = new Structure();
            structure.deserialize(value, typeOfT, context);
        } else//from   w w  w .  j a  v a 2s.com
            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);/*w  ww.  j  a  v a2s .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 long optionalJsonLong(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return -1;
    return element.getAsLong();
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public long getAsLong(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsLong();
}

From source file:com.cloudant.client.internal.views.ViewResponseImpl.java

License:Open Source License

public ViewResponseImpl(ViewQueryParameters<K, V> initialQueryParameters, JsonObject response,
        PageMetadata<K, V> pageMetadata) {
    this.initialQueryParameters = initialQueryParameters;

    if (pageMetadata == null) {
        previousPageMetadata = null;// w ww . j av a2s  .c  om
        pageNumber = 1l;
        //from a first page we can only page FORWARD
        pageMetadata = new PageMetadata<K, V>(PageMetadata.PagingDirection.FORWARD);
    } else {
        this.pageNumber = pageMetadata.pageNumber;
    }

    //build the rows from the response
    JsonArray rowsArray = response.getAsJsonArray("rows");
    if (rowsArray != null) {
        for (JsonElement row : rowsArray) {
            rows.add(new RowImpl<K, V>(initialQueryParameters, row));
        }
    }
    int resultRows = rows.size();

    JsonElement totalRowsElement = response.get("total_rows");
    if (totalRowsElement != null) {
        totalRows = totalRowsElement.getAsLong();
    } else {
        //if there is no total rows element, use the rows size
        totalRows = rows.size();
    }

    long rowsPerPage = (initialQueryParameters.getRowsPerPage() != null)
            ? initialQueryParameters.getRowsPerPage().longValue()
            : totalRows;

    //we expect limit = rowsPerPage + 1 results, if we have rowsPerPage or less we are on the
    // last page
    hasNext = resultRows > rowsPerPage;

    if (PageMetadata.PagingDirection.BACKWARD == pageMetadata.direction) {
        //Result needs reversing because to implement backward paging the view reading
        // order is reversed
        Collections.reverse(rows);
    }

    //set previous page links if not the first page
    if (this.pageNumber > 1) {
        hasPrevious = true;
        //set up previous page data, i.e. paging backward
        previousPageMetadata = new PageMetadata<K, V>(PageMetadata.PagingDirection.BACKWARD);
        //decrement the page number for the previous page
        previousPageMetadata.pageNumber = this.pageNumber - 1l;
        //this page's startKey will also be the startKey for the previous page, but with a
        // descending lookup indicated by the paging direction
        previousPageMetadata.pageRequestParameters = initialQueryParameters
                .reversePaginationQueryParameters(rows.get(0).getKey(), rows.get(0).getId());
    } else {
        hasPrevious = false;
    }

    // If we are not on the last page, we need to use the last
    // result as the start key for the next page and therefore
    // we don't return it to the user.
    // If we are on the last page, the final row should be returned
    // to the user.
    int lastIndex = resultRows - 1;
    if (hasNext) {
        //Construct the next page metadata (i.e. paging forward)
        nextPageMetadata = new PageMetadata<K, V>(PageMetadata.PagingDirection.FORWARD);
        //the last element is the start of the next page
        nextPageMetadata.pageRequestParameters = initialQueryParameters
                .forwardPaginationQueryParameters(rows.get(lastIndex).getKey(), rows.get(lastIndex).getId());
        //increment the page number for the next page
        nextPageMetadata.pageNumber = this.pageNumber + 1l;
        //the final element is the first element of the next page, so remove from the list
        rows.remove(lastIndex);
    } else {
        nextPageMetadata = null;
    }

    // calculate paging display info
    long offset = (this.pageNumber - 1) * rowsPerPage;
    resultFrom = offset + 1;
    resultTo = offset + (hasNext ? rowsPerPage : resultRows);
}

From source file:com.cognifide.aet.vs.metadata.TimestampSerializer.java

License:Apache License

@Override
public Suite.Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    Suite.Timestamp result = null;
    if (json != null) {
        final Set<Map.Entry<String, JsonElement>> jsonEntrySet = ((JsonObject) json).entrySet();
        final Iterator<Map.Entry<String, JsonElement>> iterator = jsonEntrySet.iterator();
        if (iterator.hasNext()) {
            final JsonElement value = iterator.next().getValue();
            result = new Suite.Timestamp(value.getAsLong());
        }//from ww  w . j  av a 2s  .  com
    }
    return result;
}

From source file:com.crowdmap.java.sdk.json.DateDeserializer.java

License:Open Source License

@Override
public Date deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    try {//from  w  ww.j a va 2  s.c  o  m
        return new Date(arg0.getAsLong() * 1000);
    } catch (NumberFormatException e) {
        return null;
    }
}