rocketraman
02/10/2022, 2:37 PM@Serializable
? Or @Contextual
? Ideally it would be a compile-time exception if the contract wasn't followed, but not the end of the world if its a runtime problem.rtsketo
02/10/2022, 8:11 PMAyfri
02/11/2022, 3:32 PMJohn O'Reilly
02/12/2022, 10:44 AMlouiscad
02/16/2022, 9:05 AMAmaan
02/16/2022, 8:35 PMkierans777
02/21/2022, 12:03 PM{
"healthCheck": "success"
}
I need to parse it into an object of HealthCheck
@Serializable
data class HealthCheck(
@SerialName("healthCheck")
@Serializable(with = StatusSerializer::class)
val result: Status
) {
@Serializable
enum class Status {
SUCCESS
}
}
object StatusSerializer : JsonTransformingSerializer<HealthCheck.Status>(HealthCheck.Status.serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement {
val el = super.transformDeserialize(element)
return JsonPrimitive(el.toString().uppercase())
}
}
However I get an exception
HealthCheck.Status does not contain element with name '"SUCCESS"'
rrva
02/22/2022, 5:49 PMAmaan
02/23/2022, 4:05 PMjava.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
. Here is my attached code, but essentially I want a single function that can publish all my events and every time I want to create a new type of event, I can just create a class that extends it with the right payload type.Justin Breitfeller
02/23/2022, 7:43 PMOlli Helenius
02/25/2022, 11:27 AMclass Container<@Serializable T> …
?George
02/25/2022, 3:34 PMDariusz Kuc
03/01/2022, 12:32 AMJson {
coerceInputValues = true
}
mkrussel
03/02/2022, 3:08 PMJson
instance will be slow? Parsing a small JSON text in some unit tests. The first test takes 167 ms (longer on build machine) just in the parsing. All the other tests take about one millisecond to parse the same amount of JSON.
This is on the JVM with Kotlin 1.6.10 and Serialization 1.3.2.Anton Afanasev
03/02/2022, 6:08 PMminifyEnabled = true
and therefore some of my data classes are removed by proguard.
I was able to resolve that by adding a proguard rule to keep this classes, but it did work only when I apply this rule on the app side.
Wonder if it is even possible to have this rules on the library side so that app would not need to modify their proguard rulesSrSouza
03/03/2022, 3:55 PM@Serializable
one? For Java we can easily use Serializable and on Android also Parcelable.Emerson Farrugia
03/03/2022, 5:14 PM@SerialName
as @JsonProperty
for these simple DTOs.Ian
03/07/2022, 3:08 AMKClasses.getMemberProperties()
. I'm using Kotlin 1.6.10 - would appreciate any suggestions:
java.lang.ClassCastException: class kotlin.jvm.internal.ClassReference cannot be cast to class kotlin.reflect.jvm.internal.KClassImpl (kotlin.jvm.internal.ClassReference and kotlin.reflect.jvm.internal.KClassImpl are in unnamed module of loader 'app')
at kotlin.reflect.full.KClasses.getMemberProperties(KClasses.kt:148)
George
03/09/2022, 12:13 PM"data": "{\"imahubKey\":\"eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJpbWFodWIiLCJzdWIiOiJ0ZXN0NTIxMDEiLCJleHAiOjE2NDY4MzM2MzIsImlhdCI6MTY0NjgyMzYzMn0.5L6T1-8EkzR41IX2V43ImI1hfBrIMEJEXXTr88FD8QbST82-l4_kg31tjA0xph_LgvUGPuMtXsRhfx44WgvcAA\"}"
This is my serializer:
public fun SharedPayload.encodeToJson(): JsonElement = SharedPayloadJson.encodeToJsonElement(
SharedSharedPayloadFromBuilder.serializer(), this as SharedSharedPayloadFromBuilder
How can i disable the escaping when im dealing with strings?George
03/11/2022, 6:17 PMpublic sealed interface SharedPayload {
public val data: JsonElement?
public val error: ApiError?
}
and an impl
@PublishedApi
@Serializable
internal class SharedSharedPayloadFromBuilder : SharedPayloadBuilder, SharedPayload {
@Transient
private var hasData = false
@Transient
private var hasErrors = false
@Polymorphic
override var data: JsonElement? = null
private set
override var error: ApiError? = null
private set
override fun data(value: JsonElement?) {
if (hasData) {
error("Data already provided")
}
data = value
hasData = true
}
override fun error(value: ApiError) {
if (hasErrors) {
error("Error already provided")
}
error = value
hasErrors = true
}
fun build(): SharedPayload {
check(hasData || hasErrors) { "Data or error is required" }
return this
}
}
Im serializing any object into data as jsonElement, then i want to serialize the whole object as a jsonElement, yet i got this err
Serializer for JsonPrimitive of kind STRING cannot be serialized polymorphically with class discriminator.
Anyone got any idea what im doing wrong?
The test i run is this:
@Test
fun test() {
val payload = buildSharedPayload {
data(ApiRequest("Hello from api request as json element"))
}
println(payload.data)
val jsonElement = payload.encodeToJson()
println(jsonElement)
val data = SharedPayload("Hello im json element")//.encodeToJson()
println(data.data)
}
fyi here is a sample project with this:
https://github.com/GeorgePap-719/simple-springboot-kotlinx-serialization
Thanks in advance for any help!Ola Gawell
03/15/2022, 3:10 PMKSerializer
to handle the different subclasses. Code in thread.
java.lang.AssertionError: No such value argument slot in IrConstructorCallImpl: 0 (total=0).
Symbol: /IntValueProviderSerializer.<init>|-5645683436151566731[0]
We are using a multiplatform project with org.jetbrains.kotlinx:kotlinx-serialization-json: 1.3.2
LastExceed
03/17/2022, 9:27 AM[]
as if it was just a simple property of the inner type. eg x = listOf("foo", "bar")
is serialized as "x": ["foo", "bar"]
(as expected), but x = listOf("foo")
is serialized as "x": "foo"
instead of "x": ["foo"]
. this breaks the parser, because it sees a x: String
property where x: List<String>
is expected. how do i work around this?andylamax
03/18/2022, 8:24 AM@Serializable
sealed class InfoResults<out T> {
@Serializable
data class Shared<out T>(val data: T) : InfoResults<T>()
@Serializable
data class NotShared(val message: String) : InfoResults<Nothing>()
}
Adam S
03/18/2022, 2:56 PMjmfayard
03/20/2022, 3:17 PM.toPascalCase()
and .to_snake_case()
instead of having all those @SerialName
?Yousef
03/21/2022, 4:33 PMUnexpected JSON token at offset 11023: Expected start of the object '{', but had ':' instead
JSON input: .....","type":"BLOGPOST","category":"e054042e-bc50-4743-a6f5-94d9.....
and I'm not sure whyIcyrockton
03/23/2022, 12:55 PMkevin.cianfarini
03/25/2022, 8:11 PM@JsonClassDiscriminator("country")
sealed interface Market {
@Serializable @SerialName("UK")
object UnitedKingdom : Market
@Serializable @SerialName("US")
enum class UnitedStates : Market {
@SerialName("TX") Texas,
}
}
Here the discriminator doesn’t do anything because the interface isn’t serializable (I think). I don’t want to globally specify the discriminator because it’s only applicable to this hierarchySrSouza
03/31/2022, 4:45 PMConrad Kramer
04/01/2022, 7:19 PMlistSerialDescriptor
assumes a homogenous array.
[{"key": "value"}, "string"]
In Swift, I can pull arbitrary types out of an UnkeyedDecodingContainer
in any order I needConrad Kramer
04/01/2022, 7:19 PMlistSerialDescriptor
assumes a homogenous array.
[{"key": "value"}, "string"]
In Swift, I can pull arbitrary types out of an UnkeyedDecodingContainer
in any order I needankushg
04/01/2022, 8:02 PMConrad Kramer
04/01/2022, 8:49 PMephemient
04/01/2022, 10:04 PMListSerializer(JsonElement.serializer())
(or equivalents) as a surrogate. It won't work for any format other than JSON, but this kind of structure also doesn't work with other formats so you're not really losing anythingConrad Kramer
04/02/2022, 4:07 AM