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

Moshi LocalDateTime adapter with multiple format

By default, ThreeTenABP.LocalDateTime is converted to {"date":{"day":10,"month":4,"year":2018},"time":{"hour":3,"minute":34,"nano":115000000,"second":18}} I can write an adapter to support ISO date string 2018-04-10T03:45:26.009 class…
Desmond Lua
  • 6,142
  • 4
  • 37
  • 50
9
votes
1 answer

Moshi - Parse unknown json keys

How can I parse with moshi a json structure that has keys that are unknown at compile time: "foo": { "name": "hello", "bar": { "unknownKey1": { "a": "1" } }, "unknownKey2": { "b": "2" }, "unknownKeyX": { …
Blundell
  • 75,855
  • 30
  • 208
  • 233
9
votes
3 answers

Moshi Retrofit error: "Expected a string but was BEGIN_OBJECT"

I built my Retrofit instance like this: Retrofit retrofit = new Retrofit.Builder() .baseUrl(server.url("/")) .addConverterFactory(MoshiConverterFactory.create(moshi)) .build(); Then I am calling my MockWebServer…
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
8
votes
1 answer

KotlinJsonAdapterFactory unresolved reference

I am building an Android app in Kotlin, and I'm using Retrofit for the API calls, and I'm also using Moshi. But when building Moshi it says KotlinJsonAdapteryFactory - unresolved reference, and I can't use Moshi since it gives me an error in this…
FrensisXh
  • 127
  • 2
  • 8
8
votes
2 answers

Get ParameterizedType from reified generic type for Moshi type adapter

The given assertion will fail due to the error Failed to find the generated JsonAdapter constructor for class GenericType How do I get the proper type from the reified type T for Moshi? The GenericType type is annotated with…
tynn
  • 38,113
  • 8
  • 108
  • 143
8
votes
4 answers

Serialize sealed class with Moshi

The following will produce an IllegalArgumentException because you "Cannot serialize abstract class" sealed class Animal { data class Dog(val isGoodBoy: Boolean) : Animal() data class Cat(val remainingLives: Int) : Animal() } private val…
andrew
  • 727
  • 1
  • 11
  • 21
8
votes
1 answer

Kotlin-moshi codegen inheritance with default values

If I have an open class and inherited data class from it, Kotlin-moshi codegen skip default value. Is this intended behaviour? How to make moshi-kotlin parse all values including default from superclass? @JsonClass(generateAdapter = true) data…
Sunstrike
  • 456
  • 1
  • 6
  • 22
8
votes
2 answers

How Moshi applies the default value of kotlin to the null field of json

Json { "id": "1", "name": "name", "email": null } User data class User( var id: Int = 0, var name: String = "", var email: String = "" ) I want to apply default value for null value when parsing json. (Apply the above email…
dylan.kwon
  • 435
  • 5
  • 14
8
votes
1 answer

default value in kotlin data class not work when server send null for that value in moshi

I use Moshi for parse json from server. if server send null for item default value not set! but server not send that item default value set. json: {"percentChange": null,"change": "-2500.00","value": "130000","name": null} data…
h.kelidari
  • 800
  • 1
  • 11
  • 20
8
votes
2 answers

Moshi's Custom Adapter with RxAndroid & Retrofit & Kotlin

After configuring Kotlin for Android project, I wrote a simple MainActivity.kt. It called Retrofit to get a JSON file which contained the following data: { "message": "success", "user": { "username": "Eric" } } Now I want to…
iForests
  • 6,757
  • 10
  • 42
  • 75
7
votes
0 answers

Moshi: How to provide adapter for PolymorphicJsonAdapterFactory.withFallbackJsonAdapter while configuring Moshi.Builder

I'm trying to use PolymorphicJsonAdapterFactory for JSON which provides different structure depending on type field, like below. { "notifications": [ { "type": "achievement", "title": "New Achievement!", "content": "You got…
ytRino
  • 1,450
  • 15
  • 28
7
votes
5 answers

Api call failed Unable to create converter for class Retrofit/Moshi

I am implementing retrofit and moshi to make requests to a server (I am newbie using retrofit). I follow some guides that I found on the internet on how to implement it but when launching the app I receive the following error: Error Api call failed…
Russbell G.H
  • 345
  • 4
  • 11
7
votes
5 answers

Duplicate class kotlin classes kotlin version 1.3.70

Error while adding MoshiPack Library in Kotlin latest version 1.3.70 to gradle.build application Moshi pack implementation 'com.daveanthonythomas.moshipack:moshipack:1.0.1' Error Message Duplicate class kotlin.reflect.KClasses found in modules…
Mohamed Ali
  • 103
  • 1
  • 2
  • 9
7
votes
1 answer

Unwrapping enveloped responses with Retrofit, Moshi, and Kotlin

I'm working with an API that returns responses that look like: { "irrelevantStuff": {}, "data": [] } I'd like to follow the approach demonstrated in this presentation from Jake, where he creates a Retrofit converter factory to handle and…
Chris Horner
  • 1,994
  • 2
  • 16
  • 26
7
votes
2 answers

How can I parse Nested JSON with dynamic keys in Android kotlin, Moshi and Retrofit?

How can I parse Nested JSON with NESTED dynamic keys in Android kotlin, Moshi and Retrofit? I get this JSON from alpha-vantage. Format example: { "Meta Data": { "1. Information": "Intraday (15min) open, high, low, close prices and…
Maor Hadad
  • 1,850
  • 21
  • 36
1 2
3
35 36