pajatopmr
12/07/2020, 4:19 PM"fieldName":234
), a structure ("fieldName":{...}
) or an array ("fieldName":["string1","string2","string3"]
), what is the most logical/appropriate KSX feature for handling the serialization of that JSON element. JsonContentPolymorphicSerializer has proven to be very helpful for "regular" TypeScript <-> Kotlin interface mappings, i.e. interface constructs that appear nearly the same in both languages. It is when TypeScript can have one of primitive, object and/or array JSON elements that it gets much harder, at least as I understand KSX so far. My current thinking is that the most likely feature to use is the experimental one ("Under the hood") in the Json Transformations in the fifth chapter of the Kotlin Serialization Guide. If there is an easier approach, I'd love to hear about it.andylamax
12/08/2020, 5:55 AMPerson.serializer().list
can't be resovled. How do I get a list serializer in kotlinx-serilialization-1.0.1
?Azur Haljeta
12/08/2020, 8:09 PMJsonObject
without unboxing the whole object?pajatopmr
12/10/2020, 10:37 PMadk
12/11/2020, 10:38 AM@Serializable(with= ... )
on every subclass?Marc Knaup
12/11/2020, 1:13 PMinterface
? That is, without adding any support for serialization to the implementing class
, and when deserializing have the compiler generate an object
that implements the interface
.
@Serializable
interface Foo {
val bar1: String
val bar2: Int
}
Should serialize too {"bar1":"…","bar2":…}
independent of the implementing class
.
{"bar1":"abc","bar2":42}
should deserialize to object : Foo { override val bar1 = "abc"; override val bar2 = 42 }
.Marc Knaup
12/12/2020, 2:53 AMClassis not registered for polymorphic serialization in the scope ofArrayList
. Mark the base class asAny
or register the serializer explicitly.sealed
ClassDo I really have to do this dance for every possible implementation ofis not registered for polymorphic serialization in the scope ofSingletonList
. Mark the base class asAny
or register the serializer explicitly.sealed
Collection
? 😮
Also, how do I register subclasses of Collection
? There’s no Collection.serializer()
or ArrayList.serializer()
I could use for polymorphism 🤔russhwolf
12/12/2020, 10:46 PM@Serializable
data class Foo(val bar: String = "bar")
and deserialize an empty json object
val foo = Json.decodeFromString(Foo.serializer(), "{}")
then you get Foo("bar")
as output rather than an error or Foo("")
or something else.
Can anyone point to what makes that happen? I want to do something similar for a custom format.
(NB I’m not looking for the encodeDefaults
option. That flag controls whether default values are added to Json output. I’m looking for how default values are inferred when missing from Json input.)tieskedh
12/16/2020, 7:28 PM{
"offset":0,
"limit":15,
"count":1,
//and a field with custom fieldname with the items
"fooItems": [...]
}
Can I transform this somehow in a class like this:
class PaginationResult<T>(
val offset : Int,
val limit : Int,
val count : Int,
val items : List<T>
}
@Serializable FooItem
@serializable BarItem
pajatopmr
12/17/2020, 10:30 PM{"foo":"fooValue","fee":"feeValue"}
and `{"fee":"feeValue","foo":"fooValue"}`should deserialize to the same object. But this is apparently not the case with KXS. Is there some fine print that says JSON implementors can do pretty much what they want or is this a bug of some sort? My assumption is the former so that means transforming incoming JSON to a correct order. Any chance I have this assumption wrong?Chris Fillmore
12/18/2020, 3:02 PM1.0.0-RC
and updated to serialization-json 1.0.1
and it seems there is a change in behaviour for this use case:
@Serializable
class MyClass(
val id: String
) {
val message = "my message"
}
This is my serialization code:
val serializer = Json {
// Not relevant to the example but this is the Json serialization config I'm using
ignoreUnknownKeys = true
prettyPrint = true
}
val myClass = MyClass("abc123")
val serialized = serializer.serialize(myClass)
I expect serialized
to be
{
"id": "abc123",
"message": "my message"
}
but I’m getting
{
"id": "abc123"
}
jaqxues
12/19/2020, 5:51 PMOffz
12/22/2020, 4:15 AMJavier
12/22/2020, 1:06 PMursus
12/23/2020, 4:07 AMRodrigo Silva
12/23/2020, 1:18 PMkotlinx.serialization.SerializationException: Serializer for class 'Immobile' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
Robert Jaros
12/23/2020, 5:40 PM@file:UseContextualSerialization
annotation (both with IR and Legacy)?ursus
12/24/2020, 4:23 PMcamdenorrb
12/25/2020, 2:36 PMeygraber
12/30/2020, 2:50 AMJsonObjectBuilder
(named something other than to
)? It felt more fluid that way than using put
pajatopmr
01/02/2021, 11:42 AMjaqxues
01/02/2021, 8:06 PMdagomni
01/03/2021, 2:58 PMpajatopmr
01/10/2021, 4:33 PMserverInfo
field is missing, a missing field exception is thrown which I did not expect (because of the nullable indication). My conclusion is that this means I need to detect missing fields (using a transforming polymorphic serializer) and explicitly add a null, as in {"serverInfo":null}
. Would that conclusion be correct or am I missing something?Piotr Prus
01/15/2021, 2:26 PMserialization-protobuf
to my android project.
This is what I am doing.
in project level build.gradle:
buildscipt {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}"
}
}
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'
in app-level build.gradle:
plugins {
id 'kotlinx-serialization'
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.4.20"
}
I got following error:
Could not find org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.4.20
I believe I wrongly configured the plugins, but I do not found any guide for android. All of them are for multiplatform 🙂 . Can anyone point me where the issue is?bod
01/17/2021, 4:46 PMkotlin.Number
is not Serializable. What's the easiest way to deal with this?Michael
01/17/2021, 5:48 PM@Serializable
but all of them are and work fine on Kotlin/JVMDariusz Kuc
01/17/2021, 6:55 PMString
value I'd like to have it wrapped in data class Wrapper(val value: String)
, in Jackson I can use @JsonCreator/@JsonValue
annotations to achieve this. Based on the docs it looks like I need to build a custom serializer?william
01/18/2021, 7:11 PMMap<String, String>
into an object that is marked @Serializable
?bsimmons
01/19/2021, 3:50 PM@Serializable
class on disk that persists between user sessions. Does anyone here have experience with making sure that new versions of this class are compatible with the old serialized strings? In the case of field name changes, must I keep both versions of the class and manually do the transformations myself whenever a new version of the app is pushed? Any thoughts or ideas are welcome.bsimmons
01/19/2021, 3:50 PM@Serializable
class on disk that persists between user sessions. Does anyone here have experience with making sure that new versions of this class are compatible with the old serialized strings? In the case of field name changes, must I keep both versions of the class and manually do the transformations myself whenever a new version of the app is pushed? Any thoughts or ideas are welcome.Nikky
01/20/2021, 12:49 AMJson
to ignore unknown fields, for things that are not used at all anymore (and that do nto need to be transformed into other fields)version
field, parse your file as a JsonObject
and then run migration functions on it until it is on the latest version, then you can parse and roundtrip it back to diskbsimmons
01/20/2021, 12:43 PM