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) {
}
}
}
}
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());
}
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());
}
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 "));
}
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();
}
Aggregations