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

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

Introduction

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

Prototype

public void beginArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.

Usage

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

License:LGPL

/**
 * Reads a geometry that has the following format: [[lat1,lon1], [lat2,lon2],...[latn,lonn]].
 *
 * @param reader a {@code JsonReader} object
 * @return a list of {@code LatLon} objects
 * @throws IOException if the read operation failed
 *///from   ww w. j a  v  a 2 s.  c  o m
static List<LatLon> readGeometry(final JsonReader reader) throws IOException {
    final List<LatLon> geometry = new ArrayList<>();
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
    } else {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginArray();
            final Double lat = reader.nextDouble();
            final Double lon = reader.nextDouble();
            geometry.add(new LatLon(lat, lon));
            reader.endArray();
        }
        reader.endArray();
    }
    return geometry;
}

From source file:org.oscii.panlex.PanLexJSONParser.java

private <T> void parse(InputStream in, T record, Predicate<T> process) {
    int accepted = 0;
    Class type = record.getClass();
    String name = type.getSimpleName();
    Gson gson = new Gson();
    log.info(String.format("Parsing %s records", name));
    try {//  ww w  .  j  ava 2  s  . com
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            record = gson.fromJson(reader, type);
            if (process.test(record)) {
                accepted++;
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    log.info(String.format("Parsed %d %s records", accepted, name));
}

From source file:org.sonar.server.computation.AnalysisReportService.java

License:Open Source License

@VisibleForTesting
void saveIssues(ComputeEngineContext context) {
    IssueStorage issueStorage = issueStorageFactory.newComputeEngineIssueStorage(context.getProject());

    File issuesFile = new File(context.getReportDirectory(), "issues.json");
    List<DefaultIssue> issues = new ArrayList<>(MAX_ISSUES_SIZE);

    try {// w w w.  j  a va2 s.c om
        InputStream issuesStream = new FileInputStream(issuesFile);
        JsonReader reader = new JsonReader(new InputStreamReader(issuesStream));
        reader.beginArray();
        while (reader.hasNext()) {
            ReportIssue reportIssue = gson.fromJson(reader, ReportIssue.class);
            DefaultIssue defaultIssue = toIssue(context, reportIssue);
            issues.add(defaultIssue);
            if (shouldPersistIssues(issues, reader)) {
                issueStorage.save(issues);
                issues.clear();
            }
        }

        reader.endArray();
        reader.close();
    } catch (IOException e) {
        throw new IllegalStateException("Failed to read issues", e);
    }
}

From source file:org.spongepowered.plugin.meta.gson.ModMetadataAdapter.java

License:MIT License

@Override
public PluginMetadata read(JsonReader in) throws IOException {
    in.beginObject();/*from ww  w .  java  2  s  .c  om*/

    final Set<String> processedKeys = new HashSet<>();

    final PluginMetadata result = new PluginMetadata("unknown");
    String id = null;

    while (in.hasNext()) {
        final String name = in.nextName();
        if (!processedKeys.add(name)) {
            throw new JsonParseException("Duplicate key '" + name + "' in " + in);
        }

        switch (name) {
        case "modid":
            id = in.nextString();
            result.setId(id);
            break;
        case "name":
            result.setName(in.nextString());
            break;
        case "version":
            result.setVersion(in.nextString());
            break;
        case "description":
            result.setDescription(in.nextString());
            break;
        case "url":
            result.setUrl(in.nextString());
            break;
        case "authorList":
            in.beginArray();
            while (in.hasNext()) {
                result.addAuthor(in.nextString());
            }
            in.endArray();
            break;
        case "requiredMods":
            in.beginArray();
            while (in.hasNext()) {
                result.addRequiredDependency(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        case "dependencies":
            in.beginArray();
            while (in.hasNext()) {
                result.loadAfter(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        case "dependants":
            in.beginArray();
            while (in.hasNext()) {
                result.loadBefore(ModDependencyAdapter.INSTANCE.read(in));
            }
            in.endArray();
            break;
        default:
            result.setExtension(name, this.gson.fromJson(in, getExtension(name)));
        }
    }

    in.endObject();

    if (id == null) {
        throw new JsonParseException("Mod metadata is missing required element 'modid'");
    }

    return result;
}

From source file:org.spongepowered.plugin.meta.gson.ModMetadataCollectionAdapter.java

License:MIT License

@Override
public List<PluginMetadata> read(JsonReader in) throws IOException {
    in.beginArray();
    ImmutableList.Builder<PluginMetadata> result = ImmutableList.builder();
    while (in.hasNext()) {
        result.add(this.metadataAdapter.read(in));
    }/*ww  w. ja v a2s. co m*/
    in.endArray();
    return result.build();
}

From source file:org.terasology.rendering.gui.components.UITextWrap.java

License:Apache License

public void showFromJson() throws IOException {
    int maxlines = getLineCount();
    int screenlines = getScreenLines();
    long beginpos, endpos, counter;
    if (screenlines > maxlines) {
        beginpos = -1;//from w  w w  .  ja v a2  s.  c o  m
    } else {
        if (currentpos < 0) {
            currentpos = 0;
        }
        if (currentpos > maxlines - screenlines) {
            currentpos = maxlines - screenlines;
        }
        beginpos = maxlines - (screenlines + currentpos) - 1;
    }
    endpos = beginpos + screenlines + 1;
    //if(endpos >maxlines){endpos = maxlines +1;}
    counter = 0;
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\consolelog.json"));
    reader.beginArray();
    _text = "";
    while (reader.hasNext()) {
        if (counter > beginpos && counter < endpos) {
            _text += gson.fromJson(reader, String.class);
        } else {
            gson.fromJson(reader, String.class);
        }
        counter++;
    }
    reader.endArray();
    reader.close();

}

From source file:org.terasology.rendering.gui.components.UITextWrap.java

License:Apache License

public void loadHelp() throws IOException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\help.json"));
    reader.beginArray();
    _text = "";//from  w w w  .j a  va  2  s .  c  o  m
    while (reader.hasNext()) {
        _text += gson.fromJson(reader, String.class) + newLine;
    }
    reader.endArray();
    reader.close();
}

From source file:org.terasology.rendering.gui.components.UITextWrap.java

License:Apache License

public void loadError() throws IOException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\error.json"));
    reader.beginArray();
    _text = "";//from ww w  . j av  a  2s .com
    while (reader.hasNext()) {
        _text += gson.fromJson(reader, String.class) + newLine;
    }
    reader.endArray();
    reader.close();
}

From source file:org.terasology.rendering.gui.components.UITextWrap.java

License:Apache License

public int getLineCount() throws IOException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\consolelog.json"));
    int counter = 0;
    reader.beginArray();
    while (reader.hasNext()) {
        counter++;/*from  ww w.ja  v  a2s  .  co m*/
        gson.fromJson(reader, String.class);
    }
    reader.endArray();
    reader.close();
    return counter;
}

From source file:org.translator.java.AppInventorProperties.java

License:Apache License

protected AppInventorProperties(JsonReader reader) throws IOException {
    reader.beginObject();/*from ww  w  . j a v a2s.  c  o m*/

    while (reader.peek() == JsonToken.NAME) {
        String name = reader.nextName();
        if (name.equals("$Components")) {
            reader.beginArray();

            while (reader.peek() == JsonToken.BEGIN_OBJECT)
                components.add(new AppInventorProperties(reader));

            reader.endArray();
        } else {
            String n = reader.nextString();
            if (n.matches("True|False"))
                n = n.toLowerCase();
            properties.put(name, n);
        }
    }

    reader.endObject();
}