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

537 questions
261
votes
11 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
  • 39,001
  • 44
  • 144
  • 244
92
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
  • 60,549
  • 65
  • 286
  • 456
33
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,487
  • 1
  • 17
  • 25
32
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
  • 18,107
  • 27
  • 101
  • 167
29
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
  • 599
  • 1
  • 5
  • 13
29
votes
3 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
  • 1,067
  • 3
  • 9
  • 25
26
votes
3 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,642
  • 5
  • 35
  • 34
24
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
  • 71,600
  • 54
  • 194
  • 296
21
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
  • 825
  • 2
  • 10
  • 21
20
votes
6 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
  • 5,179
  • 3
  • 34
  • 68
20
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
  • 5,356
  • 3
  • 32
  • 38
19
votes
3 answers

Unable to create call adapter for retrofit2.Response<...>

My Api: @GET("/cinema/notShownMovies") fun getNotShownMovies( @Query("token") token: String ): Response Exception when trying to call API: java.lang.IllegalArgumentException: Unable to create call adapter…
kaulex
  • 2,921
  • 3
  • 11
  • 38
19
votes
3 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
  • 8,135
  • 6
  • 40
  • 57
18
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
  • 1,286
  • 1
  • 15
  • 19
14
votes
2 answers

Moshi: Parse single object or list of objects (kotlin)

Problem How to parse either a single Warning object or a list of Warning objects (List) from an API using Moshi? The response as a single warning: { "warnings": {...} } The response as a list of warnings: { "warnings": [{...},…
Niclas
  • 510
  • 6
  • 19
1
2 3
35 36