protected static Colony fromJSON(JSONObject json) throws JSONException { Colony colony = new Colony(json.getString("id")); colony.setX(json.getDouble("x")); colony.setY(json.getDouble("y")); colony.setAttribute("census.visited", json.optBoolean("visited", false)); colony.setAttribute("census.active", json.optBoolean("active", false)); final Object updatedObject = json.opt("modified"); if (updatedObject == null || JSONObject.NULL.equals(updatedObject) || !(updatedObject instanceof String)) { // Set to now colony.setUpdateTime(DateTime.now()); } else { try { final DateTime updatedTime = ISODateTimeFormat.dateTimeParser().parseDateTime((String) updatedObject); colony.setUpdateTime(updatedTime); } catch (IllegalArgumentException e) { // Could not parse time // Set to now colony.setUpdateTime(DateTime.now()); } } return colony; }
protected static JSONObject toJSON(Colony value) throws JSONException { final JSONObject json = new JSONObject(); // Put the ID as an integer if it is valid try { json.put("id", Integer.valueOf(value.getID())); } catch (NumberFormatException e) { json.put("id", value.getID()); } json.put("x", value.getX()); json.put("y", value.getY()); if (value.hasAttribute("census.visited")) { json.put("visited", value.getAttribute("census.visited")); } if (value.hasAttribute("census.active")) { json.put("active", value.getAttribute("census.active")); } final String updatedString = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC().print(value.getUpdateTime()); json.put("modified", updatedString); return json; }