From ff2f6e18d2210ffaabcfc7b10fa1c1c560b8666a Mon Sep 17 00:00:00 2001 From: Spencer Griffin Date: Wed, 12 Jan 2022 11:42:42 -0700 Subject: [PATCH] Convert ArrayJsonAdapter.java to kotlin (#1468) Co-authored-by: Zac Sweers Co-authored-by: Spencer Griffin --- moshi/japicmp/build.gradle.kts | 3 +- .../com/squareup/moshi/ArrayJsonAdapter.java | 82 -------------- .../com/squareup/moshi/ArrayJsonAdapter.kt | 107 ++++++++++++++++++ .../src/main/java/com/squareup/moshi/Moshi.kt | 2 +- 4 files changed, 110 insertions(+), 84 deletions(-) delete mode 100644 moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java create mode 100644 moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt diff --git a/moshi/japicmp/build.gradle.kts b/moshi/japicmp/build.gradle.kts index c4b2a85..98f21f1 100644 --- a/moshi/japicmp/build.gradle.kts +++ b/moshi/japicmp/build.gradle.kts @@ -37,7 +37,8 @@ val japicmp = tasks.register("japicmp") { ) fieldExcludes = listOf( "com.squareup.moshi.CollectionJsonAdapter#FACTORY", // False-positive, class is not public anyway - "com.squareup.moshi.MapJsonAdapter#FACTORY" // Class is not public + "com.squareup.moshi.MapJsonAdapter#FACTORY", // Class is not public + "com.squareup.moshi.ArrayJsonAdapter#FACTORY" // Class is not public ) } diff --git a/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java deleted file mode 100644 index 92d1463..0000000 --- a/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2014 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.squareup.moshi; - -import java.io.IOException; -import java.lang.annotation.Annotation; -import java.lang.reflect.Array; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import javax.annotation.Nullable; - -/** - * Converts arrays to JSON arrays containing their converted contents. This supports both primitive - * and object arrays. - */ -final class ArrayJsonAdapter extends JsonAdapter { - public static final Factory FACTORY = - new Factory() { - @Override - public @Nullable JsonAdapter create( - Type type, Set annotations, Moshi moshi) { - Type elementType = Types.arrayComponentType(type); - if (elementType == null) return null; - if (!annotations.isEmpty()) return null; - Class elementClass = Types.getRawType(elementType); - JsonAdapter elementAdapter = moshi.adapter(elementType); - return new ArrayJsonAdapter(elementClass, elementAdapter).nullSafe(); - } - }; - - private final Class elementClass; - private final JsonAdapter elementAdapter; - - ArrayJsonAdapter(Class elementClass, JsonAdapter elementAdapter) { - this.elementClass = elementClass; - this.elementAdapter = elementAdapter; - } - - @Override - public Object fromJson(JsonReader reader) throws IOException { - List list = new ArrayList<>(); - reader.beginArray(); - while (reader.hasNext()) { - list.add(elementAdapter.fromJson(reader)); - } - reader.endArray(); - Object array = Array.newInstance(elementClass, list.size()); - for (int i = 0; i < list.size(); i++) { - Array.set(array, i, list.get(i)); - } - return array; - } - - @Override - public void toJson(JsonWriter writer, Object value) throws IOException { - writer.beginArray(); - for (int i = 0, size = Array.getLength(value); i < size; i++) { - elementAdapter.toJson(writer, Array.get(value, i)); - } - writer.endArray(); - } - - @Override - public String toString() { - return elementAdapter + ".array()"; - } -} diff --git a/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt b/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt new file mode 100644 index 0000000..a564894 --- /dev/null +++ b/moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2014 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi + +import java.lang.reflect.Type +import java.lang.reflect.Array as JavaArray + +/** + * Converts arrays to JSON arrays containing their converted contents. This supports both primitive + * and object arrays. + */ +internal class ArrayJsonAdapter( + private val elementClass: Class<*>, + private val elementAdapter: JsonAdapter +) : JsonAdapter() { + override fun fromJson(reader: JsonReader): Any { + val list = buildList { + reader.beginArray() + while (reader.hasNext()) { + add(elementAdapter.fromJson(reader)) + } + reader.endArray() + } + val array = JavaArray.newInstance(elementClass, list.size) + list.forEachIndexed { i, item -> + JavaArray.set(array, i, item) + } + return array + } + + override fun toJson(writer: JsonWriter, value: Any?) { + writer.beginArray() + when (value) { + is BooleanArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is ByteArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is CharArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is DoubleArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is FloatArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is IntArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is LongArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is ShortArray -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + is Array<*> -> { + for (element in value) { + elementAdapter.toJson(writer, element) + } + } + } + writer.endArray() + } + + override fun toString() = "$elementAdapter.array()" + + companion object Factory : JsonAdapter.Factory { + override fun create(type: Type, annotations: Set, moshi: Moshi): JsonAdapter<*>? { + val elementType = Types.arrayComponentType(type) ?: return null + if (annotations.isNotEmpty()) return null + val elementClass = elementType.rawType + val elementAdapter = moshi.adapter(elementType) + return ArrayJsonAdapter(elementClass, elementAdapter).nullSafe() + } + } +} diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.kt b/moshi/src/main/java/com/squareup/moshi/Moshi.kt index 962a7e2..1efc9c8 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.kt +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.kt @@ -367,7 +367,7 @@ public class Moshi internal constructor(builder: Builder) { add(StandardJsonAdapters) add(CollectionJsonAdapter.Factory) add(MapJsonAdapter.Factory) - add(ArrayJsonAdapter.FACTORY) + add(ArrayJsonAdapter.Factory) add(RecordJsonAdapter.Factory) add(ClassJsonAdapter.FACTORY) }