edenman
08/19/2020, 3:08 AMKClass.serializer()
it’s marked as @InternalSerializationApi
in 1.0.0-RC, I filed https://github.com/Kotlin/kotlinx.serialization/issues/991 to try and get some more clarity on why it’s not recommendedrrva
08/19/2020, 12:38 PMrrva
08/19/2020, 1:27 PMserializer = KotlinxSerializer(
Json {
isLenient = true
ignoreUnknownKeys = true
useArrayPolymorphism = true
}
)
tylerwilson
08/19/2020, 3:50 PMreturn when (val jsonElement = JsonElementSerializer.deserialize(decoder)) {
is JsonArray -> jsonElement.content.map { parser.decodeFromString(dataSerializer, it.toString()) }
else -> listOf(parser.decodeFromString(dataSerializer, jsonElement.toString()))
}
but in 1.0.0-RC the JsonElementSerializer is not recognized. What is the replacement for this?tylerwilson
08/19/2020, 4:54 PMLuoqiaoyou
08/20/2020, 4:16 AMNikolay Kasyanov
08/20/2020, 2:11 PMtype
to deserialize subclasses?Vampire
08/22/2020, 7:31 PMString
or a List<String>
in the serialized format, can this somehow be catered for with kotlinx.serialization
?
In my concrete case I try using kaml
to parse a GitHub workflow yml file.
It can for example have this:
jobs:
job1:
job2:
needs: job1
job3:
needs: [job1, job2]
Ovsyannikov Alexey
08/23/2020, 6:44 PMtmp0_subject
was set up from this._file_3
on line 98291 on screenshot. This value is absent, but there is value value._file_3
which is look like something what must be on place of tmp0_subject
Vampire
08/25/2020, 12:01 PMObjectMapper().writerWithDefaultPrettyPrinter().writeValue(it, this)
and ObjectMapper().readValue(buildSrcResultFile, Result::class.java)!!
.
Exists something similar that works with kotlinx.serialization?Manuel Dossinger
08/25/2020, 12:13 PMinterface WithValue { val x: Int }
@Serializable
data class WithValueImpl(override val x: Int) : WithValue
@Serializable
data class MyClass(val y: Int) : WithValue by WithValueImpl(1)
fun main() {
println(Json.encodeToString(MyClass(5)))
// prints: {"y":5}
}
In the documentation, I have only found that delegated properties are transient, but nothing regarding delegated interfaces. The desired result would be, that MyClass is encoded as {"y": 5, "x": 1}
. Do you have any pointers for me?Vampire
08/25/2020, 1:14 PM@Serializable
. Foo
in reality is a Groovy object from a library so it is not modifiable.
data class Foo(val count: Int = 1)
@Serializer(forClass = Foo::class)
object FooSerializer : KSerializer<Foo> {
override val descriptor = SerialDescriptor("Foo") {
element("count", Int.serializer().descriptor)
}
override fun deserialize(decoder: Decoder) = decoder.decodeStructure(descriptor) {
Foo(decodeIntElement(descriptor, 0))
}
override fun serialize(encoder: Encoder, value: Foo) {
encoder.encodeStructure(descriptor) {
encodeIntElement(descriptor, 0, value.count)
}
}
}
println(Json(Stable).parse(FooSerializer, Json(Stable).stringify(FooSerializer, Foo())))
Result:
Unexpected JSON token at offset 14: Failed to parse 'int'.
JSON input: {
"count": 1
}
Because it tries to parse count
as Int
instead of 1
:-/CLOVIS
08/25/2020, 1:34 PMplugins {
kotlin("multiplatform") version "1.4.0" // previously 1.3.61
kotlin("plugin.serialization") version "1.4.0" // same
}
and:
val serialization_version = "1.0.0-RC"
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version")
// ktor...
}
}
// other targets, with serialization-runtime-jvm, etc
Etc.
However, I'm getting this Gradle error:
Execution failed for task ':compileKotlinJvm'.
> Could not resolve all files for configuration ':jvmCompileClasspath'.
> Could not find org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:1.0.0-RC.
Required by: ...
> Could not find org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0.0-RC.
Required by: ...
Unless I read something incorrectly, I think that's what the GitHub README & the documentation on kotlinlang.org say to do... Did I do something wrong?JCollardBovy
08/26/2020, 7:34 PMOverload resolution ambiguity:
public fun String.Companion.serializer(): KSerializer<String> defined in kotlinx.serialization.builtins
public fun String.Companion.serializer(): KSerializer<String> defined in kotlinx.serialization.builtins
Overload resolution ambiguity:
public fun <K, V> MapSerializer(keySerializer: KSerializer<TypeVariable(K)>, valueSerializer: KSerializer<TypeVariable(V)>): KSerializer<Map<TypeVariable(K), TypeVariable(V)>> defined in kotlinx.serialization.builtins
public fun <K, V> MapSerializer(keySerializer: KSerializer<TypeVariable(K)>, valueSerializer: KSerializer<TypeVariable(V)>): KSerializer<Map<TypeVariable(K), TypeVariable(V)>> defined in kotlinx.serialization.builtins
I've checked the dependencies downloaded by Gradle and indeed see that there are 2 identical artefacts for JVM, but that's not the case for common, JS or native.
I only have a single dependency declared on the common module for org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC
Has anyone experienced something similar and could shed some light on how to solve it?pajatopmr
08/27/2020, 7:19 AMpajatopmr
08/27/2020, 7:43 AMhiperbou
08/27/2020, 12:34 PM@Serializable
data class OtherData(val id:String)
@Serializable
data class MyData(val id: String = "", val otherData: OtherData = OtherData(id))
rrva
08/28/2020, 9:24 AMursus
08/28/2020, 8:22 PMChris Fillmore
09/01/2020, 12:18 AM@Serializable
data class MyClass(val myProp: String, private val myPrivateProp: String)
does myPrivateProp
get serialized?Nicolas Bourdin
09/01/2020, 2:39 PMEvan
09/01/2020, 6:56 PMCompilation failed: Deserializer for declaration public kotlinx.coroutines/SingleThreadDispatcher|null[0] is not found
Does this look familiar to anyone?
EDIT: I reverted back to 0.20.0 and have the same issuetim
09/01/2020, 8:02 PMTmpod
09/01/2020, 8:57 PMdecodeEnum
is given a string that it should try to match to a enum value index, but SerialDescriptor#getElementIndex
doesn't seem to have any way to do a case insensitive lookup. Any ideas how I might achieve this?Ovsyannikov Alexey
09/03/2020, 12:15 PMListSerializer
with data class subserializer, which have one custom serializer in it. As a result on trying to decode encoded bytearray I got error like Expected array, but found 00
. I have tried to find some known issue on github or in the google, but didn't find it. Maybe, somebody here faced this too and could help me?louiscad
09/04/2020, 10:40 AMJess Brent
09/07/2020, 12:59 AM{
"op": "op type",
"data": {
"id": "",
"name": ""
}
}
for some context, the server utilizes both http and websockets.
the op is something like GetSite
or GetUser
and the data block is an array of the op's values.
right now i'm running into an issue where it can't figure out what to use for the data block when receiving a response on the websocket.
i'm starting to feel like it won't be possible to utilize one set of classes for both http/ws.
i got the following to work but something about it just feels hacky 😓
@Serializable
class SampleRequest {
private val op = "GetThing"
var data: Data
constructor(auth: String?): super() {
data = Data(auth)
}
@Serializable
class Data(
val auth: String?
)
}
@Serializable
class SampleResponse {
private val op = "GetThing"
var data: Data
constructor(id: Int, name: String): super() {
data = Data(id, name)
}
@Serializable
class Data(
val id: Int,
val name: String
)
}
fkrauthan
09/08/2020, 1:05 AMMyResponse<SpecificResponse>
as return type) but I get a no serializer found for MyRequest
even though I can use serialization manual of an instance correctly.natario1
09/10/2020, 11:10 AMclass BaseModel {
val keys: Set<String>
fun <T> get(key: String): T?
fun <T> set(key: String, value: T?)
}
Basically, it works as a Map<String, Any?>
and we can assume that the actual values are something that can be serialized, like numbers or strings. Then I have concrete models e.g. class UserModel : BaseModel
, class PostModel : BaseModel
and so on.
How can I make these classes serializable? I tried to write a common KSerializer and can handle serialization like:
@Serializer(UserModel::class)
object UserSerializer : BaseSerializer<UserModel>({ UserModel() })
@Serializer(PostModel::class)
object PostSerializer : BaseSerializer<PostModel>({ PostModel() })
class BaseSerializer<M : BaseModel>: KSerializer<M> {
override fun serialize(encoder: Encoder, model: T) {
for (key in model.keys) {
encoder.encodeString(key)
val value = model.get(key)
when (value) {
is Int -> encoder.encodeInt(value)
is Long -> encoder.encodeLong(value)
is String -> encoder.encodeString(value)
else -> error()
}
}
}
}
But then I have no idea of how to implement deserialize
(looks like order matters there...?) and descriptor: SerialDescriptor
(not even sure what this is). I might get it working but I don't think I'm doing things in the correct way. Can anyone help?
The BaseModel also has a working Parcelable implementation, if that matters.bitkid
09/10/2020, 11:10 AMJson { allowStructuredMapKeys = true }
) thread safe? especially this call
json.encodeToString(someData)
bitkid
09/10/2020, 11:10 AMJson { allowStructuredMapKeys = true }
) thread safe? especially this call
json.encodeToString(someData)
Vsevolod Tolstopyatov [JB]
09/16/2020, 6:50 PM