mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Convert ArrayJsonAdapter.java to kotlin (#1468)
Co-authored-by: Zac Sweers <pandanomic@gmail.com> Co-authored-by: Spencer Griffin <sgriffin@ancestry.com>
This commit is contained in:
@@ -37,7 +37,8 @@ val japicmp = tasks.register<JapicmpTask>("japicmp") {
|
|||||||
)
|
)
|
||||||
fieldExcludes = listOf(
|
fieldExcludes = listOf(
|
||||||
"com.squareup.moshi.CollectionJsonAdapter#FACTORY", // False-positive, class is not public anyway
|
"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
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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<Object> {
|
|
||||||
public static final Factory FACTORY =
|
|
||||||
new Factory() {
|
|
||||||
@Override
|
|
||||||
public @Nullable JsonAdapter<?> create(
|
|
||||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
|
||||||
Type elementType = Types.arrayComponentType(type);
|
|
||||||
if (elementType == null) return null;
|
|
||||||
if (!annotations.isEmpty()) return null;
|
|
||||||
Class<?> elementClass = Types.getRawType(elementType);
|
|
||||||
JsonAdapter<Object> elementAdapter = moshi.adapter(elementType);
|
|
||||||
return new ArrayJsonAdapter(elementClass, elementAdapter).nullSafe();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Class<?> elementClass;
|
|
||||||
private final JsonAdapter<Object> elementAdapter;
|
|
||||||
|
|
||||||
ArrayJsonAdapter(Class<?> elementClass, JsonAdapter<Object> elementAdapter) {
|
|
||||||
this.elementClass = elementClass;
|
|
||||||
this.elementAdapter = elementAdapter;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object fromJson(JsonReader reader) throws IOException {
|
|
||||||
List<Object> 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()";
|
|
||||||
}
|
|
||||||
}
|
|
107
moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt
Normal file
107
moshi/src/main/java/com/squareup/moshi/ArrayJsonAdapter.kt
Normal file
@@ -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<Any>
|
||||||
|
) : JsonAdapter<Any?>() {
|
||||||
|
override fun fromJson(reader: JsonReader): Any {
|
||||||
|
val list = buildList<Any?> {
|
||||||
|
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<Annotation>, moshi: Moshi): JsonAdapter<*>? {
|
||||||
|
val elementType = Types.arrayComponentType(type) ?: return null
|
||||||
|
if (annotations.isNotEmpty()) return null
|
||||||
|
val elementClass = elementType.rawType
|
||||||
|
val elementAdapter = moshi.adapter<Any>(elementType)
|
||||||
|
return ArrayJsonAdapter(elementClass, elementAdapter).nullSafe()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -367,7 +367,7 @@ public class Moshi internal constructor(builder: Builder) {
|
|||||||
add(StandardJsonAdapters)
|
add(StandardJsonAdapters)
|
||||||
add(CollectionJsonAdapter.Factory)
|
add(CollectionJsonAdapter.Factory)
|
||||||
add(MapJsonAdapter.Factory)
|
add(MapJsonAdapter.Factory)
|
||||||
add(ArrayJsonAdapter.FACTORY)
|
add(ArrayJsonAdapter.Factory)
|
||||||
add(RecordJsonAdapter.Factory)
|
add(RecordJsonAdapter.Factory)
|
||||||
add(ClassJsonAdapter.FACTORY)
|
add(ClassJsonAdapter.FACTORY)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user