/** * Turns a User into a JSON object. * * @return obj JSON object */ public JSONObject toJSONObject() throws ParseException { JSONObject obj = new JSONObject(); obj.put(KEY_FIRST_NAME, getFirstName()); obj.put(KEY_LAST_NAME, getLastName()); obj.put(KEY_ID, getID()); obj.put(KEY_POINTS, getPoints()); Contestant c = getWeeklyPick(); if (c == null) { c = new Contestant(); c.setNull(); } obj.put(KEY_WEEKLY_PICK_ID, c.getID()); c = getUltimatePick(); if (c == null) { c = new Contestant(); c.setNull(); } obj.put(KEY_ULT_PICK_ID, c.getID()); obj.put(KEY_WIN_PICK_POINTS, new Integer(getUltimatePoints())); obj.put(KEY_NUM_BONUS_ANSWER, getNumBonusAnswer()); return obj; }
/** * Turns a JSON object into a User. * * @param o JSON object */ public void fromJSONObject(JSONObject o) { try { setFirstName((String) o.remove(KEY_FIRST_NAME)); setLastName((String) o.remove(KEY_LAST_NAME)); setID((String) o.remove(KEY_ID)); setPoints(Utils.numToInt(o.remove(KEY_POINTS))); String id = (String) o.remove(KEY_WEEKLY_PICK_ID); Contestant c = null; GameData g = GameData.getCurrentGame(); if (id.equals(Contestant.NULL_ID)) { c = new Contestant(); c.setNull(); } else { c = g.getContestant(id); } setWeeklyPick(c); id = (String) o.remove(KEY_ULT_PICK_ID); if (id.equals(Contestant.NULL_ID)) { c = new Contestant(); c.setNull(); } else { c = g.getContestant(id); } setUltimatePick(c); setUltimatePoints(Utils.numToInt(o.remove(KEY_WIN_PICK_POINTS))); setNumBonusAnswer(((Number) o.remove(KEY_NUM_BONUS_ANSWER)).intValue()); } catch (InvalidFieldException e) { System.out.println("Warning: InvalidFieldException in fromJSONObject"); e.printStackTrace(); } }