Example usage for com.google.gson.stream JsonReader nextLong

List of usage examples for com.google.gson.stream JsonReader nextLong

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextLong.

Prototype

public long nextLong() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#NUMBER long value of the next token, consuming it.

Usage

From source file:com.google.maps.internal.DistanceAdapter.java

License:Open Source License

/**
 * Read a distance object from a Directions API result and convert it to a {@link Distance}.
 *
 * <p>We are expecting to receive something akin to the following:
 * <pre>//ww  w.j ava  2s. com
 * {
 *   "value": 207,
     "text": "0.1 mi"
 * }
 * </pre>
 */
@Override
public Distance read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    Distance distance = new Distance();

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("text")) {
            distance.humanReadable = reader.nextString();
        } else if (name.equals("value")) {
            distance.inMeters = reader.nextLong();
        }

    }
    reader.endObject();

    return distance;
}

From source file:com.google.maps.internal.DurationAdapter.java

License:Open Source License

/**
 * Read a distance object from a Directions API result and convert it to a {@link Distance}.
 *
 * <p>We are expecting to receive something akin to the following:
 * <pre>//w w  w.  j a v  a 2s . co m
 * {
 *   "value": 207,
     "text": "0.1 mi"
 * }
 * </pre>
 */
@Override
public Duration read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    Duration duration = new Duration();

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("text")) {
            duration.humanReadable = reader.nextString();
        } else if (name.equals("value")) {
            duration.inSeconds = reader.nextLong();
        }

    }
    reader.endObject();

    return duration;
}

From source file:com.google.maps.internal.InstantAdapter.java

License:Open Source License

/**
 * Read a time from the Places API and convert to a {@link Instant}
 *//*from w  w  w.  j  a  v a 2s .  co  m*/
@Override
public Instant read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        // Number is the number of seconds since Epoch.
        return new Instant(reader.nextLong() * 1000L);
    }

    throw new UnsupportedOperationException("Unsupported format");
}

From source file:com.ibasco.agql.protocols.valve.dota2.webapi.adapters.Dota2TeamInfoAdapter.java

License:Open Source License

@Override
public Dota2MatchTeamInfo read(JsonReader in) throws IOException {
    in.beginObject();//from   w  w  w  .  ja v  a 2s.com
    Dota2MatchTeamInfo teamInfo = new Dota2MatchTeamInfo();
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.startsWith("league_id_")) {
            teamInfo.getLeagueIds().add(in.nextInt());
        } else if (name.startsWith("player_")) {
            teamInfo.getPlayerAccountIds().add(in.nextLong());
        } else {
            switch (name) {
            case "name":
                teamInfo.setName(in.nextString());
                break;
            case "tag":
                teamInfo.setTag(in.nextString());
                break;
            case "time_created":
                teamInfo.setTimeCreated(in.nextLong());
                break;
            case "calibration_games_remaining":
                teamInfo.setCalibrationGamesRemaining(in.nextInt());
                break;
            case "logo":
                teamInfo.setLogo(in.nextLong());
                break;
            case "logo_sponsor":
                teamInfo.setLogoSponsor(in.nextLong());
                break;
            case "country_code":
                teamInfo.setCountryCode(in.nextString());
                break;
            case "url":
                teamInfo.setUrl(in.nextString());
                break;
            case "games_played":
                teamInfo.setGamesPlayed(in.nextInt());
                break;
            case "admin_account_id":
                teamInfo.setAdminAccountId(in.nextLong());
                break;
            default:
                break;
            }
        }
    }
    in.endObject();
    return teamInfo;
}

From source file:com.ibm.watson.developer_cloud.conversation.v1.model.util.PaginationResponseTypeAdapter.java

License:Open Source License

@Override
public PaginationResponse read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from   ww  w  .j  a v a2  s  .  c  o  m
        return null;
    }
    reader.beginObject();
    PaginationResponse pagination = new PaginationResponse();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(REFRESH_URL)) {
            pagination.setRefreshUrl(reader.nextString());
        } else if (name.equals(NEXT_URL)) {
            String nextUrl = reader.nextString();
            HttpUrl url = HttpUrl.parse(RequestUtils.DEFAULT_ENDPOINT + nextUrl);
            pagination.setCursor(url.queryParameter(CURSOR));
            pagination.setNextUrl(nextUrl);
        } else if (name.equals(TOTAL)) {
            pagination.setTotal(reader.nextLong());
        } else if (name.equals(MATCHED)) {
            pagination.setMatched(reader.nextLong());
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    return pagination;
}

