Search in sources :

Example 86 with JsonObject

use of com.google.gson.JsonObject in project openhab1-addons by openhab.

the class MystromClient method getDevices.

private List<MystromDevice> getDevices(boolean minimalMode) {
    Reader reader = null;
    logger.debug("get all devices state");
    try {
        String url = API_URL + "devices" + "?authToken=" + this.authToken;
        if (minimalMode) {
            url = url + "&minimal=true";
        }
        HttpURLConnection httpURLConnection;
        httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
        httpURLConnection.connect();
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            logger.error("Get devices http code: '{}'", responseCode);
            return new ArrayList<MystromDevice>();
        }
        InputStream inputStream = httpURLConnection.getInputStream();
        reader = new InputStreamReader(inputStream, "UTF-8");
        JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
        String status = jsonObject.get("status").getAsString();
        if (!status.equals("ok")) {
            logger.error("Error while getting devices: '{}'", status);
            return new ArrayList<MystromDevice>();
        }
        GetDevicesResult result = gson.fromJson(jsonObject, GetDevicesResult.class);
        logger.debug("Devices discovery sucessfull, found '{}' devices", result.devices.size());
        return result.devices;
    } catch (Exception ex) {
        logger.error("Error getting devices: '{}'", ex.toString());
        return new ArrayList<MystromDevice>();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice) HttpURLConnection(java.net.HttpURLConnection) GetDevicesResult(org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult)

Example 87 with JsonObject

use of com.google.gson.JsonObject in project iosched by google.

the class DataModelHelperTest method testSetSimple.

/**
   * Test method for {@link com.google.iosched.model.DataModelHelper#set(com.google.gson.JsonObject, java.lang.Enum, com.google.gson.JsonObject, java.lang.Enum, com.google.iosched.model.validator.Converter)}.
   * @throws IOException
   */
@Test
public void testSetSimple() throws IOException {
    JsonObject dest = new JsonObject();
    String originalValue = "testvalue";
    Enum<?> destKey = InputJsonKeys.VendorAPISource.Rooms.Name;
    JsonPrimitive value = new JsonPrimitive(originalValue);
    DataModelHelper.set(value, dest, destKey);
    assertEquals(1, dest.entrySet().size());
    assertEquals(originalValue, dest.get(destKey.name()).getAsString());
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 88 with JsonObject

use of com.google.gson.JsonObject in project iosched by google.

the class DataModelHelperTest method testSetDateFromComplex.

/**
   * Test method for {@link com.google.iosched.model.DataModelHelper#set(com.google.gson.JsonObject, java.lang.Enum, com.google.gson.JsonObject, java.lang.Enum, com.google.iosched.model.validator.Converter)}.
   * @throws IOException
   */
@Test
public void testSetDateFromComplex() throws IOException {
    JsonObject src = (JsonObject) TestHelper.readJsonTestDataFile("sample_topic.json");
    JsonObject dest = new JsonObject();
    Enum<?> srcKey = InputJsonKeys.VendorAPISource.Topics.Start;
    Enum<?> destKey = InputJsonKeys.VendorAPISource.Topics.Start;
    DataModelHelper.set(src, srcKey, dest, destKey, Converters.DATETIME);
    assertEquals("2013-05-16T21:00:00Z", dest.get(destKey.name()).getAsString());
}
Also used : JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 89 with JsonObject

use of com.google.gson.JsonObject in project iosched by google.

the class DataModelTest method testExtractRooms.

/**
   * Test method for {@link com.google.iosched.model.DataExtractor#extractRooms(com.google.iosched.model.JsonDataSources)}.
   */
@Test
public void testExtractRooms() {
    JsonArray rooms = new DataExtractor(false).extractRooms(sources);
    assertEquals(8, rooms.size());
    JsonObject firstRoom = rooms.get(0).getAsJsonObject();
    String roomName = firstRoom.get(OutputJsonKeys.Rooms.name.name()).getAsString();
    assertEquals(true, roomName.startsWith("Room "));
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 90 with JsonObject

use of com.google.gson.JsonObject in project iosched by google.

the class APIExtractor method run.

public void run(OutputStream optionalOutput, boolean extractUnpublished) throws IOException {
    // fill sources with extra input:
    JsonDataSources sources = new ExtraInput().fetchAllDataSources();
    // fill sources with vendor API input:
    VendorDynamicInput vendorInput = new VendorDynamicInput();
    vendorInput.setExtractUnpublished(extractUnpublished);
    sources.putAll(vendorInput.fetchAllDataSources());
    // extract session data from inputs:
    JsonObject newData = new DataExtractor(false).extractFromDataSources(sources);
    // send data to the outputstream
    Writer writer = Channels.newWriter(Channels.newChannel(optionalOutput), "UTF-8");
    JsonWriter optionalOutputWriter = new JsonWriter(writer);
    optionalOutputWriter.setIndent("  ");
    new Gson().toJson(newData, optionalOutputWriter);
    optionalOutputWriter.flush();
}
Also used : JsonDataSources(com.google.samples.apps.iosched.server.schedule.model.JsonDataSources) ExtraInput(com.google.samples.apps.iosched.server.schedule.server.input.ExtraInput) DataExtractor(com.google.samples.apps.iosched.server.schedule.model.DataExtractor) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) VendorDynamicInput(com.google.samples.apps.iosched.server.schedule.server.input.VendorDynamicInput) JsonWriter(com.google.gson.stream.JsonWriter) Writer(java.io.Writer) JsonWriter(com.google.gson.stream.JsonWriter)

Aggregations

JsonObject (com.google.gson.JsonObject)1417 JsonElement (com.google.gson.JsonElement)389 JsonArray (com.google.gson.JsonArray)293 JsonParser (com.google.gson.JsonParser)285 JsonPrimitive (com.google.gson.JsonPrimitive)137 Gson (com.google.gson.Gson)91 Test (org.junit.Test)81 HashMap (java.util.HashMap)79 Map (java.util.Map)78 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)66 Test (org.testng.annotations.Test)61 InputStreamReader (java.io.InputStreamReader)38 JsonParseException (com.google.gson.JsonParseException)27 File (java.io.File)25 List (java.util.List)21 HttpResponse (org.apache.http.HttpResponse)21 JsonReader (com.google.gson.stream.JsonReader)19 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 InputStream (java.io.InputStream)19