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.blockartistry.mod.ThermalRecycling.support.ItemDefinitions.java

License:MIT License

public static ItemDefinitions load(final String modId) {
    final String fileName = modId.replaceAll("[^a-zA-Z0-9.-]", "_");
    InputStream stream = null;/*from   w ww. ja v a 2s  .  c  o m*/
    InputStreamReader reader = null;
    JsonReader reader2 = null;
    try {
        stream = ItemDefinitions.class.getResourceAsStream("/assets/recycling/data/" + fileName + ".json");
        if (stream != null) {
            reader = new InputStreamReader(stream);
            reader2 = new JsonReader(reader);
            return (ItemDefinitions) new Gson().fromJson(reader, ItemDefinitions.class);
        }
    } finally {
        try {
            if (reader2 != null)
                reader2.close();
            if (reader != null)
                reader.close();
            if (stream != null)
                stream.close();
        } catch (final Exception e) {
            ;
        }
    }
    return new ItemDefinitions();
}

From source file:org.eclipse.agail.genconfgen.CheckAll.java

License:Open Source License

private static ArrayList<UserRequirementDefinition> parseUserRequirementsDefinition() {
    ArrayList<UserRequirementDefinition> userRequirements = new ArrayList<UserRequirementDefinition>();

    try {/*w w  w .j  a v a2s . c o  m*/
        JsonReader reader = new JsonReader(new FileReader(userRequirementsDefinitionPath));
        reader.beginObject();
        reader.nextName();
        UserRequirementDefinition[] userRequirementsArray = (new Gson()).fromJson(reader,
                UserRequirementDefinition[].class);
        reader.close();

        for (int i = 0; i < userRequirementsArray.length; i++) {
            userRequirements.add(userRequirementsArray[i]);
        }
    } catch (FileNotFoundException e) {
        System.err.println("Could not read from file " + userRequirementsDefinitionPath);
        e.printStackTrace();
        return new ArrayList<UserRequirementDefinition>();
    } catch (IOException e) {
        System.err.println("Could not close file " + userRequirementsDefinitionPath);
        e.printStackTrace();
    }

    return userRequirements;
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.RuleGSONParser.java

License:Open Source License

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/*  w  w w  .  j a  v a 2 s .  c o m*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Rule[] rules = gson.fromJson(jr, Rule[].class);
                return new HashSet<Rule>(Arrays.asList(rules));
            } else {
                Rule rule = gson.fromJson(jr, Rule.class);
                Set<Rule> rules = new HashSet<Rule>();
                rules.add(rule);
                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.eclipse.smarthome.automation.parser.gson.internal.TemplateGSONParser.java

License:Open Source License

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {/* w  w  w . jav a  2s  . co m*/
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                Template[] templates = gson.fromJson(jr, RuleTemplate[].class);
                return new HashSet<Template>(Arrays.asList(templates));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                Set<Template> templates = new HashSet<Template>();
                templates.add(template);
                return templates;
            }
        }
    } catch (Exception e1) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e1));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}

From source file:org.eclipse.thym.core.engine.AbstractEngineRepoProvider.java

License:Open Source License

protected List<DownloadableCordovaEngine> getEnginesFromStream(InputStream stream) throws IOException {
    List<DownloadableCordovaEngine> engines = new ArrayList<DownloadableCordovaEngine>();
    JsonReader reader = null;
    try {/*  w  ww. ja  v  a2  s .com*/
        reader = new JsonReader(new InputStreamReader(stream));
        JsonParser parser = new JsonParser();
        JsonObject root = (JsonObject) parser.parse(reader);
        Set<Entry<String, JsonElement>> versions = root.entrySet();
        for (Iterator<Entry<String, JsonElement>> iterator = versions.iterator(); iterator.hasNext();) {
            Entry<String, JsonElement> entry = iterator.next();
            JsonObject version = entry.getValue().getAsJsonObject();
            DownloadableCordovaEngine engine = new DownloadableCordovaEngine();
            engine.setVersion(entry.getKey());
            Set<Entry<String, JsonElement>> libs = version.entrySet();
            for (Iterator<Entry<String, JsonElement>> libsIterator = libs.iterator(); libsIterator.hasNext();) {
                Entry<String, JsonElement> lib = libsIterator.next();
                LibraryDownloadInfo info = new LibraryDownloadInfo();
                info.setPlatformId(lib.getKey());
                JsonObject infoJsonObj = lib.getValue().getAsJsonObject();
                info.setDownloadURL(infoJsonObj.get("download_url").getAsString());
                info.setVersion(infoJsonObj.get("version").getAsString());
                engine.addLibraryInfo(info);
            }
            engines.add(engine);
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return engines;
}

From source file:org.eclipse.thym.core.engine.internal.cordova.NpmBasedEngineRepoProvider.java

License:Open Source License

private List<DownloadableCordovaEngine> parseEngines(InputStream stream, String platformId) throws IOException {
    List<DownloadableCordovaEngine> engines = new ArrayList<DownloadableCordovaEngine>();
    JsonReader reader = null;
    try {//from  ww w .  ja  v  a  2  s  .c  o  m
        reader = new JsonReader(new InputStreamReader(stream));
        JsonParser parser = new JsonParser();
        final JsonObject root = (JsonObject) parser.parse(reader);
        final JsonElement element = root.get("versions");
        final JsonObject topVersions = element.getAsJsonObject();
        final Set<Entry<String, JsonElement>> versions = topVersions.entrySet();
        for (Iterator<Entry<String, JsonElement>> iterator = versions.iterator(); iterator.hasNext();) {
            Entry<String, JsonElement> entry = iterator.next();
            JsonObject v = entry.getValue().getAsJsonObject();
            DownloadableCordovaEngine engine = new DownloadableCordovaEngine();
            engine.setVersion(v.get("version").getAsString());
            engine.setPlatformId(platformId);
            JsonObject dist = v.get("dist").getAsJsonObject();
            engine.setDownloadURL(dist.get("tarball").getAsString());
            engines.add(engine);
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return engines;
}

From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java

License:Open Source License

public List<CordovaRegistryPluginInfo> retrievePluginInfos(IProgressMonitor monitor) throws CoreException {

    if (monitor == null)
        monitor = new NullProgressMonitor();

    monitor.beginTask("Retrieve plug-in registry catalog", 10);
    DefaultHttpClient theHttpClient = new DefaultHttpClient();
    HttpUtil.setupProxy(theHttpClient);/*from  w w w  .j  av  a  2  s.  c  om*/
    HttpClient client = new CachingHttpClient(theHttpClient, new HeapResourceFactory(),
            new BundleHttpCacheStorage(HybridCore.getContext().getBundle()), getCacheConfig());
    JsonReader reader = null;
    try {
        if (monitor.isCanceled()) {
            return null;
        }
        String url = REGISTRY_URL
                + "-/_view/byKeyword?startkey=%5B%22ecosystem:cordova%22%5D&endkey=%5B%22ecosystem:cordova1%22%5D&group_level=3";
        HttpGet get = new HttpGet(URI.create(url));
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        monitor.worked(7);
        reader = new JsonReader(new InputStreamReader(stream));
        reader.beginObject();//start the Registry
        final ArrayList<CordovaRegistryPluginInfo> plugins = new ArrayList<CordovaRegistryPluginInfo>();
        while (reader.hasNext()) {
            JsonToken token = reader.peek();
            switch (token) {
            case BEGIN_ARRAY:
                reader.beginArray();
                break;
            case BEGIN_OBJECT:
                plugins.add(parseCordovaRegistryPluginInfo(reader));
                break;
            default:
                reader.skipValue();
                break;
            }

        }
        return plugins;

    } catch (ClientProtocolException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e));
    } catch (IOException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e));
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {
                /*ignored*/ }
        monitor.done();
    }
}

