Adam S
04/03/2022, 9:55 PM@Serializer
annotated class and generate some TypeScript interfaces - so they can generate compatible JSON.
The basics are in, and it needs more work to finish up some of the tricky bits. Please take a look!
https://github.com/adamko-dev/kotlinx-serialization-typescript-generator/
It's available from JitPack. There's code examples in the docs directoryLuoqiaoyou
04/04/2022, 12:31 PMribesg
04/05/2022, 10:06 AMste
04/05/2022, 6:21 PM{ "nav": { "watch:" {...} } }
{ "nav": { "browse:" {...} } }
How can I deserialize Nav
based on its key ("watch" | "browse"
)?
This is what I tried:
@Serializable
data class Nav(
val action: Action
)
@Serializable
sealed class Action {
@Serializable
@SerialName("watch")
data class Watch(...) : Action()
@Serializable
@SerialName("browse")
data class Browse(...) : Action()
}
But it doesn't work - Field 'action' is required for type with serial name Nav
; what am I missing?martypitt
04/07/2022, 12:08 AMjava.lang.NoSuchMethodError: 'void kotlinx.serialization.internal.ObjectSerializer.<init>(java.lang.String, java.lang.Object, java.lang.annotation.Annotation[])'
Inside`kotlinx.serialization.internal.ObjectSerializer`, I see the constructor. (It's scoped internal
, not sure how scoping rules apply here, given the code is generated)
@PublishedApi
@OptIn(ExperimentalSerializationApi::class)
internal class ObjectSerializer<T : Any>(serialName: String, private val objectInstance: T) : KSerializer<T> {
@PublishedApi // See comment in SealedClassSerializer
internal constructor(
serialName: String,
objectInstance: T,
classAnnotations: Array<Annotation>
) : this(serialName, objectInstance) {
_annotations = classAnnotations.asList()
}
christophsturm
04/07/2022, 9:16 AM@SerialId
is serialized to protobuf? (protobuf serializes enums as ints and when they have no serialId the order matters. In json thats not a problem because enums are serialized as string.Landry Norris
04/07/2022, 6:46 PMJorge Bo
04/08/2022, 5:45 PMjson {
serializersModule = SerializersModule {
contextual(Map::class) { MarketMapSerializer }
contextual(Map::class) { StationMapSerializer }}
}
object MarketMapSerializer :
JsonTransformingSerializer<Map<String, Market>>(MapSerializer(String.serializer(), Market.serializer())) {
override fun transformDeserialize(element: JsonElement): JsonElement {
return JsonObject(element.jsonObject.filterNot { (k, _) ->
k == "token"
})
}
}
object StationMapSerializer :
JsonTransformingSerializer<Map<String, Station>>(MapSerializer(String.serializer(), Station.serializer())) {
override fun transformDeserialize(element: JsonElement): JsonElement {
return JsonObject(element.jsonObject.filterNot { (k, _) ->
k == "token"
})
}
}
and i'm getting this exception. Exception in thread "main" kotlinx.serialization.modules.SerializerAlreadyRegisteredException: Contextual serializer or serializer provider for class kotlin.collections.Map already registered in this module. Is it possible to specify types in contextual mapping?Ayfri
04/18/2022, 8:27 PMclient
& environment
, and if only client
is set, it's saved as a String, else as an object containing both ?Ayfri
04/18/2022, 10:08 PMoverride fun transformSerialize(element: JsonElement): JsonElement {
require(element is JsonArray) // this serializer is used only with lists
return element.singleOrNull() ?: element
}
Which is the exact code from the documentation but I'm getting nothing when there's one string and an array if multiple, what I'm doing wrong 🤔Joel Steres
04/18/2022, 11:56 PM@Serializable
data class Xxx(...) {
fun merge(apiSnapshot: String): JSONObject {
val merged = JSONObject(apiSnapshot)
Json.encodeToJsonElement(this).jsonObject.entries.forEach { (key, value) ->
if (value == null) {
merged.remove(key)
} else {
merged.put(key, value)
}
}
return merged
}
}
However, the values are from the serialized JsonObject
are all strings, including nulls. Is there a way to do this that preserves the type? I also tried encodeToString
and parsing with JSONObject
, which does preserve the types, but I lost nulls when I did this. I could combine the two approaches but I’m hoping there is a more elegant approach.christophsturm
04/19/2022, 10:02 AMlistOf(MissingField("surname"), WrongType("age", was=INT, expected=STRING))
, and also a path to field names when something is missing in a nested object.Ayfri
04/19/2022, 7:50 PMvalue
field, using an interface to ensure that field exists), and deserialize as the enum value
For now I have the serializer working and it was pretty easy, but what should be the deserializer ?hugo.matilla
04/20/2022, 2:13 PM{
"0": {"name": "John"},
"1": {"name": "Jane"},
"2": {"name": "Pete"},
...
}
I'm super new to the Serialization library, and I can't find which will be the best approach to deserialize it and have the id as part of the object? Something like this:
Person (id: Int, name: String)
Thank you🙂George
04/21/2022, 6:46 AMVivek Modi
04/22/2022, 11:14 AMViktor Orlyk
04/24/2022, 3:56 PMkierans777
04/25/2022, 3:54 AMkotlinx-serialization
in my MPP library, and all the tests run successfully on the JVM target. However when running iosX64Test
I'm getting a segmentation fault. I've stripped the code right back and I'm not sure what's causing it.
Can someone help me understand how to debug this please?Grouvie
04/27/2022, 9:39 AM[2,"6bd18013-b33f-43bb-bf91-7745e5517dd2","StatusNotification",{"connectorId":1,"errorCode":"NoError","status":"Available"}]
and
[2,"5a3a42a2-212d-4815-9581-f64ce55fd6cc","Heartbeat",{}]
over websockets to which I then answer:
[3,"6bd18013-b33f-43bb-bf91-7745e5517dd2",{}]
and
[3,"5a3a42a2-212d-4815-9581-f64ce55fd6cc",{"currentTime":"2022-04-27T11:29:38.179938700"}]
.
What is the most elegant way to deserialize, serialize and answer them?bbaldino
04/27/2022, 4:49 PM@Serializable
class Foo {
var id: String by Delegates.observable("initialId", ::onChange)
private fun onChange() {
keyStore.set("foo", Json.encodeToString(this))
}
}
but this won’t work because delegates don’t get serialized. Is there a good way to accomplish something like this?Joel Steres
04/29/2022, 12:46 AMSebastian Schuberth
05/02/2022, 4:36 PMEnum
, the "common base class of all enum classes", but I'm struggling with the syntax for the generic type parameter. Has some done this successfully?Landry Norris
05/02/2022, 9:19 PMbbaldino
05/02/2022, 11:55 PMtype
field when serializing polymorphic classes…I’m curious if it’s possible to customize the name of that field (type
) via an annotation? Or would that require a custom serializer?hfhbd
05/04/2022, 7:40 AMdescriptor.getElementAnnotations(index)
, but the annotation for this property is not found. Did I miss something?hfhbd
05/06/2022, 10:04 AMasarazan
05/07/2022, 11:32 PMdave08
05/08/2022, 10:57 AMdany giguere
05/08/2022, 3:30 PMobject Cars: IntIdTable() {
val name = varchar("name", 128)
val year = integer("year")
}
class Car(id: EntityID<Int>): IntEntity(id) {
companion object : IntEntityClass<Car>(Cars)
var name by Cars.name
var year by Cars.year
}
//@Serializable
class SerializedCar(id: EntityID<Int>, name: String, year: Int): IntEntity(id)
then :
class CarDAO {
private fun resultRowToCar(row: ResultRow) = SerializedCar(
id = row[Cars.id],
name = row[Cars.name],
year = row[Cars.year]
)
suspend fun show(id: Int): SerializedCar? = transaction {
Cars
.select { Cars.id eq id }
.map(::resultRowToCar)
.singleOrNull()
}
}
and my route is :
get("/cars/{id}") {
val id = call.parameters.getOrFail<Int>("id").toInt()
call.respond(mapOf("car" to carDAO.show(id)))
}
but I get this error :
500: kotlinx.serialization.SerializationException: Serializer for class 'SerializedCar' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
You can see the project here for more info:
<https://github.com/danygiguere/ktor-kotlin-example>
And if I uncomment //@Serializable
, my IDE tells me : Impossible to make this class serializable because its parent is not serializable and does not have exactly one constructor without parameters
Mendess
05/11/2022, 3:59 PM@Serializable
infrastructure? Aka, is it possible for me to define something like MyFormat.decodeFromString
and MyFormat.encodeToMyFormat
Mendess
05/11/2022, 3:59 PM@Serializable
infrastructure? Aka, is it possible for me to define something like MyFormat.decodeFromString
and MyFormat.encodeToMyFormat
Mitchell Syer
05/11/2022, 4:03 PMMendess
05/11/2022, 4:03 PMMitchell Syer
05/11/2022, 4:05 PMMendess
05/11/2022, 4:06 PMandylamax
05/12/2022, 6:30 AM