Carter
05/16/2021, 5:35 PMvar
and ?
. For all my Firestore data, I ended up creating Model
for an immutable and non-null representation and a parallel CloudModel
with mutable and nullable representation for automatic Firestore serialization. I added companion functions to convert between the two, and that deals with specific differences (e.g. date representations).
It means some extra code, but it also means that Firebase limitations don’t propagate through my codebase and I get to be explicit about the conversion.
That’s the solution I ended up with, although I’d be curious if other good patterns have emerged.Gilles Barbier
05/16/2021, 8:54 PMDaniele B
05/17/2021, 8:38 AMval obj = Json.decodeFromString<Data>("""{"a":42, "b": "str"}""")
is there a way to decode key/value pairs concatenated by &
?
a=42&b=str
or should I convert my key/value pairs into Json?Daniele B
05/17/2021, 12:05 PM@Serializable
?
interface ScreenParams
@Serializable
data class CountriesListParams(val listType: CountriesListType) : ScreenParams
@Serializable
data class CountryDetailParams(val countryName: String) : ScreenParams
all these 3 definitions are on different files, so it can happen to forget to set @Serializable
If the annotation is forgotten, the app would crash at runtime when visiting that specific screen.ribesg
05/18/2021, 9:58 AMhuehnerlady
05/18/2021, 2:32 PMDariusz Kuc
05/20/2021, 4:00 AMnull
3) value set
I used a sealed class and custom Jackson deserializer in the past (link) but now I'm looking into serialization and need to support kotlinx-serialization
. I tried something using custom serializer and "skipping" the encode part when I process an instance of Undefined
sealed class but that produces me a broken JSON, e.g. given
data class Wrapper(val name: OptionalInput<String>)
I end up with { "name": }
. Any ideas? I don't see any option to skip element in the JsonEncoder
Denys
05/20/2021, 6:05 PMIvan Đorđević
05/21/2021, 8:36 AMnatario1
05/21/2021, 5:01 PMclass Sms(val phone: String, val code: String)
into {"sms":{"phone":"1230129","code":"23984"}}
... I know that I can add SmsWrapper(val sms: Sms)
and require people to use it, but I'd like to use a custom serializer instead. Is there any example to do such thing?
I see many examples where structures are flattened (e.g. you remove a {}
container), but couldn't find one where you do the opposite (nest data in a new {}
).marzelwidmer
05/24/2021, 12:26 PMfeature
is still open 🙂 https://github.com/Kotlin/kotlinx.serialization/issues/318 for https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#data-validationSam
05/25/2021, 10:43 AMzeugederunity
05/25/2021, 12:47 PMjava.lang.IllegalStateException: Class Instant have constructor parameters which are not properties and therefore it is not serializable automatically
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.checkSerializability(SerializerCodegen.kt:28)
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generateSerializableClassPropertyIfNeeded(SerializerCodegen.kt:101)
at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generate(SerializerCodegen.kt:34)
I allready created a minimal Example at: https://github.com/FelixEngl/FailingInstantSerialisation
Does anyone know how to fix this?ushort
05/25/2021, 5:51 PMtseisel
05/27/2021, 6:35 PMEntity
: one with all properties, and another SimpleEntity
with only the most used properties from Entity
. How can I do that? I thought of using closed polymorphism but I can't use a discriminator because I don't control the input JSON.Goth
05/28/2021, 1:41 AM1. this should deserilize to Message<SimmplePayload>
{
id = "1",
type = "simple",
payload = {
name = "Blah"
}
}
2. this should deserilize to Message<AdvancedPayload>
{
id = "2",
type = "advanced",
payload = {
name = "WOW",
description = "WOW WOW"
}
}
LastExceed
05/29/2021, 7:04 PMThe default serializer is based on an ISO 8601 string representation of a type and uses `toString()`/`parse()` to serialize and deserialize valuesthe linked wikipedia article lists
2021-05-29T17:42:42+00:00
as an example string
so why do i get an exception here ?Julián Falcionelli
05/30/2021, 11:08 AM@Serializable
sealed class Activity {
abstract val type: Type
@Serializable
enum class Type(
val enabled: Boolean,
@Transient
val shortName: StringResource
) {
BJJ(
true,
MR.strings.activity_bjj,
),
GRAPPLING(
true,
MR.strings.activity_grappling,
)
}
@Serializable
data class Bjj(
override val type: Type = Type.BJJ,
var belt: Belt? = null,
val stripes: Int = 0
) : Activity() {
@Serializable
enum class Belt(
@Transient
val color: ColorResource.Single
) {
WHITE(MR.colors.belt_white),
BLUE(MR.colors.belt_blue),
PURPLE(MR.colors.belt_purple),
BROWN(MR.colors.belt_brown),
BLACK(MR.colors.belt_black)
}
}
@Serializable
data class Grappling(
override val type: Type = Type.GRAPPLING
) : Activity()
}
I actually send it as a List, something like this:
// Firebase Call
val ref = database
.child(ROOT_PATH)
.child(user.id)
.child("activities")
ref.setValue(ListSerializer(Activity.serializer()), updatedActivities)
Seems that the Firebase Library doesn’t support auto-serialization of sealed classes and I am not sure how to solve this problem.
java.lang.ClassCastException: kotlinx.serialization.descriptors.PolymorphicKind$SEALED cannot be cast to kotlinx.serialization.descriptors.StructureKind
at dev.gitlive.firebase._encodersKt.structureEncoder(_encoders.kt:12)
Spikey Sanju
06/02/2021, 8:30 AMSpikey Sanju
06/03/2021, 10:17 AMsmall chunk of JSON data
. But when I try to deserialize
the large JSON file
. It’s throwing me this weird issue. Why is it happening?
P.S - I validated the JSON
. And there’s no issue with it ✅pniederw
06/06/2021, 3:48 PMMap<String, (Number|Boolean|String)?>
, ideally in a serialization format agnostic way? I guess what I'm asking for is Decoder.peekType()
. I've looked at JsonDecoder.decodeJsonElement()
but the implementation is all internal
.LastExceed
06/08/2021, 8:44 AMclass AA
with a property x: Int
and a class BB
with properties x: Int
and y: Int
where BB
inherits from AA
(alternative: inner class would be ok too in case thats the only option)
i've tried the snippets below, but no success so far. i checked the guide but couldnt find what i need. how do i make this work?Sourabh Rawat
06/09/2021, 11:33 AMencodeToString
LastExceed
06/11/2021, 6:03 AMa unique name of the type that is being serialized
but i dont understand what that means
also why does Line 2 work but Line 3 break ?CLOVIS
06/11/2021, 10:52 AMinit
block) to a serialized data class, will it be executed on deserialization?CLOVIS
06/12/2021, 12:55 PMval data: Data
where Data
is a sealed class gives me
"data": {
"type": "SIMPLE",
"id": "TEXT"
}
on JS, and
"data": [
"SIMPLE",
{
"id": "TEXT"
}
]
on JVM, which unsurprisingly completely fails when Ktor tries to send messages between them.Sourabh Rawat
06/12/2021, 1:34 PMclass Foo(val bar: String)
in. json I have { bar: 'asd', foobar: 'jkl', fb1: 'abc'}
I want to deserialise the above json into class Foo, but also be. able to access foobar & fb1
, somehow. without having to add those fields in class Foo specifically.
What I am looking for is something like extraFields
mapNikky
06/14/2021, 12:23 PM[[Ljava.lang.Object; cannot be cast to [[[D
it works fine on js or when downgrading to serialization 1.0.1
tthe affected types / typealiases are
typealias PointCoordinates = DoubleArray
typealias MultiPointCoordinates = Array<PointCoordinates>
typealias LineStringCoordinates = Array<PointCoordinates>
typealias LinearRingCoordinates = Array<PointCoordinates>
typealias MultiLineStringCoordinates = Array<LineStringCoordinates> // Outer polygon + holes
typealias PolygonCoordinates = Array<LinearRingCoordinates> // Outer polygon + holes
typealias MultiPolygonCoordinates = Array<PolygonCoordinates>
it is for geojson
out of the typeliases PolygonCoordinates
and MultiPolygonCoordinates
seem to be affectedNikolay Kasyanov
06/14/2021, 2:11 PMKSerializer.deserialize
), is there a way to get access to the “raw” value of a primitive? As a string, for example. Since JSON numbers don’t have to be floating point numbers (by the standard https://www.json.org/json-en.html, although some implementation do assume that they are all double, unfortunately), I need a way to read fractional numbers from JSON without compromising their precision (to convert them to some decimal type, for example), none of the existing Decoder
can do that it seems. Am I missing something?
One option would be to write such values as strings server-side, but 1) it’s not always feasible to change existing endpoints that are just fine 2) one doesn’t always control the API.
UPD found an issue, apparently it’s not possible atm: https://github.com/Kotlin/kotlinx.serialization/issues/1405Jason Ankers
06/14/2021, 3:42 PM@Serializable object {
val message = "some message"
}