Kotlinx.serialization: how to parse JSONArray to List<CustomObject> ?

Created on 22 Jul 2018  路  13Comments  路  Source: Kotlin/kotlinx.serialization

I'm trying without success to serialize JSONArray to local variable List of CustomObjects, when CustomObject class is Serializable, i reread all the q&a, all the documents, I can not understand how this is done, may be can you give an example?

question

Most helpful comment

If actually want to parse a root-level array, you have to create serializer for List<CustomObject> and pass it to JSON.parse. Serializers are constructed from each other: ArrayListSerializer(CustomObject.serializer()), or, for lists, sets and maps, there is extension property: CustomObject.serializer().list.

Same for ReferenceArraySerializer(...), which gives you KSerializer<Array<...>>

All 13 comments

Also Array<CustomObject>.

If actually want to parse a root-level array, you have to create serializer for List<CustomObject> and pass it to JSON.parse. Serializers are constructed from each other: ArrayListSerializer(CustomObject.serializer()), or, for lists, sets and maps, there is extension property: CustomObject.serializer().list.

Same for ReferenceArraySerializer(...), which gives you KSerializer<Array<...>>

I'm sorry, but I could not get the result,
how to create a serializer for this List, and where to pass it?

Here is my code:
val list: List <HotDeal> = JSON.parse (jsonArrayString)
I saw there are people who have the same issue, maybe you can give a more illustrative example based on my case, it will help me further with other lists in this project, and other people will be able to use this answer.

thank you in advance!

@xdma val list: List<HotDeal> = JSON.parse(HotDeal.serializer().list, jsonArrayString)

@sandwwraith
I tried but HotDeal.serializer() = Unresolved reference

Is your HotDeal marked @Serializable? If so, you need to install corresponding plugins. I believe you've just missed IDEA plugin and the code is show is red, but gradle build will succeed.

Yes, HotDeal marker @Serializable, I'm trying to install the IDE plugin

@sandwwraith
Thanks, you really helped me!

褉械邪谢褜薪芯 褋锌邪褋懈斜芯, 薪械 蟹薪邪褞 泻邪泻 锌褉芯锌褍褋褌懈谢 褔邪褋褌褜 锌褉芯 锌谢邪谐懈薪 !

@sandwwraith Hi, I'm using android studio and I already applied the gradle plugin following readme but in compile time it still can't find serializer() reference. All of my class are annotated with @Serializable and they can work well when I don't serialize a list explicitly. Basically I have a class like

@Serializable
class A {
    val b: List<B>
}

It works well when I use JSON.stringify(a) but it failed if I want to only do JSON.stringify(a.b). Then I did JSON.stringify(A.serializer().list, a.b). It failed my compilation. Do you know why? Thanks

NVM, I figured. I need to do JSON.stringify(A::class.serializer().list, a.b)

On Android Studio, I was seeing syntax highlighting error on .serializer() with message "Unresolved reference: serializer()", but the code was able to compile without issue.

I checked my kotlin compiler version, added gradle plugins, applied the plugins, and updated Android Studio. I still saw the issue

Turns out, I only had to restart Android Studio after the update and the problem went away. Hopefully this saves some time for someone else! 馃槃

Edit: After running my app, I see the syntax highlighting again, so it appears the issue isn't fixed. Is this a known issue? Or is it likely an issue with how I set up my plugins?

@robert-w-gries What version of Android Studio do you use? Any particular steps to reproduce?

In my quest to reproduce the issue, I found out that updating the following fields in my build.gradle fixed the problem:

- compileSdkVersion 27
+ compileSdkVersion 28
- targetSdkVersion 27
+ targetSdkVersion 28
Was this page helpful?
0 / 5 - 0 ratings