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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this JSON reader and the underlying java.io.Reader .

Usage

From source file:org.opendaylight.netconf.sal.rest.impl.JsonToPATCHBodyReader.java

License:Open Source License

private PATCHContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
        throws IOException {
    if (entityStream.available() < 1) {
        return new PATCHContext(path, null, null);
    }//ww  w .jav  a 2  s .  c o  m

    final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream));
    final List<PATCHEntity> resultList = read(jsonReader, path);
    jsonReader.close();

    return new PATCHContext(path, resultList, patchId);
}

From source file:org.openhab.core.automation.internal.parser.gson.RuleGSONParser.java

License:Open Source License

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {//  w w  w .ja  va2 s  . c  om
        Set<Rule> rules = new HashSet<>();
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                List<RuleDTO> ruleDtos = gson.fromJson(jr, new TypeToken<List<RuleDTO>>() {
                }.getType());
                for (RuleDTO ruleDto : ruleDtos) {
                    rules.add(RuleDTOMapper.map(ruleDto));
                }
            } else {
                RuleDTO ruleDto = gson.fromJson(jr, RuleDTO.class);
                rules.add(RuleDTOMapper.map(ruleDto));
            }
            return rules;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.openhab.core.automation.internal.parser.gson.TemplateGSONParser.java

License:Open Source License

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {//from ww  w . j  a v  a 2 s  .  c o  m
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            Set<Template> templates = new HashSet<>();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                List<RuleTemplateDTO> templateDtos = gson.fromJson(jr, new TypeToken<List<RuleTemplateDTO>>() {
                }.getType());
                for (RuleTemplateDTO templateDto : templateDtos) {
                    templates.add(RuleTemplateDTOMapper.map(templateDto));
                }
            } else {
                RuleTemplateDTO template = gson.fromJson(jr, RuleTemplateDTO.class);
                templates.add(RuleTemplateDTOMapper.map(template));
            }
            return templates;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

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 {//from  w  ww .ja  v a 2  s .c  o  m
        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.restcomm.connect.extension.configuration.DefaultExtensionConfiguration.java

License:Open Source License

public JsonObject loadDefaultConfiguration(String localConfigFilePath) throws IOException {
    JsonObject jsonObj = null;//from   w ww.j a v  a  2  s . co m
    jsonParser = new JsonParser();
    InputStream in = (InputStream) getClass().getResourceAsStream(localConfigFilePath);
    BufferedReader inReader = new BufferedReader(new InputStreamReader(in));
    JsonReader reader = new JsonReader(inReader);
    JsonElement jsonElement = jsonParser.parse(reader);
    jsonObj = (JsonObject) jsonElement;
    in.close();
    inReader.close();
    reader.close();
    return jsonObj;
}

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.  ja  va 2  s.co  m
        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.sputnikdev.bluetooth.gattparser.spec.BluetoothGattSpecificationReader.java

License:Apache License

private Map<String, String> readRegistryFromCatalogResource(URL serviceRegistry) {
    logger.info("Reading GATT registry from: {}", serviceRegistry);
    if (serviceRegistry == null) {
        throw new IllegalStateException("GATT spec registry file is missing");
    }/* ww w.j  av  a2  s. co  m*/

    Type type = new TypeToken<Map<String, String>>() {
    }.getType();
    Gson gson = new Gson();

    JsonReader jsonReader = null;
    try {
        jsonReader = new JsonReader(new InputStreamReader(serviceRegistry.openStream(), "UTF-8"));
        return gson.fromJson(jsonReader, type);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } finally {
        if (jsonReader != null) {
            try {
                jsonReader.close();
            } catch (IOException e) {
                logger.error("Could not close stream", e);
            }
        }
    }
}

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;//  w  ww .j av  a  2 s.c  om
    } 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();//  w ww.j a va  2  s  .com
    _text = "";
    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();// w  ww. j  a  v  a  2s. c o m
    _text = "";
    while (reader.hasNext()) {
        _text += gson.fromJson(reader, String.class) + newLine;
    }
    reader.endArray();
    reader.close();
}