-1

Iam unable to convert this Json, keep getting the following error: com.squareup.moshi.JsonDataException: Expected a string but was BEGIN_OBJECT at path

{"summary": {
    "rows": [
    [
        "08 May, 2023",
        {
            "name": "some item name",
            "index": 0
        },
        "Test",
        "Test"
    ],
    [
        "09 May, 2023",
        {
            "name": "some other item name",
            "index": 0
        },
        "Test",
        "Test"
    ]
    ]

}
}

The only solution i got was using @RawValues

Ekanath
  • 24
  • 4

1 Answers1

0

As you said, the two classes you have to create are :

data class Summary(val rows: List<List<Any>>)
data class Root(val summary: Summary)

Then to have the Root object from a json you have to create the adapter as follows

val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(Root::class.java)
val root = adapter.fromJson(json)

Edit

As the error message says

Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue

What it is saying is that not all types can be serialized using Parcel, and therefore cannot be used directly with @Parcelize, so that's why it recommends you to use @RawValue.

The @RawValue annotation is used to indicate that the annotated parameter should be treated as a raw value by the compiler. When used with @Parcelize, it tells the compiler to not generate any serialization or deserialization code for the annotated parameter.

You should add @RawValue to the attribute that is Any.

In your example

data class Summary(@RawValue val rows: List<List<Any>>)

Ref:

How to parcel value with type any using parcelize

Rawvalue annotation is not applicable to target value parameter

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • if i use **Any?** i get the following error: Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue – Ekanath May 08 '23 at 13:20