uhe
05/06/2019, 3:16 PM[36, {"color": "orange", "sizes": [23, 42, 7]}]
(so a mixed JSON array)
where the first entry of the array is always an Int
that represents a type of message and the second is an arbitrary map payload.
I, as a library, need to parse the type field to do some things, but I don't care about the payload and would like to deserialize into a type that is provided by my users.
E.g. fun <UserType> parseMessage(json: String): UserType
Can this be done via kotlinx serialization?yshrsmz
05/07/2019, 9:04 AM@Serializable
data class UserProfile(val id:String)
fun test() {
val jsonElement = Json.nonstrict.toJson<UserProfile?>(NullableSerializer(UserProfile.serializer()), null)
println("jsonElement: $jsonElement")
}
Above code throws following error in kotlin 1.3.31 x serialization 1.1.0.
Caused by: kotlinx.serialization.SerializationException: No tag in stack for requested element
at kotlinx.serialization.TaggedEncoder.popTag(Tagged.kt:160)
at kotlinx.serialization.TaggedEncoder.encodeNull(Tagged.kt:92)
at kotlinx.serialization.internal.NullableSerializer.serialize(NullableSerializer.kt:39)
at kotlinx.serialization.json.internal.AbstractJsonTreeOutput.encodeSerializableValue(TreeJsonOutput.kt:174)
at kotlinx.serialization.CoreKt.encode(Core.kt:73)
at kotlinx.serialization.json.internal.TreeJsonOutputKt.writeJson(TreeJsonOutput.kt:29)
at kotlinx.serialization.json.Json.toJson(Json.kt:129)
has anyone experienced this issue?efemoney
05/12/2019, 9:20 PM@Polymorphic
?thana
05/15/2019, 4:07 PMthana
05/15/2019, 7:23 PMSerialDescriptor
generated for a @Serializable
class depends on?tylerwilson
05/16/2019, 1:38 PM@DateSerializer(format="MM-dd-YYYY")
var date1: Date
Is this possible with Kotlin Serialization (multiplatform)? Any examples anybody might be able to throw my way? Thank you!serebit
05/17/2019, 6:55 PMjeremy
05/19/2019, 4:47 PMPaul Woitaschek
05/20/2019, 12:44 PMx
. It has a UUID
and the KSerializer<UUID>
lives in gradle module y
.
My module x
doesn't know anything about my module y
.
Now I would like to inject that UUIDSerializer instance. Is that possible?jeremy
05/20/2019, 11:56 PMNikky
05/24/2019, 12:54 PM@Serializable
seems to break everythingMarc Knaup
05/25/2019, 3:56 PM.serializer()
function by default?altavir
05/25/2019, 5:58 PM@Serializer(forClass = StringProperty::class)
object StringPropertySerializer : KSerializer<StringProperty> {
override val descriptor: SerialDescriptor = StringDescriptor
override fun serialize(encoder: Encoder, obj: StringProperty) {
encoder.encodeString(obj.value)
}
override fun deserialize(decoder: Decoder): StringProperty {
return SimpleStringProperty(decoder.decodeString())
}
}
I have two questions:
1. The property is nullable, how can I create a descriptor for nullable value. In @pdvrieze's code there is a delegate wrapper that copies descriptor functionality and switches the nullable
flag. Is i the best way to do so. Do I even need the flag
2. Serialized xml seems to have class names for tag names instead of names of the peoperties. Is it something in serializaition, or a feature of xmlutil
?
3. How can I tell enums to be serializaed into their names. Do I need custom serialized for that?Marc Knaup
05/26/2019, 12:25 AMKSerializer
do some look-ahead for the next token type when reading something?
E.g.
• if next token is string then decode as A
• if next token is object then decode as BMarc Knaup
05/26/2019, 12:26 AMkotlinx-serialization-runtime-native
0.11.0
isn't compatible with Kotlin 1.3.40-eap 😕
w: skipping /Users/marc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-serialization-runtime-native_debug_ios_x64/0.11.0/c80bda43e7d80430654f2ef709df51a073ed0f0e/kotlinx-serialization-runtime-native.klib. The abi versions don't match. Expected '[9]', found '8'
w: The compiler versions don't match either. Expected '[1.3]', found '1.2-release-9411'
altavir
05/26/2019, 9:01 AMPaul Woitaschek
05/27/2019, 2:52 PMJson.parse
states:
/**
* Deserializes given json [string] into a corresponding object of type [T] using provided [deserializer].
* @throws [JsonException] subclass in case of serialization error.
*/
However upon missing field it throws a kotlinx.serialization.MissingFieldException
. Is this a known issue?sandwwraith
05/29/2019, 2:35 PM@Polymorphic
annotation, SerializersModule, etc). We have plans to improve it in future.Antanas A.
05/31/2019, 7:08 AM// * We need to register serializers for this RequestForChangeEvent to allow proper serialization for this type
companion object {
init {
registerSerializer(RequestForChangeEvent::class.java.name, RequestForChangeEvent.serializer(PolymorphicSerializer))
registerSerializer(ChangeRejectedEvent::class.java.name, ChangeRejectedEvent.serializer(PolymorphicSerializer))
registerSerializer(ChangeAcceptedEvent::class.java.name, ChangeAcceptedEvent.serializer(PolymorphicSerializer))
}
}
how can I fix this?Antanas A.
05/31/2019, 1:03 PM@Serializable
interface EventData
error:
e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
Cause: Back-end (JVM) Internal error: @Serializable annotation on class EventData would be ignored because it is impossible to serialize it automatically. Provide serializer manually via e.g. companion object
This interface will be used as a marker interface and it will be used on data class X(...) : EventDatathana
05/31/2019, 1:04 PM@SerialId
used for protobuf good for? are there any hints about how it should be used?thana
05/31/2019, 2:08 PMnull
is not supported with protobuf. that is a HUGE drawbackMarc Knaup
06/02/2019, 11:21 PMISE: Class Duration have constructor parameters which are not properties and therefore it is not serializable automatically
When I specify @Serializable(with = DurationSerializer::class)
and a matching @Serializer(forClass = Duration::class)
?
There shouldn't be automatic serialization.
EDIT: oh wow, it happens when I "half-specify" a serializer.
I had deserialize
and serialize
but not descriptor
.
Kinda unexpected that if you create a custom serializer it generates a descriptor automatically based on the constructor which may be a total mismatch with what my serializer actually does 🤔
Is that intentional?m
06/04/2019, 11:15 AM[Exception] class kotlin.String is not registered for polymorphic serialization in the scope of class kotlin.CharSequence class kotlinx.serialization.SerializationException
error?bjonnh
06/04/2019, 11:12 PMToddobryan
06/06/2019, 5:49 PMPaulius Ruminas
06/08/2019, 11:05 AMPaulius Ruminas
06/08/2019, 8:12 PM@Serializable
sealed class Foo<T : Any> {
@Serializable
data class Bar<T : Any>(val o: T) : Foo<T>()
@Serializable
data class Baz(val o: String) : Foo<String>()
}
It's not possible to do something like PolymorphicSerializer(Foo::class, UnitSerializer)
.Marc Knaup
06/09/2019, 12:46 PMlateinit
for required properties?
That will just throw an UninitializedPropertyAccessException
at runtime if the property is missing during deserialization instead of the expected SerializationException
.
Due to the examples I've assumed that such a case can never happen because the descriptor states that these properties are mandatory and it would be checked internally. Doesn't seem to be the case though 😞Nikky
06/09/2019, 9:32 PM@Optional
fixes it , but its marked as deprecated and all crossed outNikky
06/09/2019, 9:32 PM@Optional
fixes it , but its marked as deprecated and all crossed outsandwwraith
06/10/2019, 10:00 AMNikky
06/11/2019, 9:51 AMlouiscad
06/11/2019, 11:14 AMNikky
06/11/2019, 11:51 AMsandwwraith
06/11/2019, 12:20 PM