Bug 834033 - Part 3: Add Android JSONUtils for parsing URLs and UUIDs. r=blassey

This commit is contained in:
Chris Peterson 2013-06-12 10:07:34 -07:00
parent a279deb591
commit f5085029b2
2 changed files with 58 additions and 0 deletions

View File

@ -38,6 +38,7 @@ UTIL_JAVA_FILES := \
util/HardwareUtils.java \
util/INIParser.java \
util/INISection.java \
util/JSONUtils.java \
util/StringUtils.java \
util/ThreadUtils.java \
util/UiAsyncTask.java \

View File

@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.util;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
public final class JSONUtils {
private static final String LOGTAG = "JSONUtils";
private JSONUtils() {}
public static URL getURL(String name, JSONObject json) {
String url = json.optString(name, null);
if (url == null) {
return null;
}
try {
return new URL(url);
} catch (MalformedURLException e) {
Log.e(LOGTAG, "", new IllegalStateException(name + "=" + url, e));
return null;
}
}
public static void putURL(String name, URL url, JSONObject json) {
String urlString = url.toString();
try {
json.put(name, urlString);
} catch (JSONException e) {
throw new IllegalArgumentException(name + "=" + urlString, e);
}
}
public static UUID getUUID(String name, JSONObject json) {
String uuid = json.optString(name, null);
return (uuid != null) ? UUID.fromString(uuid) : null;
}
public static void putUUID(String name, UUID uuid, JSONObject json) {
String uuidString = uuid.toString();
try {
json.put(name, uuidString);
} catch (JSONException e) {
throw new IllegalArgumentException(name + "=" + uuidString, e);
}
}
}