Android Kotlin中的Moshi-将ENUM作为MutableMap键在反序列化时转换为String [英] Moshi in Android Kotlin - ENUM as MutableMap key being converted to String when deseralized

查看:317
本文介绍了Android Kotlin中的Moshi-将ENUM作为MutableMap键在反序列化时转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要保存在onSaveInstanceState中的MutableMap<CryptoTypes, CurrentTradingInfo>,并且打算使用Moshi来回转换. CryptoTypes is an ENUM

I have a MutableMap<CryptoTypes, CurrentTradingInfo> that I'm wanting to save in onSaveInstanceState and was going to use Moshi to convert back and forth. CryptoTypes is an ENUM

private var tickerData: MutableMap<CryptoTypes, CurrentTradingInfo> = mutableMapOf()


fun convertTickerDataJson(): String {
    val moshi = Moshi.Builder().build()
    val jsonAdapter = moshi.adapter<MutableMap<CryptoTypes, CurrentTradingInfo>>(MutableMap::class.java)
    return jsonAdapter.toJson(tickerData)
}

fun restoreTickerDataFromJson(data: String) {
    val moshi = Moshi.Builder().build()
    val jsonAdapter = moshi.adapter<MutableMap<CryptoTypes, CurrentTradingInfo>>(MutableMap::class.java)
    tickerData = jsonAdapter.fromJson(data)
}

数据正确地序列化了,但是当反序列化时,它又给了我MutableMap<String, CurrentTradingInfo>吗?

The data is serializing correctly, but when it's deserialized, it's giving me back a MutableMap<String, CurrentTradingInfo> instead?

当我在序列化之前在Studio中查看我的tickerData映射时,显然是将ENUM存储为ENUM

When I look at my tickerData map in studio before I serialize it, it's clearly storing the ENUM as an ENUM

这是反序列化后的地图(请注意,该地图是无序的,我不得不再次运行它,因此地图的键顺序不同)

This is the map after being deserialized back [note the map is unordered and I had to re-run it again, hence the map keys in different orders]

它如何能给我返回错误键入的地图?难道我做错了什么?

How is it able to give me back an incorrectly typed map? Am I doing something wrong?

当我尝试访问地图发布转换时,由于类型错误,它会崩溃并显示以下内容

When I try to access the map post conversion it crashes with the below since the type is wrong

Java.lang.ClassCastException: java.lang.String cannot be cast to com.nebulights.crytpotracker.CryptoTypes

如果我创建两个变量

   private var tickerDataA: MutableMap<CryptoTypes, CurrentTradingInfo> = mutableMapOf()

   private var tickerDataB: MutableMap<String, CurrentTradingInfo> = mutableMapOf()

我不能使用tickerDataA = tickerDataB,它显示为类型不匹配,并且不会让我按需编译.

I can't go tickerDataA = tickerDataB, it shows as a type mismatch and won't let me compile as it should.

推荐答案

moshi.adapter<MutableMap<CryptoTypes, CurrentTradingInfo>>(MutableMap::class.java)

之所以会出现此问题,是因为您没有提供完整的类型,仅提供了通用的MutableMap类.因此,它使用Object序列化器,而不是专门用于键/值类型的序列化器.

The problem occurs because you are not providing the complete type, only the generic MutableMap class. Because of this it uses the Object serializer instead of one specialized for the key/value types.

尝试创建参数化类型:

val type = Types.newParameterizedType(MutableMap::class.java, CryptoTypes::class.java, CurrentTradingInfo::class.java)
val jsonAdapter = moshi.adapter<MutableMap<CryptoTypes, CurrentTradingInfo>>(type)

这应该为Moshi提供正确序列化地图所需的信息.

This should provide Moshi with the information it required to correctly serialize the map.

这篇关于Android Kotlin中的Moshi-将ENUM作为MutableMap键在反序列化时转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