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

Can't parse Map with Moshi in Kotlin - Unexpected primitive double

val moshi = Moshi.Builder().build() val type = Types.newParameterizedType(Map::class.java, String::class.java, Double::class.java) val adapter = moshi.adapter>(type) val result = adapter.fromJson(InitData.json) just switching…
Flo We
  • 325
  • 2
  • 12
7
votes
1 answer

Moshi Retrofit2 Kotlin Class Not Found Exception

I'm trying to learn how to implement Retrofit2 and Moshi inside the Kotlin programming language. However, I seem to be having trouble trying to compile my code. I define the following data classes/models which map to the json response I receive from…
Nicholas DiPinto
  • 307
  • 2
  • 13
7
votes
2 answers

Moshi Determine if JSON is array or single object

Is there a way to setup a Moshi adapter to automatically create a single Object or List based on the JSON response? Currently, I can do this explicitly. For example, I can receive the following responses: { "userId": "1", "id": "2", …
D. Beard
  • 121
  • 1
  • 8
7
votes
1 answer

How to create a complicate json using moshi without define the class?

In gson, I can use JsonElement to format a complicate json, something like(in kotlin): val objIDonotWantToDefine = JsonObject() objIDonotWantToDefine.addProperty("objName", "objIDonotWantToDefine") val playerJsonArray = JsonArray() …
Softcloud
  • 141
  • 1
  • 7
7
votes
1 answer

Deserialize a field that can be one of two data types using Moshi

I'm receiving some JSON from an OrientDB server that looks something like this: { ... "out": ..., "in": ..., ... } Now these two fields out and in can be one of two types: String and my own custom object (let's call it a Record).…
taternator5000
  • 105
  • 2
  • 9
6
votes
0 answers

Debug what's returning -> Error required value missing at $

So I usually have this typical error messages when using a new API com.squareup.moshi.JsonDataException: Required value 'X' (JSON name 'x') missing at $ at com.squareup.moshi.internal.Util.missingProperty(Util.java:649) at…
Barrufet
  • 495
  • 1
  • 11
  • 33
6
votes
1 answer

Enum parsing fails with moshi and R8

I have the following dependencies: moshi-codegen: 1.10.0 kotlin: 1.4.10 Android Gradle Plugin: 4.0.1 R8 is enabled in the build. At runtime, i got the following stacktrace when Moshi tries to parse enums java.lang.AssertionError: Missing field in…
WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
6
votes
0 answers

Moshi JsonAdapter for (desugared) java.time.LocalDate is not recognized

I have an Android app, and I'm trying to transition from ThreeTenABP to the Java 8 "time" APIs that are supported by Android Gradle Plugin 4.0 through desugaring Here is my custom Moshi adapter @Keep internal class DateTimeAdapter { @ToJson fun…
heyheyhey
  • 1,216
  • 8
  • 12
6
votes
1 answer

How to parse polymorphic objects in Kotlin using Moshi

Currently having an issue with PolymorphicJsonAdapterFactory and Kotlins sealed class. I have an API that that returns polymorphic home components and I am trying to parse and create polymorphic object using Moshi in Kotlin but I getting following…
user1288005
  • 890
  • 2
  • 16
  • 36
6
votes
2 answers

ClassNotFoundException with moshi in Unity

I made a library that uses Square's moshi to handle it's JSON serializations and a few other libraries like RxJava, dagger, etc. Since I want to use it on Unity I have to export an AAR from my own library and also add moshi in the unity project. I…
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
6
votes
3 answers

Moshi adapter to skip bad objects in the List

I use Moshi and I need to solve my problem with a buggy backend. Sometimes, when I request a list of objects, some of them don't contain mandatory fields. Of course, I can catch and process JsonDataException, but I want to skip these objects. How…
0wl
  • 836
  • 8
  • 19
6
votes
0 answers

Platform class java.util.Collections$SingletonMap (with no annotations) requires explicit JsonAdapter to be registered

I have to create and pass Moshi a custom JsonAdapter for java.util.Collections$SingletonMap type, if I get the error message correctly. The problem is, that SingletonMap is private in 'Collections'. val movies: MutableList =…
AlexKost
  • 2,792
  • 4
  • 22
  • 42
6
votes
1 answer

Converting a HashMap using moshi

I'm trying to convert a HashMap of elements into a JSON string. I'm using the method used in this link. val elementsNew: HashMap = HashMap(elements) val type = Types.newParameterizedType(Map::class.java, String::class.java,…
Sanka Darshana
  • 1,391
  • 1
  • 23
  • 41
6
votes
2 answers

How to parse unknown key jsons using Moshi

This is the example of JSON I am having filter : [ { category: [] } , { sub-category: [] } , { brand: [] } , { color: [] } ] Note that the labels "category,subcategory..." may vary dynamically How do I parse this JSON using…
Bala Krishna
  • 154
  • 2
  • 10
6
votes
1 answer

How to distinguish long and double-values when deserializing with moshi?

My goal is to synchronize abitrary rows of data by using the JSON-Format. As I do not know the exact scheme for the rows (it is a general sync method), my datamodel apparently has to rely on "Object". So in Java I will have an array of…
Sqrt-1764
  • 321
  • 1
  • 17