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
5
votes
1 answer

Parse JSON key that is either object or array of object

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…
fhucho
  • 34,062
  • 40
  • 136
  • 186
4
votes
1 answer

How to specify a different name for json key while using Retrofit + Moshi

I need to make a POST request to my backend with following json: { start_time: 123456789 } I have created the below data class for the body in Retrofit request: data class MyRequestBody( @Json(name="start_time") val startTime:…
Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40
4
votes
2 answers

Moshi-JsonDataException: Required value '%s' missing at $

I was working with Moshi to pull JSON data from an API and map it to my DTO Data classes when I encountered this error: FATAL EXCEPTION: main Process: com.plcoding.weatherapp, PID: 9706 com.squareup.moshi.JsonDataException: Required value…
Tonnie
  • 4,865
  • 3
  • 34
  • 50
4
votes
0 answers

Moshi: When is Reflection needed?

Currently using Moshi with Retrofit but need to look back as I noticed I am not implementing it right and confused when to add kotlin-reflect in the dependencies. According to README The reflection adapter requires the following additional…
Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67
4
votes
1 answer

Moshi 1.12.0 with Kotlin 1.6.0 fails to parse metadata

I'm trying to build my project, but it failed so I ran it with --stacktrace and I'm getting the following error message: Execution failed for task ':app:kaptDebugKotlin'. > A failure occurred while executing…
Tom Darious
  • 434
  • 6
  • 18
4
votes
2 answers

ProGuard: ClassCastException with Moshi+Retrofit

It works fine in debug mode, and release mode with ProGuard off, but not with ProGuard on, even with an empty one. Here is the stack trace: 2021-09-07 23:42:19.556 32130-32130/com.myapp.packagename W/System.err: java.lang.ClassCastException:…
Leon
  • 811
  • 10
  • 21
4
votes
3 answers

Using an annotation to Serialize Null in Moshi with nested Event

I am attempting to add a custom annotation to serialize specific values in my model to null when calling the toJSON method from Moshi. I have something working based on this response but it's falling short for me when I have a nested…
Bueno
  • 1,840
  • 2
  • 15
  • 17
4
votes
1 answer

Unable to create @Body converter for class in Retrofit

I have an instance of retrofit built like this val moshi = Moshi.Builder() .add(SkipBadElementsListAdapter.Factory) .add(KotlinJsonAdapterFactory()) .add(Date::class.java, MoshiDateAdapter()) .build() val…
andrewedgar
  • 797
  • 1
  • 12
  • 46
4
votes
0 answers

Retrofit/Moshi: Platform class java.util.Date requires explicit JsonAdapter to be registered

I'm new to Android/Retrofit and Moshi. I'm trying to make a POST call to my API and I have a problem with serialising Date. If you see anything else that needs correcting, please point it out as I'm still learning. Thanks interface ApiInterface { …
Houman
  • 64,245
  • 87
  • 278
  • 460
4
votes
3 answers

Why I get Execution failed for task ':app:kaptDebugKotlin' if I use Moshi?

Hy I created an app which uses dagger, room, Moshi, Retrofit etc... It works correctly until I add this line to my Model class : @JsonClass(generateAdapter = true) After that I get this error: A failure occurred while executing…
Andreas1234
  • 433
  • 4
  • 25
4
votes
1 answer

Moshi expected BEGIN_OBJECT but was BEGIN_ARRAY at path $

This is not retrofit but manual parsing in a Firebase message handler service. I am using KotlinJsonAdapterFactory() when building my Moshi instance. For some reason it thinks one of the nodes is an array when I am asking for it to be parsed as an…
Charlie Niekirk
  • 1,015
  • 1
  • 10
  • 15
4
votes
1 answer

How to parse retrofit json response to object model with Moshi

Retrofit Instance: fun getMoshi() : Moshi{ return Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() } fun retrofit(baseUrl: String): Retrofit = Retrofit.Builder() .client(getClient()) …
ashaneen
  • 137
  • 1
  • 15
4
votes
2 answers

Moshi parsing with InputStream

Assuming I need to parse a huge list of Items from a json asset file in Android with the format similar to below: [ { "id": 1, "name: "Tom" // other stuff to describe "Item" } ] For performance reason I want to avoid…
H.Nguyen
  • 1,621
  • 5
  • 19
  • 31
4
votes
2 answers

Moshi Json annotation does not work with proguard

I followed https://github.com/square/moshi to add gradle dependency on moshi and proguard rules, then I write code to verify. data class Car( @Json(name = "low_speed") val lowSpeed: Int, @Json(name = "high_speed") val highSpeed: Int ) val…
Liu Adoo
  • 41
  • 1
  • 3
4
votes
1 answer

Moshi Multiple Custom Names

With Moshi you can have a custom name for a field of a class @Json(name = "your name") string name But can you have more than one custom name? @Json(name = "your name" || "your/name" || "your-name") string name, So either one of "your name" or…
TheBearF8
  • 375
  • 1
  • 3
  • 14