dawidhyzy
02/26/2025, 3:01 PM<lint>
<issue id="UnsafeOptInUsageError" severity="ignore">
<option name="opt-in" value="kotlinx.serialization.InternalSerializationApi" />
</issue>
</lint>
Robert Jaros
03/02/2025, 7:24 PMthrowLinkageError("Function 'encodeToString' can not be called: No function found for symbol 'kotlinx.serialization.json/Json.encodeToString|encodeToString(0:0){0\xA7<kotlin.Any?>}[0]'");
I'm getting this when upgrading a Kotlin/JS project to Kotlin 2.1.20-RC.James
03/05/2025, 12:46 PMT
can either be Nothing
or something user-defined (that also has a @Serializable
annotation).
Whenever I try to serialize it I get an error:
kotlinx.serialization.SerializationException: Serializer for class 'Health' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
All my data classes are defined with Serializable, so I'm unsure of how to go forward with this. I was thinking about writing a custom serializer that implements KSerializer. But I'm not entirely sure where to start as I haven't really looked into writing serializers before. I want to avoid doing anything JSON specific, as the type should work for other formats than JSON as well.
Any advice appreciated.Mehmet
03/07/2025, 9:11 AMBoolean
, without requiring explicit @Contextual
annotations on every field?Charann
03/10/2025, 5:26 AMfun testSerializer(){
val str = """{"doubleVal":1.725255546765E12}"""
val json = Json {
isLenient = true
}
val obj = json.decodeFromString<DoubleVal>(str)
println(obj)
}
@Serializable
class DoubleVal(val doubleVal: Long)
But I got an error "Unexpected JSON token at offset 13: Unexpected symbol '.' in numeric literal at path: $.doubleVal"
I tried using flags like "isLenient" but of no use. Am I missing any flags?krzysztof
03/13/2025, 9:16 AMserializer has not been found for type 'Uuid'
, but there is one built-in from serialization lib itself - how to properly mark Uuid as serializable?J
03/18/2025, 8:01 AMZyle Moore
04/01/2025, 12:19 AM@SerialInfo
annotation that indicates that the property is tagged with the given name, @Field("HEDR")
. When a property has this annotation, it is encoded as a Type-Length-Value structure. As an example,
@Field("HEDR")
val header: String = "asdf"
Results in bytes matching
TT TT TT TT LL LL VV VV VV VV
(HEDR 04 00 asdf
)
But, to get this to happen, I have to override the decode*Element
method for each primitive, to support each serializable type. Example,
@Field("HEDR")
val header: String = "asdf"
@Field("INTV")
val taggedValues: Int = 1
Is there an easy/cheap way to perform this tagging on any type, without overriding that specific type in the codec?CLOVIS
04/03/2025, 11:41 AM1
and another writes 1.0
). Could KotlinX.Serialization be used to deserialize multiple JSON representations and tell me if they are equal or not?hellman
04/04/2025, 7:10 AM@Transient
and they are calculated in the init block, but that doesn't seem to be allowed. It works if I change it to a lateinit var
but that seems wrong. Is this a bug in kotlinx-serialization or is there a workaround?Smoothie
04/07/2025, 1:50 PMLukasz Kalnik
04/07/2025, 2:55 PMJsonPrimitive
and it will work out-of-the-box?
@Serializable
data class Response(
val field: JsonPrimitive
)
Emil Kantis
04/08/2025, 9:43 AM{
"foo": { // irrelevant },
"bar": { // irrelevant },
} -> // [ "foo", "bar ]
Currently I got a
data class Wrapper(
val entries: Map<String, Irrelevant>
)
But I would like to discard/ignore the values of the mapNikky
04/11/2025, 7:22 AMAndrey Tabakov
04/21/2025, 1:13 PMrocketraman
04/25/2025, 1:37 PMdata class FooBar(val id: TypedUuid<FooBar>, …)
and the serialization of id
be as simple as fb_<uuid>
.Marcello Galhardo
04/28/2025, 8:29 PMserializer<T>()
that throws if no serializer is found, why isn’t there a serializerOrNull<T>()
as well? I know there’s a serializerOrNull(KType)
, but as I understand it, typeOf<T>()
relies on reflection and has performance implications.S.
05/01/2025, 10:27 AMJsonObject
to specify response formats of a third party lib, is it somehow possible to retrieve a JsonObject from a @Serializable class MyClass
without writing the mapping by hand?Zyle Moore
05/11/2025, 3:02 AMAny
and getting an error that no serializer for Any
was found. I understand why, I'm just not quite sure how to get what I want with that being true. By default, something like
@Serializable data class Point(val x: Int, val y: Int)
will eventually make a call to beginStructure
, two calls to encodeInt
, and a call to endStructure
. If I replace Int
with any other primitive type, like Short
, Double
, Float
, Long
, the two calls in the middle will be the appropriate method for that type, encodeShort
, encodeFloat
, etc. I'm looking for something that behaves exactly the same way, but the encode method it calls is encodeSerializableValue
instead of encodeInt
. Or maybe even encodeInline
. This example though doesn't use generics, so perhaps it's easier for the generator to map confidently.
In my generic wrapper example, I have a
@Serializable @JvmInline value class FieldValue<T : Any>(val value: T) : FieldToken
The idea being a very thin wrapper around some serializable value, that I can treat as a subtype of FieldToken
. I have a containing class
@Serializable data class Record(val header: RecordHeader, val fields: List<FieldValue<Any>>)
which is where my trouble comes in. I want to be able to serialize, roughly, something that looks like
[ "TES4", [ flags, formId, timestamp, versionControl, recordVersion, unknown ] ],// RecordHeader
[ "HEDR", [ 1.7, 0, 0 ] ],// // FieldValue<(Float, Short, Short)>
[ "CNAM", "Zymus" ],// FieldValue<NullTerminatedString>
[ "SNAM", "TES4 JSON Tuple Example" ],// FieldValue<NullTerminatedString>
[ "MAST", "Skyrim.esm" ],// FieldValue<NullterminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "MAST", "Update.esm" ],// FieldValue<NullTerminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "MAST", "Hearthfires.esm" ],// FieldValue<NullTerminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "ONAM", [ 0 ] ],// FieldValue<List<Int>>
[ "INTV", 0 ],// FieldValue<Int>
[ "INCC", 0 ]// FieldValue<Short>
martmists
05/19/2025, 6:08 PMclass UuidSerializer : KSerializer<UUID>
but I want ""
to be decoded as null
rather than a UUID value.Lukasz Kalnik
05/20/2025, 8:16 AM"12345"
). Do I need to configure something special in the Kotlinx Serialization Json
object for it to be parsed?rad
05/24/2025, 5:03 PMjava.lang.AbstractMethodError: 'kotlinx.serialization.KSerializer[] kotlinx.serialization.internal.GeneratedSerializer.typeParametersSerializers()'
Full stacktrace: https://pastes.dev/UaaPnH2ZPY
My code:
@Serializable
@SerialName("data")
public data class DataMatcher(
public val predicate: Map<String, String>
) : ServerMatcher { ... }
where ServerMatcher
is a sealed serializable interface.
I'm using 1.8.1.y
05/27/2025, 9:22 AMSerializer has not been found for type 'MyType'
.
this is not an error for us with our current environment, and seems there are no plans to add a serializer for these.
can I suppress the error?Andrey Tabakov
05/28/2025, 10:13 AM@Serializable
data class MyConcreteDataClass(val name: String)
@Serializable
data class MyCommonClass(val value: MyConcreteDataClass)
I want to support a new implementation while also maintaining compatibility:
@Serializable
sealed interface AllDataClasses {
val name: String
}
@Serializable
data class MyConcreteDataClass(override val name: String): AllDataClasses
@Serializable
data class MyNewConcreteDataClass(override val name: String): AllDataClasses
@Serializable
data class MyCommonClass(val value: AllDataClasses)
May be there is a way to provide a fallback serializer if legacy object doesn’t point to polymorphic object?Zyle Moore
05/30/2025, 2:09 AMGRUP
, it is a Folder. If it is anything else, it is a File. During serialization, it tries to encode the full name of the type. I don't seem to be able to customize the discriminator behavior, and during deserialization, I get errors like
> kotlinx.serialization.SerializationException: Invalid index in polymorphic deserialization of unknown class
> Expected 0, 1 or DECODE_DONE(-1), but found -3
Ultimately, I'm wondering how to model this. My current method has been a List<FolderChild>
, but I don't think there's enough type information to do what I'm trying to do.CLOVIS
06/22/2025, 8:50 AMAbstractEncoder
.
Let's say I want to serialize:
@Serializable
class User(val id: Int)
From what I understand, KotlinX.Serialization will call:
MyEncoder.beginStructure(User.serializer.serialDescriptor).apply {
encodeInt(1234)
endStructure(User.serializer.serialDescriptor)
}
What I don't understand is: when encodeInt(1234)
is called, how do I know it's the field id
and not another field that could exist? Am I supposed to store the serialDescriptor
of the enclosing class, keep track of the index of the last written element, and use that to query the descriptor to know what the field is?
TL;DR: within the implementation of encodeInt()
I need to find to know the current field's name, but I don't know how to.Lukas K-G
06/25/2025, 6:23 AM2.0.0
.
(I figured by now that this is probably a miss-configuration but haven't found any definitive documentation yet stating which plugin version to use with which Kotlin version.)
Now the strange thing: One of the projects produces Kotlin metadata annotations with version 2.0.0
and one with 1.9.0
. When aligning the plugin version with the Kotlin version both produce 1.9.0
metadata.
Can someone help me understand what might be influencing this?Adam S
06/25/2025, 2:58 PM{}
import kotlinx.serialization.*
import kotlinx.serialization.json.*
fun main() {
val a = A()
println(a)
println(Json.encodeToString(a))
}
@Serializable
data class A(
val list: MutableList<String> = mutableListOf("a", "b", "c"),
)
A(list=[a, b, c])
{}
https://pl.kotl.in/iubN0SLUBJoshua Hansen
06/26/2025, 9:06 PMA
, that my application reads has changed. I used the JsonNames
annotation to account for properties that were simply renamed, but there is one property in particular whose name didn't change that went from being (in typescript speak) string[]
to { foo: string, bar: string[] }
. What's the best way to handle this property would could either be a list of strings or a specific class with two properties?
Should I:
• Create a custom serializer for this property which inspects the JSON structure of the property to determine which one it is?
• Create an entire alternative class for A
and use it as a surrogate after initially trying and failing to deserialize via the old format?
• Some other option?Scott Fedorov
07/03/2025, 11:00 PM{ "a": 1, "b":2 }
and want to start storing a new set of polydata, where it would end up serializing { "a": 1, "b":2, "c":2, "type": "com.example.WibbleV2" }
... the problem is the existing records would NOT have the type field for it to know it was V1.... trying to avoid the work of custom deserializers.