private static String serializeToJson(LikeActionController controller) { JSONObject controllerJson = new JSONObject(); try { controllerJson.put(JSON_INT_VERSION_KEY, LIKE_ACTION_CONTROLLER_VERSION); controllerJson.put(JSON_STRING_OBJECT_ID_KEY, controller.objectId); controllerJson.put(JSON_INT_OBJECT_TYPE_KEY, controller.objectType.getValue()); controllerJson.put( JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY, controller.likeCountStringWithLike); controllerJson.put( JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY, controller.likeCountStringWithoutLike); controllerJson.put( JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY, controller.socialSentenceWithLike); controllerJson.put( JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY, controller.socialSentenceWithoutLike); controllerJson.put(JSON_BOOL_IS_OBJECT_LIKED_KEY, controller.isObjectLiked); controllerJson.put(JSON_STRING_UNLIKE_TOKEN_KEY, controller.unlikeToken); if (controller.facebookDialogAnalyticsBundle != null) { JSONObject analyticsJSON = BundleJSONConverter.convertToJSON( controller.facebookDialogAnalyticsBundle); if (analyticsJSON != null) { controllerJson.put( JSON_BUNDLE_FACEBOOK_DIALOG_ANALYTICS_BUNDLE, analyticsJSON); } } } catch (JSONException e) { Log.e(TAG, "Unable to serialize controller to JSON", e); return null; } return controllerJson.toString(); }
private static LikeActionController deserializeFromJson(String controllerJsonString) { LikeActionController controller; try { JSONObject controllerJson = new JSONObject(controllerJsonString); int version = controllerJson.optInt(JSON_INT_VERSION_KEY, -1); if (version != LIKE_ACTION_CONTROLLER_VERSION) { // Don't attempt to deserialize a controller that might be serialized differently // than expected. return null; } String objectId = controllerJson.getString(JSON_STRING_OBJECT_ID_KEY); int objectTypeInt = controllerJson.optInt( JSON_INT_OBJECT_TYPE_KEY, LikeView.ObjectType.UNKNOWN.getValue()); controller = new LikeActionController( objectId, LikeView.ObjectType.fromInt(objectTypeInt)); // Make sure to default to null and not empty string, to keep the logic elsewhere // functioning properly. controller.likeCountStringWithLike = controllerJson.optString(JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY, null); controller.likeCountStringWithoutLike = controllerJson.optString(JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY, null); controller.socialSentenceWithLike = controllerJson.optString(JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY, null); controller.socialSentenceWithoutLike = controllerJson.optString(JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY, null); controller.isObjectLiked = controllerJson.optBoolean(JSON_BOOL_IS_OBJECT_LIKED_KEY); controller.unlikeToken = controllerJson.optString(JSON_STRING_UNLIKE_TOKEN_KEY, null); JSONObject analyticsJSON = controllerJson.optJSONObject( JSON_BUNDLE_FACEBOOK_DIALOG_ANALYTICS_BUNDLE); if (analyticsJSON != null) { controller.facebookDialogAnalyticsBundle = BundleJSONConverter.convertToBundle(analyticsJSON); } } catch (JSONException e) { Log.e(TAG, "Unable to deserialize controller from JSON", e); controller = null; } return controller; }