From source file:org.eclipse.tm4e.core.internal.parser.json.JSONPListParser.java

License:Open Source License

public T parse(InputStream contents) throws Exception {
    PList<T> pList = new PList<T>(theme);
    JsonReader reader = new JsonReader(new InputStreamReader(contents, StandardCharsets.UTF_8));
    // reader.setLenient(true);
    boolean parsing = true;
    while (parsing) {
        JsonToken nextToken = reader.peek();
        switch (nextToken) {
        case BEGIN_ARRAY:
            pList.startElement(null, "array", null, null);
            reader.beginArray();//  w ww .  j a  v a2 s  . c  o  m
            break;
        case END_ARRAY:
            pList.endElement(null, "array", null);
            reader.endArray();
            break;
        case BEGIN_OBJECT:
            pList.startElement(null, "dict", null, null);
            reader.beginObject();
            break;
        case END_OBJECT:
            pList.endElement(null, "dict", null);
            reader.endObject();
            break;
        case NAME:
            String lastName = reader.nextName();
            pList.startElement(null, "key", null, null);
            pList.characters(lastName.toCharArray(), 0, lastName.length());
            pList.endElement(null, "key", null);
            break;
        case NULL:
            reader.nextNull();
            break;
        case BOOLEAN:
            reader.nextBoolean();
            break;
        case NUMBER:
            reader.nextLong();
            break;
        case STRING:
            String value = reader.nextString();
            pList.startElement(null, "string", null, null);
            pList.characters(value.toCharArray(), 0, value.length());
            pList.endElement(null, "string", null);
            break;
        case END_DOCUMENT:
            parsing = false;
            break;
        default:
            break;
        }
    }
    reader.close();
    return pList.getResult();
}

From source file:org.gradle.nativeplatform.toolchain.internal.msvcpp.version.CommandLineToolVersionLocator.java

License:Apache License

private List<VisualStudioInstallCandidate> parseJson(String json) {
    List<VisualStudioInstallCandidate> installs = Lists.newArrayList();
    JsonReader reader = new JsonReader(new StringReader(json));
    try {/*from w ww . j  a va  2s. c o m*/
        try {
            reader.beginArray();
            while (reader.hasNext()) {
                VisualStudioInstallCandidate candidate = readInstall(reader);

                if (candidate != null) {
                    installs.add(candidate);
                }
            }
            reader.endArray();
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }

    return installs;
}

From source file:org.guvnor.ala.openshift.access.OpenShiftRuntimeId.java

License:Apache License

public static OpenShiftRuntimeId fromString(String s) {
    String project = null;//from  w w  w .  j  ava 2s  .  co  m
    String service = null;
    String application = null;
    try {
        JsonReader jr = new JsonReader(new StringReader(s));
        jr.beginObject();
        while (jr.hasNext()) {
            String n = jr.nextName();
            if (PROJECT.equals(n)) {
                project = jr.nextString();
            } else if (SERVICE.equals(n)) {
                service = jr.nextString();
            } else if (APPLICATION.equals(n)) {
                application = jr.nextString();
            } else {
                jr.skipValue();
            }
        }
        jr.endObject();
        jr.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
    return new OpenShiftRuntimeId(project, service, application);
}