Hi! Did anyone had issue serializing `Map<Strin...
# javascript
c
Hi! Did anyone had issue serializing
Map<String, MyClass>
? I have the following class which contains a Map<String, Meta>
Copy code
@JsExport
@ExperimentalJsExport
@Serializable
data class TvChannel internal constructor(
    @SerialName("metas")
    private val metas: Map<String, Meta>? = null,
)
The Meta interface has a polymorphic serializer.
Copy code
@OptIn(ExperimentalJsExport::class)
@Serializable(with = MetaSerializer::class)
interface Meta {
  val objectType : String?
}
In Js I get the following error for this code:
Copy code
val myTvChannel = TvChannel()
console.log(myTvChannel)
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'LinkedHashMap'
|     property 'ab_1' -> object with constructor 'ChainEntry'
--- property 'ib_1' closes the circle
at JSON.stringify (<anonymous>)
at Inspector.ts:129:1
at Array.forEach (<anonymous>)
at Inspector.addMessageToLogsContainer (Inspector.ts:127:1)
at console.log (Inspector.ts:38:1)
With
Map<String, String>
it works, but I don't understand why it is not working with
Map<String, MyClass>
.
a
Hey, what version of the compiler do you have?
c
1.8.22
a
Could you check if the exact code works with the 1.9.10?
👍 1
c
I get the same error with 1.9.10
i
it's because of the structure of maps, are you sure you're using the kotlin serialization json and not accidentally using the js json?
c
In my case, I have a KMP library which is exported as an npm package, and it is imported to a js project. The library returns the mentioned TvChannel class to the client app. The client app uses console.log() (which is, based on the stacktrace, uses the js json, JSON.stringify() ) on the TvChannel instance. Correct me if I'm wrong but in this case I think its unavoidable for the client app, to use js json.
i
I have a client app and I use js json and kotlin serialization json where necessary.