From source file:com.ibm.watson.developer_cloud.util.LongToDateTypeAdapter.java

License:Open Source License

@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/* ww w .  j a va2 s  . com*/
        return null;
    } else {
        // nextLong() tries to parse Strings to Longs as well
        return new Date(in.nextLong());
    }
}

From source file:com.netease.flume.taildirSource.ReliableTaildirEventReader.java

License:Apache License

/**
 * Load a position file which has the last read position of each file. If the position file exists, update tailFiles
 * mapping./*  ww w .  j  ava 2  s.c om*/
 */
public void loadPositionFile(String filePath) {
    Long inode, pos;
    String path;
    FileReader fr = null;
    JsonReader jr = null;
    try {
        fr = new FileReader(filePath);
        jr = new JsonReader(fr);
        jr.beginArray();
        while (jr.hasNext()) {
            inode = null;
            pos = null;
            path = null;
            jr.beginObject();
            while (jr.hasNext()) {
                switch (jr.nextName()) {
                case "inode":
                    inode = jr.nextLong();
                    break;
                case "pos":
                    pos = jr.nextLong();
                    break;
                case "file":
                    path = jr.nextString();
                    break;
                }
            }
            jr.endObject();

            for (Object v : Arrays.asList(inode, pos, path)) {
                Preconditions.checkNotNull(v, "Detected missing value in position file. " + "inode: " + inode
                        + ", pos: " + pos + ", path: " + path);
            }
            TailFile tf = tailFiles.get(inode);
            if (tf != null && tf.updatePos(path, inode, pos)) {
                tailFiles.put(inode, tf);
            } else {
                logger.info("Missing file: " + path + ", inode: " + inode + ", pos: " + pos);
            }
        }
        jr.endArray();
    } catch (FileNotFoundException e) {
        logger.info("File not found: " + filePath + ", not updating position");
    } catch (IOException e) {
        logger.error("Failed loading positionFile: " + filePath, e);
    } finally {
        try {
            if (fr != null)
                fr.close();
            if (jr != null)
                jr.close();
        } catch (IOException e) {
            logger.error("Error: " + e.getMessage(), e);
        }
    }
}

From source file:com.nridge.core.io.gson.RangeJSON.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into a field range.
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 *//* w  w w. ja va 2  s. com*/
public FieldRange load(JsonReader aReader) throws IOException {
    String jsonName;

    boolean isFirst = true;
    Date firstDate = new Date();
    long firstLong = Long.MIN_VALUE;
    int firstInt = Integer.MIN_VALUE;
    double firstDouble = Double.MIN_VALUE;

    Field.Type rangeType = Field.Type.Text;
    FieldRange fieldRange = new FieldRange();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            rangeType = Field.stringToType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_DELIMITER_MEMBER_NAME))
            fieldRange.setDelimiterChar(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_VALUE_MEMBER_NAME))
            fieldRange.setItems(StrUtl.expandToList(aReader.nextString(), fieldRange.getDelimiterChar()));
        else if (StringUtils.equals(jsonName, "min")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(aReader.nextLong(), firstLong);
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(aReader.nextInt(), firstInt);
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(aReader.nextDouble(), firstDouble);
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(Field.createDate(aReader.nextString()), firstDate);
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else if (StringUtils.equals(jsonName, "max")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(firstLong, aReader.nextLong());
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(firstInt, aReader.nextInt());
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(firstDouble, aReader.nextDouble());
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(firstDate, Field.createDate(aReader.nextString()));
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else
            aReader.skipValue();
    }

    aReader.endObject();

    return fieldRange;
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Long readNullSafeLong(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from  w w w  .ja  v  a 2s .  c  o  m*/
        return null;
    } else {
        return reader.nextLong();
    }
}

From source file:com.pcloud.sdk.internal.networking.serialization.DateTypeAdapter.java

License:Apache License

@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NUMBER) {
        return new Date(in.nextLong() * 1000);
    }//from www  . ja v  a2 s. co  m
    return null;
}