Questions tagged [moshi]

Moshi is a JSON library for Android and Java. It makes it easy to parse JSON into Java objects:

About

Moshi is a modern JSON library for Android and Java. It makes it easy to parse JSON into Java objects

Moshi has built-in support for reading and writing Java’s core data types:

  • Primitives (int, float, char...) and their boxed counterparts (Integer, Float, Character...).
  • Arrays, Collections, Lists, Sets, and Maps
  • Strings
  • Enums

Links

399 questions
131
votes
6 answers

How to make "inappropriate blocking method call" appropriate?

I am currently trying to leverage kotlin coroutines more. But I face a problem: when using moshi or okhttp inside these coroutines I get a warning: "inappropriate blocking method call" What is the best way to fix these? I really do not want to be…
ligi
  • 35,518
  • 37
  • 123
  • 220
83
votes
3 answers

Moshi vs Gson in android

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…
j2emanue
  • 51,417
  • 46
  • 239
  • 380
24
votes
4 answers

Moshi 1.9.1 Cannot serialize Kotlin type

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…
lorenzo
  • 1,161
  • 10
  • 22
24
votes
1 answer

SerializedName annotation doesn't seem to work in Moshi

So I'm trying to parse a call from my server using Moshi. This is my response object. public class TokenResponse { @SerializedName("accessToken") public String accessToken; public String token_type; public int expires_in; public…
Diskdrive
  • 15,875
  • 25
  • 87
  • 148
24
votes
2 answers

Moshi ignore field in Kotlin

I want to know how to ignore a Kotlin class field when using Moshi. I've found this answer for Java (Moshi ignore field), that indicates to use the keyword transient as follows private transient String your_variable_name; But I can't find the right…
heisen
  • 799
  • 6
  • 22
19
votes
2 answers

Moshi + Kotlin + SealedClass

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…
miszmaniac
  • 565
  • 1
  • 6
  • 16
18
votes
3 answers

Custom converter to subclass with Moshi

I have a User class. And two subclasses. Parent and Child. I get json from my server with {"user":"..."} and need to convert it to parent or to child depending on user.type As I understand I need to add custom converter this way: Moshi moshi…
Defuera
  • 3,903
  • 2
  • 25
  • 36
16
votes
2 answers

Moshi generic type adapter

Suppose have the following parameterised data class representing a server response: public class SocketResponse { private String responseMessage; private int responseCode; private T entity; } I know at runtime what type T will be.…
sirFunkenstine
  • 7,007
  • 6
  • 34
  • 51
16
votes
2 answers

Turn string date from json to a Date object with Moshi

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…
tyczj
  • 66,691
  • 50
  • 172
  • 271
15
votes
2 answers

Moshi 1.9.x Cannot serialize Kotlin type

I'm running into the following crash and stack trace after upgrading to Moshi 1.9.1 (from 1.8.0): java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.garpr.android.data.models.RankedPlayer. Reflective serialization of Kotlin classes…
Charles Madere
  • 6,222
  • 2
  • 31
  • 32
13
votes
5 answers

Moshi KotlinJsonAdapterFactory cannot parse Json after enabled minify

I have developed an Android app, using Moshi as one of its dependencies. Today I want to enable minify for this project. So I set minifyEnabled true in my build.gradle. After that, I found that all responses from server become null. First of all, I…
Sira Lam
  • 4,412
  • 24
  • 53
12
votes
2 answers

What's the use of Moshi's Kotlin codegen?

I've recently started using Moshi for my Android app and I'm curious to know more about what the annotation @JsonClass(generateAdapter = true) really does. Example data class: data class Person( val name: String ) I'm able to…
Jignesh Shah
  • 389
  • 2
  • 13
12
votes
2 answers

How To Deserialize Generic Types with Moshi?

Suppose we have this JSON: [ { "__typename": "Car", "id": "123", "name": "Toyota Prius", "numDoors": 4 }, { "__typename": "Boat", "id": "4567", "name": "U.S.S. Constitution", "propulsion": "SAIL" } ] (there…
CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
12
votes
1 answer

Moshi ignore field

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
ligi
  • 35,518
  • 37
  • 123
  • 220
11
votes
1 answer

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

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…
Jegan Babu
  • 843
  • 1
  • 9
  • 18
1
2 3
26 27