moshi

Moshi + Kotlin + SealedClass

廉价感情. 提交于 2020-07-05 05:44:55
问题 Is there a way of deserializing json using sealed class Layer data class ShapeLayer(var type: LayerType) : Layer data class TextLayer(var type: LayerType) : Layer data class ImageLayer(var type: LayerType) : Layer LayerType is just some enum which can be used to distinguish which type should this object have. I thought I could add Adapter this way: class LayerAdapter{ @FromJson fun fromJson(layerJson: LayerJson): Layer { return when (layerJson.layerType) { LayerType.SHAPE -> PreCompLayer()

Moshi + Kotlin + SealedClass

百般思念 提交于 2020-07-05 05:44:15
问题 Is there a way of deserializing json using sealed class Layer data class ShapeLayer(var type: LayerType) : Layer data class TextLayer(var type: LayerType) : Layer data class ImageLayer(var type: LayerType) : Layer LayerType is just some enum which can be used to distinguish which type should this object have. I thought I could add Adapter this way: class LayerAdapter{ @FromJson fun fromJson(layerJson: LayerJson): Layer { return when (layerJson.layerType) { LayerType.SHAPE -> PreCompLayer()

Moshi + Kotlin + SealedClass

半城伤御伤魂 提交于 2020-07-05 05:44:08
问题 Is there a way of deserializing json using sealed class Layer data class ShapeLayer(var type: LayerType) : Layer data class TextLayer(var type: LayerType) : Layer data class ImageLayer(var type: LayerType) : Layer LayerType is just some enum which can be used to distinguish which type should this object have. I thought I could add Adapter this way: class LayerAdapter{ @FromJson fun fromJson(layerJson: LayerJson): Layer { return when (layerJson.layerType) { LayerType.SHAPE -> PreCompLayer()

Moshi LocalDateTime adapter with multiple format

别来无恙 提交于 2020-06-24 22:28:39
问题 By default, ThreeTenABP.LocalDateTime is converted to {"date":{"day":10,"month":4,"year":2018},"time":{"hour":3,"minute":34,"nano":115000000,"second":18}} I can write an adapter to support ISO date string 2018-04-10T03:45:26.009 class LocalDateTimeAdapter { @ToJson fun toJson(value: LocalDateTime): String { return FORMATTER.format(value) } @FromJson fun fromJson(value: String): LocalDateTime { return FORMATTER.parse(value, LocalDateTime.FROM) } companion object { private val FORMATTER =

Dealing with one field that is sometimes boolean and sometimes int

人盡茶涼 提交于 2020-05-14 03:58:10
问题 I'm trying to work with the reddit JSON API. There are post data objects that contain a field called edited which may contain a boolean false if the post hasn't been edited, or a timestamp int if the post was edited. Sometimes a boolean: { "edited": false, "title": "Title 1" } Sometimes an int: { "edited": 1234567890, "title": "Title 2" } When trying to parse the JSON where the POJO has the field set to int, I get an error: JsonDataException: Expected an int but was BOOLEAN... How can I deal

Moshi equivalent of Gson's “serializeNulls”

浪尽此生 提交于 2020-01-30 08:20:07
问题 I recently replaced Moshi for Gson in a backend that's expected to serialize responses with nullable values as { "value": null } , instead of {} . Neither Moshi nor Gson do this by default, but Gson has an option to do it directly in the builder: Gson gson = new GsonBuilder().serializeNulls().create() . Does Moshi have support for something similar? 回答1: You can call serializeNulls() on any JsonAdapter to get a JsonAdapter that'll serialize nulls. 来源: https://stackoverflow.com/questions

Moshi adapter to skip bad objects in the List<T>

半世苍凉 提交于 2020-01-13 11:32:50
问题 I use Moshi and I need to solve my problem with a buggy backend. Sometimes, when I request a list of objects, some of them don't contain mandatory fields. Of course, I can catch and process JsonDataException , but I want to skip these objects. How can I do it with Moshi? Update I have a couple of models for my task @JsonClass(generateAdapter = true) data class User( val name: String, val age: Int? ) @JsonClass(generateAdapter = true) data class UserList(val list: List<User>) and buggy JSON {

deserialization of a JSON API response with moshi

末鹿安然 提交于 2020-01-03 04:14:05
问题 I got a null object attributes after deserialization of a json response. Developing under android, I'm using retrofit2 , moshi as converter (https://github.com/kamikat/moshi-jsonapi ) . When debugging ,I saw a json response fully retrieved (not null attributes),but deserialization fails. Should I use GSON instead? Here's my retrofit builder I use to make my json call: (no issue) public static JsonServerInterface getSimpleClient(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_AUTH

Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

怎甘沉沦 提交于 2020-01-01 05:29:10
问题 I'm trying to write a null-safe String adapter that will serialize this JSON {"nullString": null} into this: Model(nullString = "") so that any JSON with a 'null' value that I expect to be a String will be replaced with "" (assuming there exists a data class like this: data class Model(val nullString: String) ) I wrote a custom adapter to try and handle this: class NullStringAdapter: JsonAdapter<String>() { @FromJson override fun fromJson(reader: JsonReader?): String { if (reader == null) {