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
3
votes
5 answers

How to serialize an Object to Map by Moshi

I want to serialize an Object to Map by Moshi.Here is my codes by Gson public static Map toMap(Object obj, Gson gson) { if (gson == null) { gson = new Gson(); } String json = gson.toJson(obj); Map
Nano Java8
  • 41
  • 1
  • 1
  • 3
3
votes
2 answers

Gson or Moshi: field in POJO could have 2 types, how to save to any of the fields

EDITED: Here is the json string that I have: json#1 { [ { field1 : "" field2 : 0 field3 : "Amount not fixed" or field : 250 // this field can be string or int }, { field1 :…
iadcialim24
  • 3,817
  • 5
  • 21
  • 32
3
votes
1 answer

Moshi Unable to create converter for class with generic

Some of my server response for success are: { "error": null, "data": null } My model is: public class BaseResponse { @Json(name = "error") public ErrorModel error; @Json(name = "data") public E data; } If…
X3Btel
  • 1,408
  • 1
  • 13
  • 21
3
votes
1 answer

How to send Date object through Moshi JSON serializer?

I have to send Date object to server through API (actually, Date is object that server is expecting). I want to use Moshi, but I can't figure out how to use Custom Adapter to make it happen. Anyone?
jean d'arme
  • 4,033
  • 6
  • 35
  • 70
2
votes
0 answers

How to handle rubbish data in Moshi

I do have a json: { "items":[ { "id":"0000301553526", "values":{ "name":{ "value":{ "en_En":"Item" }, "formatted":{ …
Unii
  • 1,587
  • 15
  • 33
2
votes
1 answer

Parse JSON string to a list of objects in Kotlin Android (MOSHI?)

In my app, I'm using Retrofit to get the data from API (with flights data). I want to obatin List< Itinerary > from JSON, but the problem is that it is badly formatted, and I'm getting the itineraries seperately. I heard that it's possible to do…
deja
  • 176
  • 10
2
votes
1 answer

Moshi can't deserialize Map with List of values, returns LinkedHashTreeMap instead of value's class instance

Can't deserialize json to this structure: Map> I can deserialize to Map like this: ParameterizedType type = Types.newParameterizedType(Map.class, String.class, SomeClass.class); JsonAdapter
wanderbild
  • 43
  • 5
2
votes
1 answer

How to leave json object as raw string in Moshi

I want be able to save json object as raw json String. I've already have solution wit JSONObject, but it seems a bit redundant. Is there any way to get json string from reader? class ObjectAsStringFactory : JsonAdapter.Factory { override fun…
shagi
  • 629
  • 8
  • 25
2
votes
1 answer

Can't recieve byte array using Retrofit 2 in Kotlin

I have a DTO class using Moshi that's supposed to send and recieve byte arrays[], but it only works when sending them, because when I recieve then I have this exception. com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was STRING at…
Kanadista
  • 51
  • 4
2
votes
1 answer

Platform class java.util.Currency requires explicit JsonAdapter to be registered

Getting error while using Moshi code gen for data class with Currency. Referred similar questions. (Adding referred question at the end) Error Caused by: java.lang.IllegalArgumentException: Platform class java.util.Currency requires explicit…
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
2
votes
0 answers

No JsonAdapter for java.util.LinkedHashMap using moshi

Running this code val itemsInCart = viewModelScope.async { cartItemRepository = CartItemRepository() cartItemRepository?.getItemsInCartBySellerSuspend(email, sellerPK) } val setItems = arrayListOf() val getItemsInCart =…
imin
  • 4,504
  • 13
  • 56
  • 103
2
votes
0 answers

Could not resolve com.squareup.moshi:moshi-kotlin:1.13.0

Getting this error and unable to build Gradle file. Android Studio Version : Bumblebee Latest Patch Gradle Plugin Version : 7.1.2 Gradle Version : 7.2 I already tried adding android.jetifier.ignorelist=moshi-1.13.0 in Gradle properties but it's not…
oyeraghib
  • 878
  • 3
  • 8
  • 26
2
votes
1 answer

com.squreup.moshi.JsonDataException: Expected a string but was NULL at path $.data.gender

I need to parse data class from server request like this: @JsonClass(generateAdapter = true) data class User( @Json(name = "user_name") val userName: String, @Json(name = "gender") val gender: Gender?, ) { enum class Gender { …
2
votes
0 answers

Dalvik crash on Oneplus on Android 12

I've got an app in the play store, and have just started seeing the following crash on crashlytics as we're releasing a new version: Fatal Exception: java.lang.VerifyError: Verifier rejected class app.models.basket.BasketPrice:…
James Riordan
  • 1,139
  • 1
  • 10
  • 25
2
votes
1 answer

Kotlin + Moshi serialization of object type

I have following code in Kotlin: sealed class ParentClass data class ChildA(val prop: String): ParentClass() object ChildB: ParentClass() but when I try to serialize it into JSON with Moshi I get following Error: Caused by:…