thana
08/23/2019, 2:34 PMsandwwraith
08/23/2019, 3:45 PMv0.12.0
has been released! This release is binary compatible with Kotlin/Native 1.3.50. Some highlights:
• Added wasm and linuxArm32Hfp Kotlin/Native targets to publication
• Multiplatform serializer resolving from KType
class (which can be obtained from typeOf() function). Doesn't work for user-defined generic classes, but works for standard collections.
• JSON parser is more strict now: leading/trailing commas and other separators are prohibited.
• JSON exceptions hierarchy has been reworked.
Full changelog: https://github.com/Kotlin/kotlinx.serialization/blob/master/CHANGELOG.md#v0120--2019-08-23ankushg
08/27/2019, 3:47 AMIntArray
and LongArray
when working in a multiplatform project?kevin.cianfarini
08/28/2019, 12:50 AMPaulius Ruminas
08/29/2019, 12:37 PMe: java.lang.IllegalStateException: class NewImport has duplicate serial name of property feedId, either in it or its parents.
when I'm annotating a sealed class with `@Serializable`:
@Serializable
sealed class FeedOperation(open val feedId: FeedId) {
@Serializable
data class NewImport(override val feedId: FeedId) : FeedOperation(feedId)
@Serializable
data class RedoImport(override val feedId: FeedId) : FeedOperation(feedId)
@Serializable
data class RemoveImported(override val feedId: FeedId) : FeedOperation(feedId)
}
Are there any workarounds for this?Nikky
08/30/2019, 8:07 PM@Serializable
data class SomeClass(
var aInt: Int = 5
) {
@Transient
lateinit var rootDir: File
}
this fails with: This property is marked as @Transient and therefore must have an initializing expression
the rootDir is in this case the full path to the workingdirectory on the users machine, it should not be serialized, it gets set first thing after deserialization
but it seems that is no longer allowedJuanoterocas
08/31/2019, 9:27 PMansman
08/31/2019, 11:06 PM[x, y]
. There seems to be many ways of doing it. The simplest is to read it as a List<Float>
but I’d like to avoid boxing the floats if possible on the JVM. There is beginStructure
and beginCollection
but no documentation on when to use either or.ansman
09/01/2019, 1:18 AMkotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:79)
at kotlin.jvm.internal.ClassReference.getSimpleName(ClassReference.kt:12)
at kotlinx.serialization.SerialKind.toString(Descriptors.kt:11)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at kotlinx.serialization.json.internal.StreamingJsonInput.beginStructure(StreamingJsonInput.kt:39)
russhwolf
09/03/2019, 4:01 AMNullPointerException
inside the unmap()
call. Is this a limitation of the use of Mapper
on native or am I doing something wrong?
@Test fun mapCoders() {
val foo = Foo("hello", 43110)
val mapper = Mapper.default
val map = mapper.map(Foo.serializer(), foo)
assertEquals("hello", map["bar"])
assertEquals(43110, map["baz"])
val newFoo = mapper.unmap(Foo.serializer(), map)
assertEquals(foo, newFoo)
}
@Serializable
data class Foo(val bar: String, val baz: Int)
Paulius Ruminas
09/03/2019, 8:50 AMe: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:167)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:188)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.analyzePropertiesSerializers(SerializationPluginDeclarationChecker.kt:155)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.check(SerializationPluginDeclarationChecker.kt:44)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.runDeclarationCheckers(ModifiersChecker.java:272)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifierListCommon(ModifiersChecker.java:213)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifiersForDeclaration(ModifiersChecker.java:172)
at org.jetbrains.kotlin.resolve.DeclarationsChecker.process(DeclarationsChecker.kt:91)
at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:245)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:225)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:60)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:111)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:81)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:555)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:82)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:546)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:349)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:105)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:237)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:37)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:79)
.....
Justin
09/08/2019, 9:08 PMkotlinx.serialization
in a multplatform project.
The basic question is this: what is the best way to handle deserialization of Json when one of the field names (i.e. keys) is variable?
I have put together a simple example to illustrate the problem:
@Serializable data class UserData(val name: String)
@Serializable
data class User(
val user_data: UserData,
val user_contributions: List<Contribution>
) {}
interface Action {
val id: String
}
@Polymorphic interface Content
@Polymorphic interface Contribution : Action {
val content: Content
}
@Serializable data class Mentor(
override val id: String,
@SerialName("mentorContent")
override val content: MentorContent
) : Contribution {
@Serializable data class MentorContent(val mentor: String, val mentee: String) : Content
}
@Serializable data class Judge(
override val id: String,
@SerialName("judgeContent")
override val content: JudgeContent
) : Contribution {
@Serializable data class JudgeContent(val judge: String, val judged: String) : Content
}
// Given this JSON:
{
"user_data": {
"name": "Justin"
},
"user_contributions": [
{
"id": "1",
"mentorContent": {
"mentor": "Lisa",
"mentee": "Phil"
}
},
{
"id": "2",
"judgeContent": {
"judge": "Robert",
"judged": "Karen"
}
}
]
}
// This is the desired deserialized object:
User(
user_data = UserData(name = "Justin"),
user_contributions = listOf(
Mentor(
id = "1",
content = MentorContent(mentor = "Lisa", mentee = "Phil")
),
Judge(
id = "2",
content = JudgeContent(judge = "Robert", judged = "Karen")
)
)
)
jush
09/09/2019, 6:49 PMmingw_x86
target)?
I keep getting this error:
Unable to find a matching variant of org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.12.0
Laurenv
09/11/2019, 9:52 AMFudge
09/12/2019, 11:40 AM@Serializable
class in a library:
@Serializable class LibClass(val num :Int)
Is it intended that I can't use it as serializable in a project that consumes the library?
// Error! Serializer has not been found for LibClass
@Serializable class MyClass(val nested : LibClass)
leandro
09/13/2019, 10:40 AMJson.nonstrict
with Retrofit 2.6.1.
The field is a String, and with it on my class I receive the message:
Field 'full_name' is required, but it was missing
but if I remove it then I get:
Encountered an unknown key full_name
ansman
09/14/2019, 10:55 PMnull
in JSON as absent? I have a val foo: List<String> = emptyList()
which fails to be parsed if foo
is null
in the JSONrobgray
09/16/2019, 2:06 PM@kotlinx.serialization.Serializable(TestEnum.TestEnumSerializer::class)
enum class TestEnum(val value: Int) {
RED(0),
GREEN(1),
BLUE(2);
@Serializer(forClass = TestEnum::class)
companion object TestEnumSerializer : KSerializer<TestEnum> {
private val map = values().associateBy(TestEnum::value)
override val descriptor: SerialDescriptor
get() = StringDescriptor
override fun deserialize(decoder: Decoder): TestEnum {
return map.getValue(decoder.decodeInt())
}
override fun serialize(encoder: Encoder, obj: TestEnum) {
encoder.encodeInt(obj.value)
}
}
}
ansman
09/16/2019, 6:23 PMType.serializer()
on Jvm/Android? In what cases would a reflective serializer be used then?ivan.savytskyi
09/17/2019, 6:31 PMInitializers are called iff (if and only if) property is @Transient or optional and was not read (see below).
Is there any way to change this default behaviour if was not read
, make initializer to be called without check if it was seen or not? (Basically have computed properties that only been serialized but never deserialized)Hamed Ghasemzadeh
09/19/2019, 2:20 PMJustin
09/19/2019, 4:47 PMInstances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlinx.serialization.MissingFieldExcpetion: Field 'deviceId' is required, but it was missing
As the message suggests, the KMP library failed to deserialize to:
@Serializable data class Details(val id: String, val deviceId: String)
...because the JSON string didn't contain a deviceId
field, hence the crash on the iOS app using the library.
I have already fixed this specific crash by modifying the model as follows:
@Serializable data class Details(val id: String, val deviceId: String? = null)
But my question is this:
Is there a way to have kotlinx.serialization
propagate any/all exceptions it throws to the implementing platform (iOS/Android/Web/etc.) to decide how to handle it?
In this case, it would have been preferable to have caught this error in iOS, logged it using the iOS logging service, and not-crashed.
Any ideas would be much appreciated, thanks!Nikky
09/22/2019, 7:56 PMBaseClass.serializer()
Justin
09/25/2019, 3:06 PMList<Any?>
.
In practice, the Any?
can be (a) null
, (b) a String
, (c) an Int
, or (d) another `List<Any?>.
What's the best practice for deserializing this with kotlinx.serialization
?
Currently, I'm doing @ContextualSerialization
like this:
@Serializable
data class Items(
val id: Int,
val values: List<@ContextualSerialization Any?> = emptyList()
)
This seems like the right direction, except I'm not sure how to get from this to an array of usable types (e.g. String, Int, List<Any>)?basher
09/25/2019, 5:48 PMKSerializer<T>
, but I'd prefer to add an extension function to T
, for exampleKroppeb
09/26/2019, 4:42 PMbasher
09/26/2019, 10:43 PM@Serializable
internal
? I want to expose a class publicly, but I don't to provide access to its serializerBig Chungus
09/27/2019, 8:57 AMKroppeb
09/27/2019, 12:33 PM@Serializable
@Polymorphic
sealed class Command {
companion object { val context = SerializersModule {
polymorphic(CommandTest::class) {
addSubclass(Function.serializer())
addSubclass(Foo.serializer())
}
include(Foo.context)
}}
@Serializable
@SerialName("function")
class Function(val name:String): Command()
@Serializable
@SerialName("foo")
@Polymorphic
sealed class Foo : Command(){
companion object { val context = SerializersModule {
polymorphic(Foo::class) {
addSubclass(FooBar.serializer())
}
}}
@Serializable
@SerialName("foobar")
class FooBar(val x:Int, val y: Int, val z: Int): Foo()
}
}
And I have a reader that reads each time to the next space.
I want FooBar
to be serialised and deserialised by foo foobar
but if i try deserialised it says foo
isn't registered and I get a NPE when serialising.
Is there a way to do this?Kroppeb
09/27/2019, 12:33 PM@Serializable
@Polymorphic
sealed class Command {
companion object { val context = SerializersModule {
polymorphic(CommandTest::class) {
addSubclass(Function.serializer())
addSubclass(Foo.serializer())
}
include(Foo.context)
}}
@Serializable
@SerialName("function")
class Function(val name:String): Command()
@Serializable
@SerialName("foo")
@Polymorphic
sealed class Foo : Command(){
companion object { val context = SerializersModule {
polymorphic(Foo::class) {
addSubclass(FooBar.serializer())
}
}}
@Serializable
@SerialName("foobar")
class FooBar(val x:Int, val y: Int, val z: Int): Foo()
}
}
And I have a reader that reads each time to the next space.
I want FooBar
to be serialised and deserialised by foo foobar
but if i try deserialised it says foo
isn't registered and I get a NPE when serialising.
Is there a way to do this?Dominaezzz
09/27/2019, 12:58 PMKroppeb
09/27/2019, 12:59 PMDominaezzz
09/27/2019, 1:00 PMJson.stringify
?Kroppeb
09/27/2019, 1:01 PMElementValueEncoder
and ElementValueDecoder
Dominaezzz
09/27/2019, 1:03 PMKroppeb
09/27/2019, 1:03 PMfoo foobar
it would need to read 2 strings. But I can't do that without coding it into the reader.Dominaezzz
09/27/2019, 1:03 PMKroppeb
09/27/2019, 1:04 PM