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:org.mitre.util.JsonUtils.java

License:Apache License

public static Map readMap(JsonReader reader) throws IOException {
    Map map = new HashMap<>();
    reader.beginObject();/* w  w  w  . ja  v a2 s .c o  m*/
    while (reader.hasNext()) {
        String name = reader.nextName();
        Object value = null;
        switch (reader.peek()) {
        case STRING:
            value = reader.nextString();
            break;
        case BOOLEAN:
            value = reader.nextBoolean();
            break;
        case NUMBER:
            value = reader.nextLong();
            break;
        default:
            logger.debug("Found unexpected entry");
            reader.skipValue();
            continue;
        }
        map.put(name, value);
    }
    reader.endObject();
    return map;
}

From source file:org.mitre.util.JsonUtils.java

License:Apache License

public static Set readSet(JsonReader reader) throws IOException {
    Set arraySet = null;/*  ww  w . j a va2 s  .  co m*/
    reader.beginArray();
    switch (reader.peek()) {
    case STRING:
        arraySet = new HashSet<>();
        while (reader.hasNext()) {
            arraySet.add(reader.nextString());
        }
        break;
    case NUMBER:
        arraySet = new HashSet<>();
        while (reader.hasNext()) {
            arraySet.add(reader.nextLong());
        }
        break;
    default:
        arraySet = new HashSet();
        break;
    }
    reader.endArray();
    return arraySet;
}

From source file:org.netxms.websvc.json.adapters.DateAdapter.java

License:Open Source License

@Override
public Date read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from w  w  w .j  a v a 2  s  .c om*/
        return null;
    }

    try {
        return new Date(reader.nextLong() * 1000);
    } catch (NumberFormatException e) {
        return null;
    } catch (IllegalStateException e) {
        return null;
    }
}

From source file:org.openstreetmap.josm.plugins.openstreetcam.service.photo.adapter.ReaderUtil.java

License:LGPL

static Long readLong(final JsonReader reader) throws IOException {
    Long value = null;/*ww w. ja v a2  s. c om*/
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        value = reader.nextLong();
    }
    return value;
}

From source file:org.openstreetmap.josm.plugins.openstreetcam.service.PhotoTypeAdapter.java

License:Apache License

private Long readLong(final JsonReader reader) throws IOException {
    Long value = null;/* ww  w  . j a va 2 s  .c om*/
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        value = reader.nextLong();
    }
    return value;
}

From source file:org.ttrssreader.net.JSONConnector.java

License:Open Source License

private boolean parseArticle(final Article a, final JsonReader reader,
        final Set<Article.ArticleField> skipNames, final IArticleOmitter filter) throws IOException {

    boolean skipObject = false;
    while (reader.hasNext() && reader.peek().equals(JsonToken.NAME)) {
        if (skipObject) {
            // field name
            reader.skipValue();//  ww w  .j av  a  2s.c  o m
            // field value
            reader.skipValue();
            continue;
        }

        String name = reader.nextName();
        Article.ArticleField field = Article.ArticleField.valueOf(name);

        try {

            if (skipNames != null && skipNames.contains(field)) {
                reader.skipValue();
                continue;
            }

            switch (field) {
            case id:
                a.id = reader.nextInt();
                break;
            case title:
                a.title = reader.nextString();
                break;
            case unread:
                a.isUnread = reader.nextBoolean();
                break;
            case updated:
                a.updated = new Date(reader.nextLong() * 1000);
                break;
            case feed_id:
                if (reader.peek() == JsonToken.NULL)
                    reader.nextNull();
                else
                    a.feedId = reader.nextInt();
                break;
            case content:
                a.content = reader.nextString();
                break;
            case link:
                a.url = reader.nextString();
                break;
            case comments:
                a.commentUrl = reader.nextString();
                break;
            case attachments:
                a.attachments = parseAttachments(reader);
                break;
            case marked:
                a.isStarred = reader.nextBoolean();
                break;
            case published:
                a.isPublished = reader.nextBoolean();
                break;
            case labels:
                a.labels = parseLabels(reader);
                break;
            case author:
                a.author = reader.nextString();
                break;
            default:
                reader.skipValue();
                continue;
            }

            if (filter != null)
                skipObject = filter.omitArticle(field, a);

        } catch (IllegalArgumentException | StopJsonParsingException | IOException e) {
            Log.w(TAG, "Result contained illegal value for entry \"" + field + "\".");
            reader.skipValue();
        }
    }
    return skipObject;
}

From source file:vogar.ExpectationStore.java

License:Apache License

