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

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

Introduction

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

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

From source file:bmw.assigment.BMWAssigment.java

/**
 * For this reading I used GSON, a library made by google to parse JSON objects
 * In order for this to run propertly, a file employees.json must be in the path /resources/employees.json
 * //from w  ww  .  ja v  a  2s  .  c om
 * @return JsonObject
 */
public JsonObject readJsonFile() {

    JsonObject employeeRoot = null;

    try {
        InputStream str = BMWAssigment.class.getResourceAsStream("/resources/employees.json");
        JsonReader reader = new JsonReader(new InputStreamReader(str));
        JsonParser jsonParser = new JsonParser();
        employeeRoot = jsonParser.parse(reader).getAsJsonObject();

        reader.close();
        str.close();

    } catch (IOException ex) {
        Logger.getLogger(BMWAssigment.class.getName()).log(Level.SEVERE, null, ex);
    }

    return employeeRoot;
}

From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java

License:Apache License

public static JsonElement json(InputStream is) {
    JsonParser parser = new JsonParser();
    JsonReader reader = null;/*from w ww.  j a  va  2 s. c o m*/
    try {
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw Exceptions.propagate(e);
    }
    JsonElement el = parser.parse(reader);
    return el;
}

From source file:brooklyn.storage.softlayer.SoftLayerRestClient.java

License:Apache License

public static JsonElement json(InputStream is) {
    JsonParser parser = new JsonParser();
    JsonReader reader = null;// w ww .  j av a2 s .  c o  m
    try {
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    JsonElement el = parser.parse(reader);
    return el;
}

From source file:bytehala.flowmortarexample.GsonParceler.java

License:Apache License

private Path decode(String json) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(json));

    try {/*from ww w .j  a  va  2 s.  com*/
        reader.beginObject();

        Class<?> type = Class.forName(reader.nextName());
        return gson.fromJson(reader, type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        reader.close();
    }
}

From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java

License:Apache License

private void tryreadingthejson() throws IOException {
    InputStream in = new FileInputStream(DataPath.get() + "dead_links.json");
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();//from   www  . j  a v a 2 s. c o m
    while (reader.hasNext()) {

    }

    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java

License:Apache License

private static void readFile(String pFilePath) throws IOException, InterruptedException {
    InputStream in = new FileInputStream(pFilePath);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();/*  ww w  .  j av a2s .c om*/
    while (reader.hasNext()) {

        ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class);
        writeToFile = writeToFile.concat("{\"product_id\":" + prodStub.id + ",");
        String urltext = prodStub.modelOverviewPageUrl;
        // Write 404 error if no URL exists
        if (urltext == null) {
            writeToFile = writeToFile.concat("\"state\":" + "404" + "},");
            continue;
        }

        Thread.sleep(SLEEP);
        URL url = new URL(urltext);
        // Attempt a connection and see the resulting response code it returns
        int responseCode = ((HttpURLConnection) url.openConnection()).getResponseCode();
        writeToFile = writeToFile.concat("\"state\":" + responseCode + "},");
    }
    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java

License:Apache License

private static void readFile(String filePath, IDataCollector pCollector) throws IOException {
    InputStream in = new FileInputStream(filePath);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

    reader.beginArray();/* w  ww.j a v  a2 s  . c  om*/
    while (reader.hasNext()) {
        ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class);
        pCollector.addProduct(buildProduct(prodStub));

    }
    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java

License:Apache License

private void readDeadLinks() throws FileNotFoundException, IOException {
    // Try to read the dead links file
    InputStream in;/*from  w  w  w. ja  v  a2s  . com*/
    try {
        in = new FileInputStream(aPath + aDeadLinksFileName);
    } catch (FileNotFoundException e) {
        return;
    }

    // Flag that we were succesful finding the deadlins file, so deadlinks
    // will be checked while building products
    aDoCheckDeadLinks = true;

    // Make a json reader for the deadlinks file
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();

    // Iterate over each entry in the deadlinks file, putting a records in a HashTable
    while (reader.hasNext()) {
        LinkResponseStub responseStub = new Gson().fromJson(reader, LinkResponseStub.class);
        aDeadLinks.put(responseStub.product_id, responseStub.state);
    }

    reader.endArray();
    reader.close();
    in.close();
}

From source file:cern.c2mon.shared.client.request.ClientRequestImpl.java

License:Open Source License

/**
 * @param json Json string representation of a <code>TransferTagRequestImpl</code> class
 * @return The deserialized Json message
 * @throws JsonSyntaxException This exception is raised when Gson attempts to read
 *                             (or write) a malformed JSON element.
 *//*from   w  ww .  java  2 s.  co m*/
public static final ClientRequest fromJson(final String json) {
    JsonReader jsonReader = new JsonReader(new StringReader(json));
    jsonReader.setLenient(true);
    return getGson().fromJson(jsonReader, ClientRequestImpl.class);
}

From source file:cern.c2mon.shared.client.request.ClientRequestImpl.java

License:Open Source License

@Override
public final Collection<T> fromJsonResponse(final String jsonString) throws JsonSyntaxException {
    Type collectionType;//from  w ww  .  j  a  v  a 2 s  .  co  m
    TypeReference jacksonCollectionType;
    JsonReader jsonReader = new JsonReader(new StringReader(jsonString));
    jsonReader.setLenient(true);

    try {
        switch (resultType) {
        case TRANSFER_TAG_LIST:
            jacksonCollectionType = new TypeReference<Collection<TransferTagImpl>>() {
            };
            return TransferTagSerializer.fromCollectionJson(jsonString, jacksonCollectionType);
        case TRANSFER_TAG_VALUE_LIST:
            jacksonCollectionType = new TypeReference<Collection<TransferTagValueImpl>>() {
            };
            return TransferTagSerializer.fromCollectionJson(jsonString, jacksonCollectionType);
        case SUPERVISION_EVENT_LIST:
            collectionType = new TypeToken<Collection<SupervisionEventImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_DAQ_XML:
            collectionType = new TypeToken<Collection<ProcessXmlResponseImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_TAG_CONFIGURATION_LIST:
            collectionType = new TypeToken<Collection<TagConfigImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_CONFIGURATION_REPORT_HEADER:
            collectionType = new TypeToken<Collection<ConfigurationReportHeader>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_CONFIGURATION_REPORT:
            collectionType = new TypeToken<Collection<ConfigurationReport>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_ACTIVE_ALARM_LIST:
            collectionType = new TypeToken<Collection<AlarmValueImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_ALARM_LIST:
            collectionType = new TypeToken<Collection<AlarmValueImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_COMMAND_HANDLES_LIST:
            collectionType = new TypeToken<Collection<CommandTagHandleImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_COMMAND_REPORT:
            collectionType = new TypeToken<Collection<CommandReportImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_PROCESS_NAMES:
            collectionType = new TypeToken<Collection<ProcessNameResponseImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_DEVICE_CLASS_NAMES:
            collectionType = new TypeToken<Collection<DeviceClassNameResponseImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_DEVICE_LIST:
            collectionType = new TypeToken<Collection<TransferDeviceImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        case TRANSFER_TAG_STATISTICS:
            collectionType = new TypeToken<Collection<TagStatisticsResponseImpl>>() {
            }.getType();
            return getGson().fromJson(jsonReader, collectionType);
        default:
            throw new JsonSyntaxException("Unknown result type specified");
        }
    } finally {
        try {
            jsonReader.close();
        } catch (IOException e) {
        }
    }
}