moshi

(Moshi in kotlin) @Json vs @field:Json

浪子不回头ぞ 提交于 2019-12-07 13:15:26
问题 Serialization does not happen properly when I use @Json in the fields but it started working after changing to @field:Json . I came through this change after reading some bug thread and I think this is specific to kotlin. I would like to know what difference does @field:Json bring and is it really specific to kotlin? 回答1: Whatever you put between @ and : in your annotation specifies the exact target for your Annotation. When using Kotlin with JVM there is a substantial number of things

Turn string date from json to a Date object with Moshi

寵の児 提交于 2019-12-06 20:27:42
问题 with Gson you would do this Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm") .create(); and pass it to the retrofit builder and Gson would make a Date object for you, Is there some way to get Moshi to do this too in a kotlin class? 回答1: If you’d like to use a standard ISO-8601/RFC 3339 date adapter (you probably do) then you can use the built-in adapter: Moshi moshi = new Moshi.Builder() .add(Date.class, new Rfc3339DateJsonAdapter().nullSafe()) .build(); JsonAdapter<Date>

(Moshi in kotlin) @Json vs @field:Json

帅比萌擦擦* 提交于 2019-12-05 23:19:34
Serialization does not happen properly when I use @Json in the fields but it started working after changing to @field:Json . I came through this change after reading some bug thread and I think this is specific to kotlin. I would like to know what difference does @field:Json bring and is it really specific to kotlin? Whatever you put between @ and : in your annotation specifies the exact target for your Annotation. When using Kotlin with JVM there is a substantial number of things generated, therefore your Annotation could be put in many places. If you don't specify a target you're letting the

Parse JSON key that is either object or array of object

筅森魡賤 提交于 2019-12-05 08:50:55
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 } I would suggest you make just what you said, an adapter. make a function(the adapter) to check whether its a single object or an array, probably based on the '[' char and the adapter will direct the input into

Moshi vs Gson in android

混江龙づ霸主 提交于 2019-12-03 04:41:17
问题 I'm deciding on whether to use Moshi by square or Gson to serialize and deserialize model data. one thing i always did not like about Gson is i think it uses reflection which can be slow on android? Does Moshi use reflection also? What are some of the pros and cons of moshi vs Gson? I see them as similar. take for example this statement that creates a typeAdapter: class CardAdapter { @ToJson String toJson(Card card) { return card.rank + card.suit.name().substring(0, 1); } @FromJson Card

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

冷暖自知 提交于 2019-12-02 04:30:09
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 fromJson(reader: JsonReader): Foo override fun toJson(writer: JsonWriter, value: Foo?) } The

moshi custom qualifier annotation to serialise null on one property only

北城余情 提交于 2019-12-02 02:23:51
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: [CollegeField] var otherCollege: String?,) I only want to serialise collegeInput and otherCollege fields if