private void readExpectation(JsonReader reader, ModeId mode, Variant variant) throws IOException {
    boolean isFailure = false;
    Result result = Result.SUCCESS;
    Pattern pattern = Expectation.MATCH_ALL_PATTERN;
    Set<String> names = new LinkedHashSet<String>();
    Set<String> tags = new LinkedHashSet<String>();
    Map<ModeId, Set<Variant>> modeVariants = null;
    Set<ModeId> modes = null;
    String description = "";
    long buganizerBug = -1;

    reader.beginObject();//from  w  w w . jav a2  s  .  c o  m
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("result")) {
            result = Result.valueOf(reader.nextString());
        } else if (name.equals("name")) {
            names.add(reader.nextString());
        } else if (name.equals("names")) {
            readStrings(reader, names);
        } else if (name.equals("failure")) {
            isFailure = true;
            names.add(reader.nextString());
        } else if (name.equals("pattern")) {
            pattern = Pattern.compile(reader.nextString(), PATTERN_FLAGS);
        } else if (name.equals("substring")) {
            pattern = Pattern.compile(".*" + Pattern.quote(reader.nextString()) + ".*", PATTERN_FLAGS);
        } else if (name.equals("tags")) {
            readStrings(reader, tags);
        } else if (name.equals("description")) {
            Iterable<String> split = Splitter.on("\n").omitEmptyStrings().trimResults()
                    .split(reader.nextString());
            description = Joiner.on("\n").join(split);
        } else if (name.equals("bug")) {
            buganizerBug = reader.nextLong();
        } else if (name.equals("modes")) {
            modes = readModes(reader);
        } else if (name.equals("modes_variants")) {
            modeVariants = readModesAndVariants(reader);
        } else {
            log.warn("Unhandled name in expectations file: " + name);
            reader.skipValue();
        }
    }
    reader.endObject();

    if (names.isEmpty()) {
        throw new IllegalArgumentException("Missing 'name' or 'failure' key in " + reader);
    }
    if (modes != null && !modes.contains(mode)) {
        return;
    }
    if (modeVariants != null) {
        Set<Variant> variants = modeVariants.get(mode);
        if (variants == null || !variants.contains(variant)) {
            return;
        }
    }

    Expectation expectation = new Expectation(result, pattern, tags, description, buganizerBug, true);
    Map<String, Expectation> map = isFailure ? failures : outcomes;
    for (String name : names) {
        if (map.put(name, expectation) != null) {
            throw new IllegalArgumentException("Duplicate expectations for " + name);
        }
    }
}

From source file:yagw2api.server.character.LocalDateTimeGSONAdapter.java

License:Apache License

@Override
public LocalDateTime read(final JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from  w  w w  . j a  v  a  2s .c o  m
        return null;
    } else {
        return LocalDateTime.ofEpochSecond(reader.nextLong(), 0, ZoneOffset.UTC);
    }
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private Alert parseAlert(JsonReader reader) throws IOException {
    Alert output = new Alert();
    reader.beginObject();// w  ww  .ja  v a  2s .c o m
    String name = reader.nextName();
    if (name.equals("title")) {
        output.setTitle(reader.nextString());
    } else if (name.equals("expires")) {
        output.setExpires(reader.nextLong());
    } else if (name.equals("description")) {
        output.setDescription(reader.nextString());
    } else if (name.equals("uri")) {
        output.setUri(reader.nextString());
    } else {
        reader.skipValue();
    }
    reader.endObject();
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private void getDataPointProperty(JsonReader reader, DataPoint output) throws IOException {
    String name = reader.nextName();
    if (name.equals("time")) {
        output.setTime(reader.nextLong() * 1000); // forecast api gives time in seconds, while Java, Android and joda time work with milliseconds...
    } else if (name.equals("icon")) {
        output.setIcon(reader.nextString());
    } /*else if (name.equals("precipIntensity")) {
      output.setPrecipIntensity(reader.nextDouble());
      } else if (name.equals("precipProbability")) {
      output.setPrecipProbability(reader.nextDouble());
      } else if (name.equals("precipType")) {
      output.setPrecipType(reader.nextString());
      } else if (name.equals("precipAccumulation")) {
      output.setPrecipAccumulation(reader.nextDouble());
      }*/ else if (name.equals("temperature")) {
        output.setTemperature(reader.nextDouble());
    } else if (name.equals("temperatureMin")) {
        output.setTemperatureMin(reader.nextDouble());
    } else if (name.equals("temperatureMinTime")) {
        output.setTemperatureMinTime(reader.nextLong());
    } else if (name.equals("temperatureMax")) {
        output.setTemperatureMax(reader.nextDouble());
    } else if (name.equals("temperatureMaxTime")) {
        output.setTemperatureMaxTime(reader.nextLong());
    } else if (name.equals("apparentTemperature")) {
        output.setApparentTemperature(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMin")) {
        output.setApparentTemperatureMin(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMax")) {
        output.setApparentTemperatureMax(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMaxTime")) {
        output.setApparentTemperatureMaxTime(reader.nextLong());
    } else if (name.equals("windSpeed")) {
        output.setWindSpeed(reader.nextDouble());
    } else if (name.equals("windBearing")) {
        output.setWindBearing(reader.nextDouble());
    } /*else if (name.equals("cloudCover")) {
      output.setCloudCover(reader.nextDouble());
      }*/ else if (name.equals("humidity")) {
        output.setHumidity(reader.nextDouble());
    } else {//from w  ww  . j av  a  2s. c  o m
        reader.skipValue();
    }
}