Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java |
| index 87d7fc583af152a7b4a51bf20d033cb9b9200b91..8db0ade10dd986686969a953df9aa9b6381a0e22 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsResult.java |
| @@ -5,6 +5,9 @@ package org.chromium.chrome.browser.physicalweb; |
| import org.chromium.base.Log; |
| +import org.json.JSONException; |
| +import org.json.JSONObject; |
| + |
| import java.net.MalformedURLException; |
| import java.net.URL; |
| @@ -17,6 +20,13 @@ import java.net.URL; |
| */ |
| class PwsResult { |
| private static final String TAG = "PhysicalWeb"; |
| + private static final String PAGE_INFO_KEY = "pageInfo"; |
| + private static final String REQUEST_URL_KEY = "scannedUrl"; |
| + private static final String SITE_URL_KEY = "resolvedUrl"; |
| + private static final String ICON_KEY = "icon"; |
| + private static final String TITLE_KEY = "title"; |
| + private static final String DESCRIPTION_KEY = "description"; |
| + private static final String GROUP_ID_KEY = "groupId"; |
| /** |
| * The URL that was set in the request to the PWS. |
| @@ -72,4 +82,37 @@ class PwsResult { |
| } |
| this.groupId = groupIdToSet; |
| } |
| + |
| + /** |
| + * Creates a JSON object that represents this data structure. |
| + * @return a JSON serialization of this data structure. |
| + * @throws JSONException if the values cannot be deserialized. |
| + */ |
| + public JSONObject jsonSerialize() throws JSONException { |
| + return new JSONObject() |
| + .put(REQUEST_URL_KEY, requestUrl) |
| + .put(SITE_URL_KEY, siteUrl) |
| + .put(PAGE_INFO_KEY, new JSONObject() |
| + .put(ICON_KEY, iconUrl) |
| + .put(TITLE_KEY, title) |
| + .put(DESCRIPTION_KEY, description) |
| + .put(GROUP_ID_KEY, groupId)); |
|
gone
2016/08/04 21:31:47
nit: Might be nicer if you pulled out the inner cr
|
| + } |
| + |
| + /** |
| + * Populates a PwsResult with data from a given JSON object. |
| + * @param jsonObject a serialized PwsResult. |
| + * @return The PwsResult represented by the serialized object. |
| + * @throws JSONException if the values cannot be serialized. |
| + */ |
| + public static PwsResult jsonDeserialize(JSONObject jsonObject) throws JSONException { |
| + JSONObject pageInfo = jsonObject.getJSONObject(PAGE_INFO_KEY); |
| + return new PwsResult( |
| + jsonObject.getString(REQUEST_URL_KEY), |
| + jsonObject.getString(SITE_URL_KEY), |
| + pageInfo.optString(ICON_KEY, null), |
| + pageInfo.optString(TITLE_KEY, ""), |
| + pageInfo.optString(DESCRIPTION_KEY, null), |
| + pageInfo.optString(GROUP_ID_KEY, null)); |
| + } |
| } |