diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java')
-rw-r--r-- | mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java new file mode 100644 index 000000000..4ec98ec9e --- /dev/null +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/JSONUtils.java @@ -0,0 +1,69 @@ +/* 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.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.os.Bundle; +import android.util.Log; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +public final class JSONUtils { + private static final String LOGTAG = "GeckoJSONUtils"; + + private JSONUtils() {} + + 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); + } + } + + public static JSONObject bundleToJSON(Bundle bundle) { + if (bundle == null || bundle.isEmpty()) { + return null; + } + + JSONObject json = new JSONObject(); + for (String key : bundle.keySet()) { + try { + json.put(key, bundle.get(key)); + } catch (JSONException e) { + Log.w(LOGTAG, "Error building JSON response.", e); + } + } + + return json; + } + + // Handles conversions between a JSONArray and a Set<String> + public static Set<String> parseStringSet(JSONArray json) { + final Set<String> ret = new HashSet<String>(); + + for (int i = 0; i < json.length(); i++) { + try { + ret.add(json.getString(i)); + } catch (JSONException ex) { + Log.i(LOGTAG, "Error parsing json", ex); + } + } + + return ret; + } + +} |