Lukasz Kalnik
03/01/2024, 12:45 PM@Serializable
. But are they being prevented from obfuscation in such case (by the default kotlinx.serialization Proguard rules)?Miguel Oliveira
03/01/2024, 1:56 PMRobert Jaros
03/01/2024, 3:54 PM2.0.0-Beta4
with serialization 1.6.3
I see these warnings when using public typealias Decimal = @Serializable(with = DecimalSerializer::class) InternalDecimal
(following https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#specifying-serializer-globally-using-typealias):
w: file:///home/rjaros/git/kilua/modules/kilua-common-types/src/jvmMain/kotlin/dev/kilua/types/serializers/DecimalSerializer.jvm.kt:39:5 Annotation `@Serializable(with = DecimalSerializer::class)` is missing on actual declaration.
All annotations from expect `dev.kilua.types.serializers.DecimalSerializer.deserialize` must be present with the same arguments on actual `dev.kilua.types.serializers.DecimalSerializer.deserialize`, otherwise they might behave incorrectly.
w: file:///home/rjaros/git/kilua/modules/kilua-common-types/src/jvmMain/kotlin/dev/kilua/types/serializers/DecimalSerializer.jvm.kt:43:5 Annotation `@Serializable(with = DecimalSerializer::class)` is missing on actual declaration.
All annotations from expect `dev.kilua.types.serializers.DecimalSerializer.serialize` must be present with the same arguments on actual `dev.kilua.types.serializers.DecimalSerializer.serialize`, otherwise they might behave incorrectly.
Stylianos Gakis
03/01/2024, 11:00 PM@Required
in order to ensure that during deserialization process, even if a class has a default value, the input still needs to have the field present.
Is there something for the opposite, where I got a class like
@Serializable
internal data class Foo(
val bar: String,
) {
val baz: String = "fixed_value"
}
and I do in fact want the serialization process for this for object Foo("someInput")
to serialize into this
{
"bar": "someInput",
"baz": "fixed_value"
}
jamshedalamqaderi
03/02/2024, 6:00 AMokarm
03/05/2024, 12:23 PM1
and 2
in the following snippet? Maybe 1
will be problematic in native code? I seem to remember these reified inline functions threw at runtime in K/N, is that still a thing?
object UUIDSerializer : KSerializer<UUID> {
override val descriptor: SerialDescriptor = serialDescriptor<String>() // (1)
override val descriptor: SerialDescriptor = String.serializer().descriptor // (2)
...
}
DareFox
03/05/2024, 1:06 PM@Serializable
sealed class Parser {
abstract fun parse(text: String)
}
@Serializable
@SerialName("XParser")
object XParser: Parser() {
override fun parse(text: String) {}
}
@Serializable
@SerialName("YParser")
object YParser: Parser() {
override fun parse(text: String) {}
}
@Serializable
data class Settings(val parsers: List<Parser>)
fun main() {
val settings = listOf(XParser, YParser)
Json.encodeToString(settings)
}
Gives {"parsers":[{"type":"XParser"},{"type":"YParser"}]}
that could be better represented as {"parsers": ["XParser", "YParser"]}
(like enum serialiaztion) since every object will not have value, only type.
I know that limitation exists because children of sealed class can be non-singleton, but is there a way to override it? Or is there a better way to do this?Fred Bowker
03/06/2024, 1:41 PM@Singleton
class PostServiceWeb
@Inject
constructor(
properties: AppProperties,
networkJson: Json,
) : ServiceWeb(
to use a Json that has
Json { encodeDefaults = true }
Basically I want to create a default JSON that has this set then it would be injected across the application, or do people change this at the retrofit level, any help would be appreciatedYuri
03/06/2024, 4:05 PM@Serializable
data class Xxx(...)
object XxxSerializer : KSerializer<Xxx> {
private val delegate = Xxx.serializer()
... custom serialize/deserialize logic using delegate
}
val json = Json {
serializersModule = SerializersModule { contextual(Xxx::class, XxxSerializer) }
}
json instance is being used for Retrofit converter factory which performs deserialization with json.serializersModule.serializer(type)
.
This code picks default serializer instead of custom one, because Xxx
has @Serializable
annotation.
Is it possible to force runtime to use contextual serializer for this class?
Seems like @UseContextualSerialization
should help, but I couldn't figure out correct syntax.Stylianos Gakis
03/06/2024, 4:41 PM@Serializable
data class Outer(val inner: Inner)
@Serializable
@JvmInline
public value class Inner(public val url: String)
I then realized that value classes do not get exported to our iOS target properly, so I am thinking I’d rather just make it not be a value class anymore.
However this then has implications on serialization. For an object like Outer(Inner("someValue"))
Before, with value
keyword:
{
"inner": "someValue"
}
After, without the value
keyword:
{
"inner": {
"url": "someValue"
}
}
Is there a way to still keep the old behavior while not having my class be a value class and therefore not degrade the experience of the iOS target?Zakhar
03/07/2024, 3:26 PMMap<String, JsonElement?>
.
All strings in values of the map will have quotes at the start and at the end. E.g instead of just consumable
a string will look like "consumable"
How to avoid this?
Tried to use org.json.JSONObject. And in this case the result looks correctly.dave08
03/11/2024, 1:29 PM@Serializable data class Foo(val id: Int, val name: String) { val computed: String? = "$id --- $name" } // computed doesn't appear in the resulting json...
how can I get this to work?curioustechizen
03/12/2024, 12:24 PMJsonClassDiscriminator
and iOS?
I'm using that in combination with a sealed class hierarchy. On Android, the value that I specified in this annotation is respected. On iOS, it just uses the default ("type"
).ursus
03/19/2024, 1:19 PM@Suppress("FunctionName")
inline fun <reified T : Any> JsonColumnAdapter2(json: Json): ColumnAdapter<T, String> {
return JsonColumnAdapter2(json, Json.serializersModule.serializer())
}
can I somehow constrain this so callsites can only pass-in @Serializable
types, as to avoid runtime crashes?y
03/19/2024, 3:17 PMdata object
instead of object
, solve the issue of needing to implement readResolve()
?Noah Sragow
03/19/2024, 6:46 PMsnowe
03/19/2024, 9:21 PM@Serializable(with = TaxSerializer::class)
interface LineResponseTaxesInner {
and then my custom serializer
object TaxSerializer : JsonContentPolymorphicSerializer<LineResponseTaxesInner>(LineResponseTaxesInner::class) {
override fun selectDeserializer(element: JsonElement) = when {
"FullDetails" in element.jsonObject -> Tax.serializer()
else -> TaxSummary.serializer()
}
}
Weirdly this works completely fine locally, but fails when compiled as a graalvm native image and run in aws…Slackbot
03/20/2024, 12:35 PMursus
03/20/2024, 2:38 PMKSerializer<List<Foo>>
How do I get the List serializer reference?
I have ListSerializer(Foo.serializer())
but that creates a new instance every time, i.e. it's not cached the way it would if I were to just leave it up reified generics to get the serializer via serializersModule.serializer<List<Foo>>()
zt
03/22/2024, 6:16 AMLuc Girardin
03/22/2024, 10:42 AMList<Map<String, Any?>>
. Seems that I need to register a PolymorphicSerializer for that purpose, but I didn’t figure out how to exactly do it so that I can pass it to the encodeToString()
method to get JSON output. Would very much appreciate help!Jon Bailey
04/06/2024, 2:07 PMLaxystem
04/08/2024, 3:31 PM@Serializable(with = FooSerializer::class)
public sealed interface Foo
Then will all subclasses of Foo
be serialized with FooSerializer
, or do I need to create a serializer for them too?oSumAtrIX
04/11/2024, 10:53 PMkotlinx.serialization.SerializationException: Class 'LinkedHashSet' is not registered for polymorphic serialization in the scope of 'Set'.
To be registered automatically, class 'LinkedHashSet' has to be '@Serializable', and the base class 'Set' has to be sealed and '@Serializable'.
Alternatively, register the serializer for 'LinkedHashSet' explicitly in a corresponding SerializersModule.
at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:233)
at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:28)
at kotlinx.serialization.json.Json.encodeToString(Json.kt:81)
But the docs suggest that a Set is serializableZyle Moore
04/14/2024, 1:40 AMwith
property of the @Serializable
annotation?y
04/14/2024, 1:04 PMPair
? if not, what's the least boilerplatey way to support this?Stylianos Gakis
04/15/2024, 12:25 PMproguard-rules.pro
file and we had a bunch of things there related to kotlinx.serialization.
I was trying to look into cleaning up any potential unused things, and going through the docs
After that, I tried just commenting out all of our rules and running the app in release mode with r8 and everything and everything seems to just work fine? Maybe everything we had is now obsolete because either kotlinx.serialization now has them in the library itself or we were doing something weird back in those days?
So my question is, do you use any proguard rules in your android apps when using kotlinx.serialization nowadays? If yes what are you using and why?Zyle Moore
04/17/2024, 2:16 AM0xDEADBEEF
at the start of a binary stream. I imagine something magic like this wouldn't vary on a per-instance level, and so wouldn't be part of the Serializable class's public properties. So far I just call a couple encodeX
methods in a hand-built serializer.pajatopmr
04/19/2024, 5:12 AM$$serializer()
is one and I believe that there are others generated for a property of type List, for example @Serializable data class KoverData(val list: List<String> = emptyList())
My goal is to write a test for this data class that Kover will report as 100% covered. If you have an answer, or even a clue, please do tell.Zyle Moore
04/19/2024, 6:30 AMPID=1.1
I made a serializable data class to represent this
@Serializable
data class Pid(
val first: Digits,
val second: Digits? = null,
)
If second
is absent, then the period and second number are not included.
Pid("1") == PID=1
Pid("1", "1") == PID=1.1
I'm making a hand-built custom serializer, but can't figure out how the descriptor aligns to the serialization and deserialization methods. Does second
become a substructure made up of separator value
".1"
Is it inline? Would it be elements[2]
of the base descriptor, or closer to elements[1][1]
?