moshi

moshi custom qualifier annotation to serialise null on one property only

纵然是瞬间 提交于 2019-12-31 03:18:46
问题 I'd like to serialise null for only one property in my JSON body that is going on a PUT. I don't want to serialize null for any other types in the object. Model class is like this @Parcel class User @ParcelConstructor constructor(var college: College?, var firstname: String?, var lastname: String?, var email: String?, var active: Boolean = true, var updatedAt: String?, var gender: String?, var picture: String?, var id: String?, @field: [CollegeField] var collegeInput: String?, @field:

Moshi HashMap deserializer

那年仲夏 提交于 2019-12-24 02:07:27
问题 I don't know if it is possible to deserialise arrays into hashMap i have got json : "additionalProperties": [ { "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities", "category": "Description", "key": "TerminalName", "sourceSystemKey": "BikePoints", "value": "200017", "modified": "2016-05-19T12:02:39.82" } ........ ] and for that json i have got list : private List<AdditionalProperties> additionalProperties; everything works perfect but how store that

Parse JSON key that is either object or array of object

删除回忆录丶 提交于 2019-12-22 06:47:22
问题 In Moshi, is it possible to create a type adapter that would parse both an object or a list of objects of the same type? For example, sometimes the JSON is: { "person": {...} } Other times it is: { "person": [{...}, {...}] } Ideally, I'd want to have a Java object looking like this: class PersonContainer { @PersonsList List<Person> persons; // @List(Person.class) would be even better } 回答1: I would suggest you make just what you said, an adapter. make a function(the adapter) to check whether

What does `moshi` in the adapter-constructor generated by codegen mean?

与世无争的帅哥 提交于 2019-12-20 05:15:43
问题 I'm a newbee in kotlin-android development. I want to parse a JSON like { "name": "This is my name", } to a Foo class defined as Foo.kt @JsonClass(generateAdapter = true) data class Foo(val name: String) This Foo class file generates a code by codegen as follows. FooJsonAdapter.kt (generated by codegen) class FooJsonAdapter(moshi: Moshi) : JsonAdapter<Foo>() { private val options: JsonReader.Options private val stringAdapter: JsonAdapter<String> override fun toString(): String override fun

Moshi Determine if JSON is array or single object

流过昼夜 提交于 2019-12-19 21:48:45
问题 Is there a way to setup a Moshi adapter to automatically create a single Object or List<Object> based on the JSON response? Currently, I can do this explicitly. For example, I can receive the following responses: { "userId": "1", "id": "2", "body": "body...", "title": "title..." } Or [ { "userId": "1", "id": "2", "body": "body...", "title": "title..." } ] And I would like to create Object or List<Object> without having to explicitly specify which one to use. 回答1: You can use a JsonQualifier

How to deseralize an int array into a custom class with Moshi?

此生再无相见时 提交于 2019-12-12 06:08:20
问题 I use Moshi to deserialize the following JSON file: { "display": "Video 1", "isTranslated": false, "videoSize": [ 1920, 1080 ] } ... using the following model class: public class Stream { public final String display; public final boolean isTranslated; public final int[] videoSize; public Stream(String display, boolean isTranslated, int[] videoSize) { this.display = display; this.isTranslated = isTranslated; this.videoSize = videoSize; } } This works as expected. Now, I would like to replace

Broken server response handling with Moshi

不问归期 提交于 2019-12-11 02:15:49
问题 The expected json response from server should be : { "teacher": { "123": { "_id": "389", "name": "test_fast_teacher1" } } } Server returned json with this : { "teacher": [ ] } Anyway to handle this broken json response? Before I switching from Gson, the teacher object will still be deserialised, just that it will be null. By using Moshi, the error would be threw and I can't proceed with the other json which is serialised correctly. Please refer to the link for the reply from author. 回答1: How

Base64 decode then deserialize json to pojo with Moshi

匆匆过客 提交于 2019-12-10 23:49:59
问题 I have the following json payload: { "foo" : "bar", "foo2" : "TCFNhbiBKb3NlMRgwFgYDVQQK" } The value Json key foo2 has a value that is a Json string that is base64 encoded. For example, when the value of foo2 is Base64.decoded(), the above Json would look something like this: { "foo" : "bar", "foo2" : "{"cat":"meow","dog":"woof"}" } Current Solution public class Message { public String foo; public AnimalSound foo2; } public class AnimalSound { public String cat; public String dog; } public

Moshi 1.9.1 Cannot serialize Kotlin type

*爱你&永不变心* 提交于 2019-12-10 15:28:23
问题 I have a working code serializing/deserializing data using Moshi 1.8.0 Upgrading to 1.9.1 now leads to a crash when attempting to serialize: java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.xxx.Spot. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact. Here is the serializer code: val moshi = Moshi

Moshi ignore field

人走茶凉 提交于 2019-12-10 02:51:56
问题 Is there a simple way to ignore a field when using moshi to serialize to a json string? I can only think about is a custom adapter - but I have the feeling that there is a better way 回答1: Use transient on the field declaration. private transient String your_variable_name; Originally I found Exclude fields from serialization and deserialization Hope It's help you. 来源: https://stackoverflow.com/questions/36204499/moshi-ignore-field