Bug 1244944 - Use typed put() variants. r?rnewman draft
authorNick Alexander <nalexander@mozilla.com>
Mon, 01 Feb 2016 16:55:36 -0800
changeset 327802 66c12013e1a5eb8f9a26851565a931c38246e76f
parent 327801 9da41d9ee4b49225cfd8a953ee8be727720b334b
child 327803 9514d4c4a8fcdad570e30b5ff9c45e7721adcfa4
push id10307
push usernalexander@mozilla.com
push dateTue, 02 Feb 2016 01:08:05 +0000
reviewersrnewman
bugs1244944
milestone47.0a1
Bug 1244944 - Use typed put() variants. r?rnewman
mobile/android/services/src/main/java/org/mozilla/gecko/sync/ExtendedJSONObject.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/test/TestExtendedJSONObject.java
--- 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
@@ -1,28 +1,29 @@
 /* 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.sync;
 
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 import org.mozilla.apache.commons.codec.binary.Base64;
 import org.mozilla.gecko.sync.UnexpectedJSONException.BadRequiredFieldJSONException;
 
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
 /**
  * Extend JSONObject to do little things, like, y'know, accessing members.
  *
  * @author rnewman
  *
  */
 public class ExtendedJSONObject {
 
@@ -283,22 +284,46 @@ public class ExtendedJSONObject {
     return this.object.toJSONString();
   }
 
   @Override
   public String toString() {
     return this.object.toString();
   }
 
-  public void put(String key, Object value) {
+  protected void putRaw(String key, Object value) {
     @SuppressWarnings("unchecked")
     Map<Object, Object> map = this.object;
     map.put(key, value);
   }
 
+  public void put(String key, String value) {
+    this.putRaw(key, value);
+  }
+
+  public void put(String key, boolean value) {
+    this.putRaw(key, value);
+  }
+
+  public void put(String key, long value) {
+    this.putRaw(key, value);
+  }
+
+  public void put(String key, int value) {
+    this.putRaw(key, value);
+  }
+
+  public void put(String key, ExtendedJSONObject value) {
+    this.putRaw(key, value);
+  }
+
+  public void put(String key, JSONArray value) {
+    this.putRaw(key, value);
+  }
+
   /**
    * 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) {
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/test/TestExtendedJSONObject.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/test/TestExtendedJSONObject.java
@@ -60,17 +60,17 @@ public class TestExtendedJSONObject {
   }
 
   @Test
   public void testSafeInteger() {
     ExtendedJSONObject o = new ExtendedJSONObject();
     o.put("integer", Integer.valueOf(5));
     o.put("string",  "66");
     o.put("object",  new ExtendedJSONObject());
-    o.put("null",    null);
+    o.put("null", (JSONArray) null);
 
     assertEquals(Integer.valueOf(5),  o.getIntegerSafely("integer"));
     assertEquals(Integer.valueOf(66), o.getIntegerSafely("string"));
     assertNull(o.getIntegerSafely(null));
   }
 
   @Test
   public void testParseJSONArray() throws Exception {