5

我正在尝试使用 Android 的 Moshi 库将 Java 对象转换为 JSON。该对象包含类型的属性

数组列表 <浮点[]>

我正在注册以下适配器来转换对象。

Type type = Types.newParameterizedType
        (List.class, HistoryPath.class, ArrayList.class, Float[].class, Float.class);

JsonAdapter<Drawing> adapter = moshi.adapter(type);

String json = adapter.toJson(drawing);

“toJson”方法失败,出现以下异常:

java.lang.IllegalArgumentException:平台 java.util.ArrayList annotated [] 需要在 com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:50) 在 com.squareup.moshi.Moshi.adapter( Moshi.java:99) 在 com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:90) 在 com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:74) 在 com.squareup.moshi.Moshi.adapter(Moshi.java:99) 在 com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:90) 在 com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter .java:74) 在 com.squareup.moshi.Moshi.adapter(Moshi.java:99) 在 com.squareup.moshi.Moshi.adapter(Moshi.java:57) 在 com.squareup.moshi.CollectionJsonAdapter.newArrayListAdapter(CollectionJsonAdapter.java:51) 在 com.squareup.moshi.CollectionJsonAdapter$1.create(CollectionJsonAdapter.java:35) 在 com.squareup.moshi.Moshi.adapter(Moshi. java:99) 在 com.squareup.moshi.Moshi.adapter(Moshi.java:57)squareup.moshi.CollectionJsonAdapter$1.create(CollectionJsonAdapter.java:35) at com.squareup.moshi.Moshi.adapter(Moshi.java:99) at com.squareup.moshi.Moshi.adapter(Moshi.java:57)squareup.moshi.CollectionJsonAdapter$1.create(CollectionJsonAdapter.java:35) at com.squareup.moshi.Moshi.adapter(Moshi.java:99) at com.squareup.moshi.Moshi.adapter(Moshi.java:57)

我认为我的类型定义是错误的,但我似乎找不到正确的值

4

3 回答 3

11

正如错误所说,ArrayList 是一种平台类型。您可以使用 List(或专门为 ArrayList 制作自定义 JsonAdapter)。

另外,这里有个问题: Type type = Types.newParameterizedType(List.class, HistoryPath.class, ArrayList.class, Float[].class, Float.class);

这种类型是List<HistoryPath, ArrayList, Float[], Float>无效的,它不会产生正确的结果JsonAdapter<Drawing> adapter = moshi.adapter(type);

你可能只是想要JsonAdapter<Drawing> adapter = moshi.adapter(Drawing.class);.

于 2017-05-09T07:02:48.960 回答
8

那些想用ArrayList和魔时


解决方案 1

您必须为要使用的每个对象制作适配器。

class ThingArrayListMoshiAdapter {
    @ToJson
    fun arrayListToJson(list: ArrayList<Thing>): List<Thing> = list

    @FromJson
    fun arrayListFromJson(list: List<Thing>): ArrayList<Thing> = ArrayList(list)
}

用法

Moshi
    .Builder()
    .add(ThingArrayListMoshiAdapter())
    .build()

解决方案 2

做一个JsonAdapterfor ArrayList,像 Moshi 的CollectionJsonAdapter

abstract class MoshiArrayListJsonAdapter<C : MutableCollection<T>?, T> private constructor(
    private val elementAdapter: JsonAdapter<T>
) :
    JsonAdapter<C>() {
    abstract fun newCollection(): C

    @Throws(IOException::class)
    override fun fromJson(reader: JsonReader): C {
        val result = newCollection()
        reader.beginArray()
        while (reader.hasNext()) {
            result?.add(elementAdapter.fromJson(reader)!!)
        }
        reader.endArray()
        return result
    }

    @Throws(IOException::class)
    override fun toJson(writer: JsonWriter, value: C?) {
        writer.beginArray()
        for (element in value!!) {
            elementAdapter.toJson(writer, element)
        }
        writer.endArray()
    }

    override fun toString(): String {
        return "$elementAdapter.collection()"
    }

    companion object {
        val FACTORY = Factory { type, annotations, moshi ->
            val rawType = Types.getRawType(type)
            if (annotations.isNotEmpty()) return@Factory null
            if (rawType == ArrayList::class.java) {
                return@Factory newArrayListAdapter<Any>(
                    type,
                    moshi
                ).nullSafe()
            }
            null
        }

        private fun <T> newArrayListAdapter(
            type: Type,
            moshi: Moshi
        ): JsonAdapter<MutableCollection<T>> {
            val elementType =
                Types.collectionElementType(
                    type,
                    MutableCollection::class.java
                )

            val elementAdapter: JsonAdapter<T> = moshi.adapter(elementType)

            return object :
                MoshiArrayListJsonAdapter<MutableCollection<T>, T>(elementAdapter) {
                override fun newCollection(): MutableCollection<T> {
                    return ArrayList()
                }
            }
        }
    }
}

用法

Moshi
    .Builder()
    .add(MoshiArrayListJsonAdapter.FACTORY)
    .build()
于 2020-04-17T13:23:31.683 回答
1

将其转换为数组。

假设您要使用arraylist : ArrayList<>. 您将其转换为数组。arraylist.toArray()

于 2019-12-31T06:52:20.267 回答