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
4
votes
2 answers

How to parse a field in 2 different ways with Moshi

I need to parse an object that contains a property "triggers" which is an List. This list can contain 2 type of triggers: Custom and Event. Here are my Trigger classes : @JsonClass(generateAdapter = true) open class Trigger(open val…
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
4
votes
1 answer

Moshi deserializing values as null

I've recently switched from Gson to Moshi and am having trouble parsing some Json. { "access_token": "-LNe2LQ7DQH5Y2zs_W5iUumKuaUE", "token_type": "bearer", "device_id": "461f-837e-af5050c92fe9", "expires_in": 3600, "scope": "*" } And…
SpecialSnowflake
  • 945
  • 4
  • 16
  • 32
4
votes
2 answers

How to make JSON annotation of Moshi case insensitive in Android

Sometimes my back-end returns Email and sometimes it returns email. I want the first character to be case insensitive. Is this possible? public class GetConversationListResponseBody { @Keep @Json(name = "email") private String email; …
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
4
votes
0 answers

Moshi is not mapping null values in Map

Let's say that we have a class that contains only one property: data class MyClass(val map: Map) As you can see, map property may contain null values, e.g.: { "key1": "value1", "key2": null } Now if I use this instance of…
Nominalista
  • 4,632
  • 11
  • 43
  • 102
4
votes
1 answer

Moshi equivalent of Gson's "serializeNulls"

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:…
Luiz Scheidegger
  • 788
  • 5
  • 12
4
votes
1 answer

Moshi Expected BEGIN_OBJECT but was BEGIN_ARRAY - custom converter ignored

I'm using Moshi as converter for Retrofit, but for one particular request it doesn't work and exception is thrown: com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $.results The JSON I want to parse: { "id":…
Luke
  • 2,539
  • 2
  • 23
  • 40
4
votes
1 answer

TypeToken.getParameterized in Jackson json

In GSON I can next: Type envelopeType = TypeToken.getParameterized(RestResponse.class, type).getType(); In Moshi: Type envelopeType = Types.newParameterizedType(RestResponse.class, type); How can I do it with Jackson json?
ip696
  • 6,574
  • 12
  • 65
  • 128
4
votes
1 answer

Broken server response handling with Moshi

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…
wenyang9319
  • 180
  • 2
  • 13
4
votes
0 answers

Generic json parse in Moshi

I am using Square's JSON parsing library.i want to warp a common library. just like public synchronized boolean put(String key, Class clzz) { JsonAdapter tJsonAdapter = mMoshi.adapter(clzz); String jsonString =…
4
votes
1 answer

Conversion of a List of HashMaps from/to JSON fails with Moshi 1.2.0

I want to migrate my code from GSon to MOSHI in order to get the benefits of the common underlying usage of the OK-Libraries as I am using this also with OKHTTP and Retrofit. But a task that is simple with Gson seems to be complicated with MOSHI: I…
Sqrt-1764
  • 321
  • 1
  • 17
3
votes
1 answer

Kotlin Android Room handle empty list of objects in Moshi TypeConverter

I inherited an Android project where I had to rebase the rxJava code to a more pure Kotlin centric code base with a MVVM design pattern. Given my inexperience, along with lots of head scratching and reading, I finally finished the code conversion. …
Hmerman6006
  • 1,622
  • 1
  • 20
  • 45
3
votes
0 answers

Platform class kotlin.Unit requires explicit JsonAdapter to be registered

In a retrofit interface I have defined a method return type as ApiModel < Unit >. but when I call the method I get the error below. Platform class kotlin.Unit requires explicit JsonAdapter to be registered so I tried to create my own JsonAdapter as…
mrzbn
  • 497
  • 1
  • 3
  • 15
3
votes
3 answers

Failed to transform moshi-1.13.0.jar (failed for task biometric_storage:kaptGenerateStubsDebugKotlin)

I pulled a team's project that is made in Flutter from Git and for some reason when building the app (pressing the green arrow in Android Studio and running main.dart) I get the following message: What went wrong: Execution failed for task…
Pero122
  • 892
  • 11
  • 25
3
votes
1 answer

Moshi: PolymorphicJsonAdapterFactory is it possible to get the type in withDefaultValue?

I have a moshi PolymorphicJsonAdapterFactory and it works great. .withSubtype(ColdWeather::class.java, "Cold") .withSubtype(HotWeather::class.java, "Hot") .withDefaultValue(//how to grab the label) The method withDefaultValue is a great catch all,…
c_idle
  • 1,448
  • 4
  • 22
  • 40
3
votes
2 answers

Why does Moshi parse integers, longs as Double?

I'm trying to parse a not very well designed api's json using Moshi + kotlin. For some reasons it parses numbers like 71 as Double. The 3rd party api has a list of objects that could either look like: {"foo":[[1234567000,12]]} // long,…
Gavriel
  • 18,880
  • 12
  • 68
  • 105