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

Accessing deeply nested JSON array with Moshi/Retrofit2

All I need is the "photo" array. My JSON looks like this: { "photos": { "page": 1, "pages": "1000", "perpage": 1, "total": "1000", "photo": [ { "id": "44049202615", "owner": "159796861@N07", …
MayNotBe
  • 2,110
  • 3
  • 32
  • 47
13
votes
3 answers

How does one manually convert Apollo Android-generated objects into JSON strings and back?

I use the Apollo Android library to make queries to a GraphQL endpoint. Everything works OK until I try to convert the results back to JSON strings (to store them in a Room database). I naively tried to use Moshi, however this fails with the…
Giorgos Kylafas
  • 2,243
  • 25
  • 25
13
votes
1 answer

How to stop Moshi from parsing a specific object attribute

My JSON response (from a server) has attributes that are JSON objects, but I don't want to parse them all, instead I want to keep some of them as a JSON-encoded String. For instance: { "date": "23-03-2019", "changed": true, "data": { "login":…
Muhammadjon
  • 1,476
  • 2
  • 14
  • 35
12
votes
5 answers

R8 full mode throws class cast exception AGP 8.0

I recently upgraded to Android Studio Flamingo and AGP 8.0 My production builds with minfiy and shrinkResources enabled, doesn't allow retrofit to make calls. This is the error thrown. java.lang.ClassCastException: java.lang.Class cannot be cast to…
12
votes
1 answer

Moshi: problem with platform class BigDecimal

I have a class: @JsonClass(generateAdapter = true) data class DayAveragePriceModel( val asset: Asset, val value: BigDecimal ) where Asset is a custom class I have. I'm trying to use Moshi but I'm getting the following error: Caused by:…
noloman
  • 11,411
  • 20
  • 82
  • 129
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
  • 986,068
  • 189
  • 2,389
  • 2,491
12
votes
1 answer

How to parse a json list of multiple type in Moshi

Got a json list mixed with plain String and Image object like this: { "mixList": [ "string", { "imageUrl": "http://...", "height": 320, "width": 480 } ] } How to parse with Moshi? I would expect to have a…
Felix.D
  • 724
  • 6
  • 17
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
  • 39,001
  • 44
  • 144
  • 244
11
votes
2 answers

Kotlin Moshi Load Json from assets

I'm trying to load an assets Json file into my project with moshi. However, i keep getting the following error: com.squareup.moshi.JsonEncodingException: Use JsonReader.setLenient(true) to accept malformed JSON at path $ How should I load the…
11
votes
1 answer

Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

I'm trying to write a null-safe String adapter that will serialize this JSON {"nullString": null} into this: Model(nullString = "") so that any JSON with a 'null' value that I expect to be a String will be replaced with "" (assuming there exists a…
Mitch Ware
  • 415
  • 1
  • 4
  • 15
11
votes
2 answers

json formatting with moshi

Does anyone know a way to get moshi to produce a multi-line json with indentation ( for human consumption in the context of a config.json ) so from: {"max_additional_random_time_between_checks":180,"min_time_between_checks":60} to something like…
ligi
  • 39,001
  • 44
  • 144
  • 244
10
votes
0 answers

Configure Moshi for Room TypeConverter

Given that I'm already using Moshi to convert the Retrofit response from JSON String to Objects, can I also configure the Room Database on Android to use Moshi for TypeConverter instead of manually typing up the TypeConverter functions and…
Brandon Lim
  • 271
  • 2
  • 12
10
votes
2 answers

moshi custom qualifier annotation to serialise null on one property only

I'd like to serialise null for only one property in my JSON body that is going on a PUT. I don't want to serialize null for any other types in the object. Model class is like this @Parcel class User @ParcelConstructor constructor(var college:…
Jaskaran Singh
  • 113
  • 1
  • 5
9
votes
1 answer

How do I use Moshi to serialize a json string into org.json.JSONObject?

I have a JSON response from my server which is dynamic in nature and I cannot map it into a Kotlin Data Class. I would like to create a org.json.JSONObject out of it and parse it from there. I've looked around SO and Moshi's doc but couldn't find…
Jignesh Shah
  • 599
  • 1
  • 5
  • 13
9
votes
2 answers

How to fallback on a enum if values don't match in Moshi

I have an enum class and would like it to fallback to a specific enum value if values don't match any of them. I found a Moshi issue that talks about using EnumJsonAdapter but I don't see any public class for me to use. I'm using Moshi 1.8.0 Any…
Jayaprakash Mara
  • 323
  • 4
  • 11
1
2
3
35 36