List of usage examples for com.google.gson JsonElement getAsBoolean
public boolean getAsBoolean()
From source file:de.azapps.mirakel.model.task.Task.java
License:Open Source License
/** * Parses a JSON-Object to a task//from www. j a va 2 s . com * * @param el * @return */ public static Task parse_json(JsonObject el, AccountMirakel account) { Task t = null; JsonElement id = el.get("id"); if (id != null) { t = Task.get(id.getAsLong()); } else { id = el.get("uuid"); if (id != null) t = Task.getByUUID(id.getAsString()); } if (t == null) { t = new Task(); } // Name Set<Entry<String, JsonElement>> entries = el.entrySet(); for (Entry<String, JsonElement> entry : entries) { String key = entry.getKey(); JsonElement val = entry.getValue(); if (key == null || key.equals("id")) continue; if (key.equals("uuid")) { t.setUUID(val.getAsString()); } else if (key.equals("name") || key.equals("description")) { t.setName(val.getAsString()); } else if (key.equals("content")) { String content = val.getAsString(); if (content == null) content = ""; t.setContent(content); } else if (key.equals("priority")) { String prioString = val.getAsString().trim(); if (prioString.equals("L") && t.getPriority() != -1) { t.setPriority(-2); } else if (prioString.equals("M")) { t.setPriority(1); } else if (prioString.equals("H")) { t.setPriority(2); } else if (!prioString.equals("L")) { t.setPriority(val.getAsInt()); } } else if (key.equals("list_id")) { ListMirakel list = ListMirakel.getList(val.getAsInt()); if (list == null) list = SpecialList.firstSpecial().getDefaultList(); t.setList(list); } else if (key.equals("project")) { ListMirakel list = ListMirakel.findByName(val.getAsString()); if (list == null || list.getAccount().getId() != account.getId()) { list = ListMirakel.newList(val.getAsString(), ListMirakel.SORT_BY_OPT, account); } t.setList(list); } else if (key.equals("created_at")) { t.setCreatedAt(val.getAsString().replace(":", "")); } else if (key.equals("updated_at")) { t.setUpdatedAt(val.getAsString().replace(":", "")); } else if (key.equals("entry")) { t.setCreatedAt(parseDate(val.getAsString(), context.getString(R.string.TWDateFormat))); } else if (key.equals("modification")) { t.setUpdatedAt(parseDate(val.getAsString(), context.getString(R.string.TWDateFormat))); } else if (key.equals("done")) { t.setDone(val.getAsBoolean()); } else if (key.equals("status")) { String status = val.getAsString(); if (status.equals("pending")) { t.setDone(false); } else if (status.equals("deleted")) { t.setSyncState(SYNC_STATE.DELETE); } else { t.setDone(true); } t.addAdditionalEntry(key, val.getAsString()); // TODO don't ignore waiting and recurring!!! } else if (key.equals("due")) { Calendar due = parseDate(val.getAsString(), "yyyy-MM-dd"); if (due == null) { due = parseDate(val.getAsString(), context.getString(R.string.TWDateFormat)); if (val.getAsString().contains("T230000Z")) { // Hope this works around a tw-bug // if you add a task with due:today, the task which will be sync will be // due:yesterday:23:00 due.add(Calendar.DAY_OF_MONTH, 1); } } t.setDue(due); } else if (key.equals("reminder")) { Calendar reminder = parseDate(val.getAsString(), "yyyy-MM-dd"); if (reminder == null) { reminder = parseDate(val.getAsString(), context.getString(R.string.TWDateFormat)); } t.setReminder(reminder); } else if (key.equals("annotations")) { String content = ""; try { JsonArray annotations = val.getAsJsonArray(); boolean first = true; for (JsonElement a : annotations) { if (first) first = false; else content += "\n"; content += a.getAsJsonObject().get("description").getAsString(); } } catch (Exception e) { Log.e(TAG, "cannot parse json"); } t.setContent(content); } else if (key.equals("content")) { t.setContent(val.getAsString()); } else if (key.equals("sync_state")) { t.setSyncState(SYNC_STATE.parseInt(val.getAsInt())); } else if (key.equals("depends")) { t.setDependencies(val.getAsString().split(",")); } else { t.addAdditionalEntry(key, val.getAsString()); } } if (t.getList() == null) { ListMirakel l = MirakelPreferences.getImportDefaultList(true); t.setList(l); } return t; }
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
@Override public Task deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonObject el = json.getAsJsonObject(); Optional<ListMirakel> taskList = absent(); final JsonElement id = el.get("id"); if (id == null) { throw new JsonParseException("Json malformed"); }// w ww . java 2 s. c om final Task task = Task.get(id.getAsLong()).or(new Task()); // Name final Set<Entry<String, JsonElement>> entries = el.entrySet(); for (final Entry<String, JsonElement> entry : entries) { final String key = entry.getKey(); final JsonElement val = entry.getValue(); if ((key == null) || "id".equalsIgnoreCase(key)) { continue; } switch (key.toLowerCase()) { case "name": task.setName(val.getAsString()); break; case "content": String content = val.getAsString(); if (content == null) { content = ""; } task.setContent(content); break; case "priority": task.setPriority((int) val.getAsFloat()); break; case "progress": task.setProgress((int) val.getAsDouble()); break; case "list_id": taskList = ListMirakel.get(val.getAsInt()); if (!taskList.isPresent()) { taskList = fromNullable(SpecialList.firstSpecialSafe().getDefaultList()); } break; case "created_at": try { task.setCreatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", ""))); } catch (final ParseException e) { Log.wtf(TAG, "invalid dateformat: ", e); } break; case "updated_at": try { task.setUpdatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", ""))); } catch (final ParseException e) { Log.wtf(TAG, "invalid dateformat: ", e); } break; case "done": task.setDone(val.getAsBoolean()); break; case "due": try { task.setDue(of(DateTimeHelper.createLocalCalendar(val.getAsLong()))); } catch (final NumberFormatException ignored) { task.setDue(Optional.<Calendar>absent()); } break; case "reminder": try { task.setReminder(of(DateTimeHelper.createLocalCalendar(val.getAsLong()))); } catch (final NumberFormatException ignored) { task.setReminder(Optional.<Calendar>absent()); } break; case "tags": handleTags(task, val); break; case "sync_state": task.setSyncState(DefinitionsHelper.SYNC_STATE.valueOf((short) val.getAsFloat())); break; case "show_recurring": task.setIsRecurringShown(val.getAsBoolean()); break; default: handleAdditionalEntries(task, key, val); break; } } if (!taskList.isPresent()) { taskList = of(ListMirakel.safeFirst()); } task.setList(taskList.get(), true); return task; }
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) { if (val.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) val; if (p.isBoolean()) { t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean())); } else if (p.isNumber()) { t.addAdditionalEntry(key, String.valueOf(val.getAsInt())); } else if (p.isJsonNull()) { t.addAdditionalEntry(key, "null"); } else if (p.isString()) { t.addAdditionalEntry(key, '"' + val.getAsString() + '"'); } else {/*from ww w. ja v a2 s . co m*/ Log.w(TAG, "unknown json-type"); } } else if (val.isJsonArray()) { final JsonArray a = (JsonArray) val; StringBuilder s = new StringBuilder("["); boolean first = true; for (final JsonElement e : a) { if (e.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) e; final String add; if (p.isBoolean()) { add = String.valueOf(p.getAsBoolean()); } else if (p.isNumber()) { add = String.valueOf(p.getAsInt()); } else if (p.isString()) { add = '"' + p.getAsString() + '"'; } else if (p.isJsonNull()) { add = "null"; } else { Log.w(TAG, "unknown json-type"); break; } s.append(first ? "" : ",").append(add); first = false; } else { Log.w(TAG, "unknown json-type"); } } t.addAdditionalEntry(key, s + "]"); } else { Log.w(TAG, "unknown json-type"); } }
From source file:de.baydev.hueemulator.network.resources.service.ConfigService.java
License:Open Source License
public Object updateConfig(String data) { LOGGER.info("putState called"); List<HueResponse> responses = new ArrayList<>(); if (data != null) { LOGGER.log(Level.INFO, "data={0}", data); HueConfig config = getConfig();//from www. java 2 s . co m JsonUtil.toJsonObject(data).entrySet().forEach(k -> { JsonElement val = k.getValue(); switch (k.getKey()) { case "name": config.setName(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/name", config.getName())); break; case "proxyaddress": config.setProxyaddress(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/proxyaddress", config.getProxyaddress())); break; case "proxyport": config.setProxyport(val.getAsInt()); responses.addAll(responseSimpleSuccess("/config/proxyport", config.getProxyport())); break; case "swupdate": config.setSwupdate(JsonUtil.fromJson(val.getAsJsonObject().toString(), HueSwUpdate.class)); responses.addAll(responseSimpleSuccess("/config/swupdate", config.getSwupdate())); break; case "linkbutton": config.setLinkbutton(val.getAsBoolean()); responses.addAll(responseSimpleSuccess("/config/linkbutton", config.getLinkbutton())); break; case "ipaddress": config.setIpaddress(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/ipaddress", config.getIpaddress())); break; case "netmask": config.setNetmask(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/netmask", config.getNetmask())); break; case "gateway": config.setGateway(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/gateway", config.getGateway())); break; case "dhcp": config.setDhcp(val.getAsBoolean()); responses.addAll(responseSimpleSuccess("/config/dhcp", config.getDhcp())); break; case "UTC": config.setUTC(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/UTC", config.getUTC())); break; case "timezone": config.setTimezone(val.getAsString()); responses.addAll(responseSimpleSuccess("/config/timezone", config.getTimezone())); break; case "touchlink": /** * TODO */ break; default: } }); HueLoader.getInstance().getBridge().setConfig(config); } return responses; }
From source file:de.baydev.hueemulator.network.resources.service.ResourceLinkService.java
License:Open Source License
public List<HueResponse> putAttributes(String id, String data) { List<HueResponse> responses = new ArrayList<>(); HueResourceLink item = manager.getItems().get(id); if (item != null) { JsonUtil.toJsonObject(data).entrySet().forEach(k -> { JsonElement val = k.getValue(); switch (k.getKey()) { case "name": item.setName(val.getAsString()); responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/name", item.getName())); break; case "type": item.setDescription(val.getAsString()); responses.addAll(/*from w w w. jav a 2 s.c o m*/ responseSimpleSuccess("/resourcelinks/ " + id + "/description", item.getDescription())); break; case "classid": item.setClassid(val.getAsInt()); responses .addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/classid", item.getClassid())); break; case "owner": item.setOwner(val.getAsString()); responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/owner", item.getOwner())); break; case "recycle": item.setRecycle(val.getAsBoolean()); responses .addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/recycle", item.getRecycle())); break; case "links": item.setLinks(Arrays.asList(JsonUtil.fromJson(val.getAsString(), String[].class))); responses.addAll(responseSimpleSuccess("/resourcelinks/ " + id + "/links", item.getLinks())); break; } }); manager.onChange(id, item); } return responses; }
From source file:de.baydev.hueemulator.network.resources.service.ScheduleService.java
License:Open Source License
public List<HueResponse> putAttributes(String id, String data) { HueSchedule item = manager.getItems().get(id); List<HueResponse> responses = new ArrayList<>(); if (item != null) { JsonUtil.toJsonObject(data).entrySet().forEach(k -> { JsonElement val = k.getValue(); switch (k.getKey()) { case "name": item.setName(val.getAsString()); responses.addAll(responseSimpleSuccess("/schedules/ " + id + "/name", item.getName())); break; case "description": item.setDescription(val.getAsString()); responses.addAll(/*from ww w . j a v a 2 s . c o m*/ responseSimpleSuccess("/schedules/ " + id + "/description", item.getDescription())); break; case "command": item.setCommand(JsonUtil.fromJson(val.getAsJsonObject().toString(), HueCommand.class)); responses.addAll(responseSimpleSuccess("/schedules/ " + id + "/command", item.getCommand())); break; case "time": item.setTime(val.getAsString()); responses.addAll(responseSimpleSuccess("/schedules/ " + id + "/time", item.getTime())); break; case "localtime": item.setLocaltime(val.getAsString()); responses .addAll(responseSimpleSuccess("/schedules/ " + id + "/localtime", item.getLocaltime())); break; case "status": item.setStatus(val.getAsString()); responses.addAll(responseSimpleSuccess("/schedules/ " + id + "/status", item.getStatus())); break; case "autodelete": item.setAutodelete(val.getAsBoolean()); responses.addAll( responseSimpleSuccess("/schedules/ " + id + "/autodelete", item.getAutodelete())); break; } }); manager.onChange(id, item); } return responses; }
From source file:de.baydev.hueemulator.network.resources.service.SensorService.java
License:Open Source License
public List<HueResponse> updateConfig(String id, String data) { HueSensor sensor = manager.getItems().get(id); List<HueResponse> responses = new ArrayList<>(); if (sensor != null) { HueSensorConfig item = sensor.getConfig(); JsonUtil.toJsonObject(data).entrySet().forEach(k -> { JsonElement val = k.getValue(); switch (k.getKey()) { case "battery": item.setBattery(val.getAsInt()); responses.addAll(// w w w .ja v a2 s . c o m responseSimpleSuccess("/sensors/ " + id + "/config/battery", item.getBattery())); break; case "lat": item.setLatitude(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/config/lat", item.getLatitude())); break; case "long": item.setLongitude(val.getAsString()); responses .addAll(responseSimpleSuccess("/sensors/ " + id + "/config/long", item.getLongitude())); break; case "on": item.setOn(val.getAsBoolean()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/config/on", item.getOn())); break; case "reachable": item.setReachable(val.getAsBoolean()); responses.addAll( responseSimpleSuccess("/sensors/ " + id + "/config/reachable", item.getReachable())); break; case "sunriseoffset": item.setSunriseoffset(val.getAsInt()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/config/sunriseoffset", item.getSunriseoffset())); break; case "sunsetoffset": item.setSunsetoffset(val.getAsInt()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/config/sunsetoffset", item.getSunsetoffset())); break; default: } }); sensor.setConfig(item); manager.onChange(id, sensor); } return responses; }
From source file:de.baydev.hueemulator.network.resources.service.SensorService.java
License:Open Source License
public List<HueResponse> putAttributes(String id, String data) { HueSensor item = manager.getItems().get(id); List<HueResponse> responses = new ArrayList<>(); if (item != null) { JsonUtil.toJsonObject(data).entrySet().forEach(k -> { JsonElement val = k.getValue(); switch (k.getKey()) { case "name": item.setName(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/name", item.getName())); break; case "modelid": item.setModelid(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/modelid", item.getModelid())); break; case "swversion": item.setSwversion(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/swversion", item.getSwversion())); break; case "type": item.setType(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/type", item.getType())); break; case "uniqueid": item.setUniqueId(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/uniqueid", item.getUniqueId())); break; case "manufacturername": item.setManufacturername(val.getAsString()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/manufacturername", item.getManufacturername())); break; case "state": item.setState(JsonUtil.fromJson(val.getAsString(), HueSensorState.class)); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/state", item.getState())); break; case "config": item.setConfig(JsonUtil.fromJson(val.getAsString(), HueSensorConfig.class)); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/config", item.getConfig())); break; case "recycle": item.setRecycle(val.getAsBoolean()); responses.addAll(responseSimpleSuccess("/sensors/ " + id + "/recycle", item.getRecycle())); break; default: }// ww w. j a va 2 s.c o m }); manager.onChange(id, item); } return responses; }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static boolean getBoolVal(JsonElement json) { if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); }//from w ww .j a v a 2 s . c o m if (!json.isJsonPrimitive()) { throw new JsonSyntaxException("Expected value to be a primitive"); } return json.getAsBoolean(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static boolean getBoolVal(JsonElement json, boolean defVal) { if (json == null || !json.isJsonPrimitive()) { return defVal; }//from w w w. j a va 2 s .co m return json.getAsBoolean(); }