如何使用适用于Android的Moshi JSON库序列化ArrayList <float []>

时间:2017-05-08 16:49:42

标签: moshi

我正在尝试使用适用于Android的Moshi库将Java对象转换为JSON。该对象包含

类型的属性
  

ArrayList&lt;浮[]&GT;

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

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);

&#34; toJson&#34;方法失败,但出现以下异常:

  

java.lang.IllegalArgumentException:Platform   java.util.ArrayList annotated []需要显式   JsonAdapter要注册                                                                                            在   com.squareup.moshi.ClassJsonAdapter $ 1。创建(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。创建(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。创建(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。创建(CollectionJsonAdapter.java:35)                                                                                            在com.squareup.moshi.Moshi.adapter(Moshi.java:99)                                                                                            在com.squareup.moshi.Moshi.adapter(Moshi.java:57)

我认为我的类型定义错了,但我似乎无法找到正确的值

3 个答案:

答案 0 :(得分:7)

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);

答案 1 :(得分:4)

那些想使用ArrayList和Moshi的人


解决方案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

JsonAdapter做一个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()

答案 2 :(得分:0)

将其转换为数组。

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