Bug 1244944 - Don't stick an org.json.JSONArray into an ExtendedJSONObject. r?rnewman draft
authorNick Alexander <nalexander@mozilla.com>
Mon, 01 Feb 2016 16:57:24 -0800
changeset 327806 d7529a9f8fadba0d30324ef2ca93be0999b71203
parent 327805 34d94568c1c6ee6c2b571a56a5a36b371a9115c2
child 513764 cd1e9c2fe42966598b97c27a920016b52e9e1455
push id10307
push usernalexander@mozilla.com
push dateTue, 02 Feb 2016 01:08:05 +0000
reviewersrnewman
bugs1244944
milestone47.0a1
Bug 1244944 - Don't stick an org.json.JSONArray into an ExtendedJSONObject. r?rnewman This works due to string conversions, but it's not elegant. Let's just define the API we want, and work to improve the implementation when we remove org.json.simple entirely.
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
mobile/android/services/src/main/java/org/mozilla/gecko/sync/ExtendedJSONObject.java
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryPingGenerator.java
@@ -2,27 +2,28 @@
  * 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.telemetry;
 
 import android.content.Context;
 import android.os.Build;
-import java.io.IOException;
-import java.util.Locale;
 
 import com.keepsafe.switchboard.SwitchBoard;
-import org.json.JSONArray;
+
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.Locales;
 import org.mozilla.gecko.sync.ExtendedJSONObject;
 import org.mozilla.gecko.telemetry.TelemetryConstants.CorePing;
 import org.mozilla.gecko.util.StringUtils;
 
+import java.io.IOException;
+import java.util.Locale;
+
 /**
  * A class with static methods to generate the various Java-created Telemetry pings to upload to the telemetry server.
  */
 public class TelemetryPingGenerator {
 
     // In the server url, the initial path directly after the "scheme://host:port/"
     private static final String SERVER_INITIAL_PATH = "submit/telemetry";
 
@@ -81,20 +82,13 @@ public class TelemetryPingGenerator {
 
         ping.put(CorePing.ARCHITECTURE, AppConstants.ANDROID_CPU_ARCH);
         ping.put(CorePing.CLIENT_ID, clientId);
         ping.put(CorePing.DEVICE, deviceDescriptor);
         ping.put(CorePing.LOCALE, Locales.getLanguageTag(Locale.getDefault()));
         ping.put(CorePing.OS_VERSION, Integer.toString(Build.VERSION.SDK_INT)); // A String for cross-platform reasons.
         ping.put(CorePing.SEQ, seq);
         if (AppConstants.MOZ_SWITCHBOARD) {
-            ping.put(CorePing.EXPERIMENTS, getActiveExperiments(context));
+            ping.putArray(CorePing.EXPERIMENTS, SwitchBoard.getActiveExperiments(context));
         }
         return ping;
     }
-
-    private static JSONArray getActiveExperiments(final Context context) {
-        if (!AppConstants.MOZ_SWITCHBOARD) {
-            throw new IllegalStateException("This method should not be called with switchboard disabled");
-        }
-        return new JSONArray(SwitchBoard.getActiveExperiments(context));
-    }
 }
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/ExtendedJSONObject.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/ExtendedJSONObject.java
@@ -295,16 +295,24 @@ public class ExtendedJSONObject {
   public void put(String key, ExtendedJSONObject value) {
     this.putRaw(key, value);
   }
 
   public void put(String key, JSONArray value) {
     this.putRaw(key, value);
   }
 
+  @SuppressWarnings("unchecked")
+  public void putArray(String key, List<String> value) {
+    // Frustratingly inefficient, but there you have it.
+    final JSONArray jsonArray = new JSONArray();
+    jsonArray.addAll(value);
+    this.putRaw(key, jsonArray);
+  }
+
   /**
    * Remove key-value pair from JSONObject.
    *
    * @param key
    *          to be removed.
    * @return true if key exists and was removed, false otherwise.
    */
   public boolean remove(String key) {