Vaibhav Jaiswal
02/01/2024, 1:29 PM@Serializable
sealed interface PostMediaDto {
companion object {
val serializerModule = SerializersModule {
polymorphic(PostMediaDto::class){
subclass(Image::class)
subclass(Link::class)
defaultDeserializer { NotImplemented.serializer() }
}
}
}
@Serializable
@SerialName("IMAGE")
data class Image(val data: List<String>) : PostMediaDto
@Serializable
@SerialName("LINK")
data class Link(val data: List<String>) : PostMediaDto
@Serializable
data object NotImplemented : PostMediaDto
}
zt
02/01/2024, 9:02 PMMichael Paus
02/04/2024, 10:59 AMAerodromeCollection
which is marked as beeing @Serializable. Now I want to add some method to easily save that as a JSON file. My first attempt was:
fun AerodromeCollection.exportAsJson(jsonFilePath: Path, fileSystem: FileSystem) {
fileSystem.write(jsonFilePath) {
writeUtf8(jsonFormat.encodeToString(this))
}
}
This compiles and runs but leads to this strange exception:
kotlinx.serialization.SerializationException: Serializer for subclass 'RealBufferedSink' is not found in the polymorphic scope of 'BufferedSink'.
More Details inside thread.Tudor Luca
02/06/2024, 1:52 PMelectrolobzik
02/08/2024, 4:27 PMexpect class
?
I have the following class in the common module:
expect class PlatformFileLocation
and this in the Android module:
@Serializable
data class AndroidFileLocation(
@Serializable(with = PathSerializer::class)
val filePath: Path,
val fileProviderAuthority: String
)
actual typealias PlatformFileLocation = AndroidFileLocation
If I mark the PlatformFileLocation
as @Serializable
I get an error. But without this I can’t get serializer in the common code. Is there any trick to make it work?Emil Kantis
02/09/2024, 7:56 PM@Serializable
? Preferably in a multiplatform-compatible way윤동환
02/13/2024, 7:55 AM16:50:11: Executing 'compileKotlin'...
> Configure project :
Profile: dev
> Task :checkKotlinGradlePluginConfigurationErrors
> Task :kaptGenerateStubsKotlin FAILED
2 actionable tasks: 2 executed
e: java.lang.AssertionError: Recursion detected on input: IMPORT_DIRECTIVE under LockBasedStorageManager@1f28f64d (TopDownAnalyzer for JVM)
at org.jetbrains.kotlin.resolve.lazy.LazyImportResolver.getImportScope(LazyImportScope.kt:129)
at org.jetbrains.kotlin.resolve.lazy.LazyImportScope$getClassifier$1.invoke(LazyImportScope.kt:279)
at org.jetbrains.kotlin.resolve.lazy.LazyImportScope$getClassifier$1.invoke(LazyImportScope.kt:274)
at org.jetbrains.kotlin.storage.LockBasedStorageManager.compute(LockBasedStorageManager.java:290)
at org.jetbrains.kotlin.resolve.lazy.LazyImportScope.getClassifier(LazyImportScope.kt:274)
at org.jetbrains.kotlin.resolve.lazy.LazyImportScope.getContributedClassifier(LazyImportScope.kt:270)
at org.jetbrains.kotlin.resolve.scopes.ResolutionScope$DefaultImpls.getContributedClassifierIncludeDeprecated(ResolutionScope.kt:40)
at org.jetbrains.kotlin.resolve.scopes.HierarchicalScope$DefaultImpls.getContributedClassifierIncludeDeprecated(Scopes.kt:27)
at org.jetbrains.kotlin.resolve.scopes.ImportingScope$DefaultImpls.getContributedClassifierIncludeDeprecated(Scopes.kt:113)
at org.jetbrains.kotlin.resolve.lazy.LazyImportScope.getContributedClassifierIncludeDeprecated(LazyImportScope.kt:235)
at org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt.findFirstClassifierWithDeprecationStatus(ScopeUtils.kt:119)
at org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.findClassifierAndReportDeprecationIfNeeded(QualifiedExpressionResolver.kt:82)
at org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.resolveDescriptorForType(QualifiedExpressionResolver.kt:106)
at org.jetbrains.kotlin.resolve.TypeResolver.resolveDescriptorForType(TypeResolver.kt:1053)
at org.jetbrains.kotlin.resolve.TypeResolver$resolveTypeElement$1.visitUserType(TypeResolver.kt:259)
at org.jetbrains.kotlin.psi.KtVisitorVoid.visitUserType(KtVisitorVoid.java:937)
at org.jetbrains.kotlin.psi.KtVisitorVoid.visitUserType(KtVisitorVoid.java:21)
at org.jetbrains.kotlin.psi.KtUserType.accept(KtUserType.java:42)
I meet Recursion detected on input: IMPORT_DIRECTIVE under LockBasedStorageManager
error when i build my spring-boot application with kotlinx serialization plugin.
What is reason of this error? I cannot event compile my code by this error.
My environments are…
• kotlin version 1.9.22
• jdk version 17
• kotlin jvm plugin version 1.9.21
• kotlinx-serialization plugin version 1.9.21
• kotlinx-serialization-json-jvm version 1.9.22
• spring boot version 3.2.1Sam Stone
02/15/2024, 11:13 PMFoo
in my serializable API model, but I need to serialize it as an empty string if it is null. How can I do this?
@Serializable
data class Foo(val bar: Int)
@Serializable
data class ApiModel(val foo: Foo?)
//ApiModel(null) -> {"foo":""}
//ApiModel(Foo(1)) -> {"foo":{"bar":1}}
elect
02/16/2024, 11:38 AMJon Bailey
02/16/2024, 8:47 PMVaibhav Jaiswal
02/17/2024, 9:48 AMdave08
02/18/2024, 4:27 PMAny
can either contain a String
a Set<String>
or a Map<String, Set<String>>
?Bernhard
02/20/2024, 12:27 PMzt
02/20/2024, 10:27 PM@JsonPath("a.b.c.d")
val x: Int
Does anything like this already exist and if not would this be better as a core feature or a separate libraryDmytro Boiko
02/21/2024, 12:01 PMHuy Tâm
02/28/2024, 6:54 AM@Serializable
class TestProtoBuf {
@ProtoNumber(number = 45)
val id: UInt? = null
@ProtoNumber(number = 48)
val otherValue: OtherValue?
}
@Test
fun testProto() {
val data = "e802058203061001200128018a0500"
val result = ProtoBuf.decodeFromHexString<TestProtoBuf>(data)
val dataAfterSerialize = ProtoBuf.encodeToHexString(result)
assertEquals(expected = data, actual = dataAfterSerialize)
}
When I decode with “e802058203061001200128018a0500" and encode back to a string.
The dataAfterSerialize equals “e80205820306100120012801”, can see “8a0500" is removed.
So how can I keep unknown field data in dataAfterSerialize?arekolek
02/28/2024, 11:42 AMkotlinx.serialization.json.internal.JsonDecodingException
but it is an internal API so I have to use reflection
is there any better way to do this?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"
).