Serialization of Set as Collection does not work
See original GitHub issueDescribe the bug
I’m using Retrofit with kotlinx.serialization and the following code:
@Serializable
data class PackagesWrapper(
val purls: Collection<String>
)
@POST("api/packages/bulk_search")
suspend fun getPackageVulnerabilities(@Body packageUrls: PackagesWrapper): List<PackageVulnerabilities>
When calling service.getPackageVulnerabilities(PackagesWrapper(packageMap.keys))
where keys
is a Set
, I get
java.lang.ClassCastException: class java.util.LinkedHashMap$LinkedKeySet cannot be cast to class java.util.List (java.util.LinkedHashMap$LinkedKeySet and java.util.List are in module java.base of loader 'bootstrap')
To Reproduce
Checkout https://github.com/oss-review-toolkit/ort/tree/vc2kxs-bug and run ./gradlew :advisor:test --tests org.ossreviewtoolkit.advisor.advisors.VulnerableCodeTest
.
Expected behavior
As a Set
is a Collection
that can be serialized to a JSON array, I’d expected this to just work. However, ArrayListSerializer
seems to hard-code in ListLikeSerializer<E, List<E>, ArrayList<E>>(element)
that the “list like” collection needs to be a List
.
Environment
- Kotlin version: 1.4.32
- Library version: 1.1.0
- Kotlin platforms: JVM
- Gradle version: 7.0
- IDE version (if bug is related to the IDE): IntellijIDEA 2021.1
- Other relevant context: Windows 10 64-bit
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Collection serialization issues - java - Stack Overflow
Inside of user class i have multiple variables (ints, Strings, etc.) and few Collections. Problem is in overwriting the file. 2 out of...
Read more >Tuning - Spark 3.3.1 Documentation - Apache Spark
This setting configures the serializer used for not only shuffling data between worker nodes but also when serializing RDDs to disk. The only...
Read more >How to serialize properties of derived classes with System ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >JSON Serialization Usage
This causes problems when you want a connected object model in your server, but you can't send that object model to the client...
Read more >Serialization Guide - Json.NET
NET collections are not serialized. In situations where a type implements IEnumerable but a JSON array is not wanted, then the JsonObjectAttribute can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oh sorry, I think I’ve got distracted and mistyped. I meant
Use-case ’ a Set and sometimes a List, and want to avoid an additional toList() conversion’ looks reasonable
I filed a draft PR to move forward with this, in the hope that tests would be executed as part of PR checks, but unfortunately no tests seem to be run as part of the PR 😢