@SuppressWarnings("unchecked") public MobeelizerJsonEntity(final String json) throws JSONException { JSONObject jsonObject = new JSONObject(json); model = jsonObject.getString("model"); guid = jsonObject.getString("guid"); if (jsonObject.has("owner")) { owner = jsonObject.getString("owner"); } if (jsonObject.has("conflictState")) { conflictState = ConflictState.valueOf(jsonObject.getString("conflictState")); } else { conflictState = ConflictState.NO_IN_CONFLICT; } if (jsonObject.has("fields")) { JSONObject jsonFields = jsonObject.getJSONObject("fields"); Iterator<String> keys = jsonFields.keys(); fields = new HashMap<String, String>(); while (keys.hasNext()) { String key = keys.next(); fields.put(key, jsonFields.isNull(key) ? null : jsonFields.getString(key)); } } }
public String getJson() throws JSONException { JSONObject json = new JSONObject(); json.put("model", model); json.put("guid", guid); json.put("resolveConflict", "false"); json.put("owner", owner); json.put( "conflictState", conflictState == null ? ConflictState.NO_IN_CONFLICT : conflictState.name()); if (fields != null) { JSONObject jsonFields = new JSONObject(); for (Map.Entry<String, String> field : fields.entrySet()) { jsonFields.put(field.getKey(), field.getValue()); } json.put("fields", jsonFields); } return json.toString(); }