correctly serialize primitive arrays
MODIFY
analytics/src/main/java/com/segment/analytics/Cartographer.java
analytics/src/main/java/com/segment/analytics/Cartographer.java
@@ -32,6 +32,7 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -190,11 +191,15 @@ public class Cartographer {
writer.endArray();
}
- /** Print the json representation of an array to the given writer. */
- private static void arrayToWriter(Object[] array, JsonWriter writer) throws IOException {
+ /**
+ * Print the json representation of an array to the given writer. Primitive arrays cannot be cast
+ * to Object[], to this method accepts the raw object and uses {@link Array#getLength(Object)} and
+ * {@link Array#get(Object, int)} to read the array.
+ */
+ private static void arrayToWriter(Object array, JsonWriter writer) throws IOException {
writer.beginArray();
- for (Object value : array) {
- writeValue(value, writer);
+ for (int i = 0, size = Array.getLength(array); i < size; i++) {
+ writeValue(Array.get(array, i), writer);
}
writer.endArray();
}
@@ -216,7 +221,7 @@ public class Cartographer {
} else if (value instanceof Map) {
mapToWriter((Map) value, writer);
} else if (value.getClass().isArray()) {
- arrayToWriter((Object[]) value, writer);
+ arrayToWriter(value, writer);
} else {
writer.value(String.valueOf(value));
}

MODIFY
analytics/src/test/java/com/segment/analytics/CartographerTest.java
analytics/src/test/java/com/segment/analytics/CartographerTest.java
@@ -245,6 +245,17 @@ public class CartographerTest {
+ "}");
}
+ @Test
+ public void encodesPrimitiveArrays() throws IOException {
+ // Exercise a bug where primitive arrays would throw an IOException.
+ // https://github.com/segmentio/analytics-android/issues/507
+ Map<String, Object> map =
+ ImmutableMap.<String, Object>builder().put("a", new int[] {1, 2}).build();
+
+ assertThat(cartographer.toJson(map))
+ .isEqualTo("{\n" + " \"a\": [\n" + " 1,\n" + " 2\n" + " ]\n" + "}");
+ }
+
@Test
public void decodesArraysWithArraysAsLists() throws IOException {
String json =

correctly serialize primitive arrays