xenomachina
08/29/2022, 7:38 PMDecoder
implementations will no longer be @ExperimentalSerializationApi
?Eugen Martynov
08/30/2022, 10:56 AMSerialName
to all implementations to serialise the sealed interface. However, when I try to test it, I see that serialiset is not found for the discriminator. Does it mean I have to define all subclasses when defining the serializable module?nikolaymetchev
08/31/2022, 12:51 PMcom.google.protobuf.GeneratedMessageV3
automatically. Has somebody written a Contextual serializer that does that?RayeW47
08/31/2022, 9:05 PM@Serializable class A
and I have a helper class for companions to automatically grab a file to deserialize something in to an A
and put it in A(.Companion).INSTANCE
.
However, using .INSTANCE
gets a bit annoying to use and looks kind of gross, so I am trying to convert A
in to a @Serializable sealed interface A
, so that A.Companion
can be an A
and I can simply reference A
rather than A(.Companion).INSTANCE
.
I can't seem to understand enough to get this working with a sealed interface
, I keep running in to polymorphism errors or NPEs if I try to delegate similar to how I do in Loader
, so I am here for help.
Here's a minimum example.
abstract class Loader<T>(val type: StringFormat, val filePath: String, val serializer: () -> KSerializer<T>, ) {
val INSTANCE by lazy {
type.decodeFromString(serializer(), File(filePath).readText())
}
}
current
@Serializable
class A(val someProperty: Int) {
companion object: Loader<A>(
type = Json.Default, filePath = "./config/A-config.json", serializer = { A.serializer() }
)
}
fun main() {
A.INSTANCE.someProperty
}
goal
@Serializable
sealed interface A {
val someProperty: Int
companion object: Loader<A>(
type = Json.Default, filePath = "./config/A-config.json", serializer = { A.serializer() }
)
}
fun main() {
A.someProperty
}
xxfast
09/06/2022, 12:37 PMspand
09/06/2022, 2:16 PMType mismatch: inferred type is ProjectSerializer but S
erializationStrategy<TypeVariable(T)> was expected
. What could I be doing wrong ?
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.descriptors.*
// NOT @Serializable
class Project(val name: String, val language: String)
@Serializer(forClass = Project::class)
object ProjectSerializer
fun main() {
val data = Project("kotlinx.serialization", "Kotlin")
println(Json.encodeToString(ProjectSerializer, data))
}
xxfast
09/06/2022, 10:50 PMkotlinx-serialization-json-okio
like this in commonMain
class ValueStore<T: Any>(
val path: Path,
val serializer: Json = Json,
..
) {
suspend inline fun <reified V: T> set(value: V?) = lock.withLock {
stateFlow.emit(value)
serializer.encodeToBufferedSink(value, FILE_SYSTEM.sink(path).buffer())
}
}
And have a commonTest
written up like
private val store: ValueStore<Pet> = ValueStore(path = "test.json".toPath())
@Test
fun testWriteValue() = runTest {
val mylo = Pet(name = "Mylo", age = 1, type = Cat)
store.set(mylo)
val actual: Pet? = store.get<Pet>()
val expect: Pet = mylo
assertSame(expect, actual)
}
This test is failing 🤔 Although, I can see the test.json
file being created, but it is empty. Am I using this correctly? Full source hereAyfri
09/08/2022, 3:54 PMephemient
09/11/2022, 12:06 AMJan Skrasek
09/11/2022, 12:45 PM// in library
@Serializable
interface Base
// in user code
sealed interface All : Base {
@Serializable
object A : All
@Serializable
data class B(...) : All
}
Ideally, I'd like to register as a subclass only the All type and not every children (A, B, ...). Do I miss something or it is not possible?Ayfri
09/17/2022, 2:31 AMIllegal Input
when I get a response that is an empty array, so just []
, that should totally not create that error right ?xxfast
09/18/2022, 4:40 AMkotlinx.serialization.json.internal.JsonEncodingException: Value of type 'kotlinx.serialization.Polymorphic<IdType>' can't be used in JSON as a key in the map. It should have either primitive or enum kind, but its kind is 'OPEN'.
where
interface IdType : Parcelable {
val name: String
}
enum class PersonIdType : IdType {
GIVEN_NAME, MIDDLE_NAMES, FAMILY_NAME
}
enum class VehicleIdType : IdType {
PLATE_NUMBER, VIN, ENGINE_NUMBER,
}
is this a known limitation? 🤔Ben Woodworth
09/22/2022, 4:04 AMcypher121
09/25/2022, 8:50 AMcypher121
09/25/2022, 8:58 AMlouiscad
09/27/2022, 6:23 AMNoSuchMethodError
occurs when I try to use the copy
function on a @Serializable
data class
that has one enum, one ULong
, and one Long
.
java.lang.NoSuchMethodError: No virtual method copy-2TYgG_w(blablabla)_*blablabla*_
Ovsyannikov Alexey
09/27/2022, 9:59 AMmartmists
09/28/2022, 11:25 AMJorge Bo
09/28/2022, 1:49 PMflights_information": {
"OD_CUNMIA_01_20221111": {
"CPA_1": {
"FL00GH8001": {
"arrival_information": {
"airport_reference_id": "MIA_0",
"change_day_indicator": "0",
"date": "2022-11-11",
"date_of_week_name": "Friday",
"time": "17:30:00"
},
Javier
09/29/2022, 12:36 PMHrafn Thorvaldsson
09/30/2022, 5:13 PMString
and JsonObject
for target type classes not marked with the @Serializable annotation, and if this behavior difference is expected.
Please observe the following data class:
@Serializable
data class Test(val value: Int)
and the following manual construction of a JsonObject:
val jsonObject = buildJsonObject {
put("version", 1)
}
As expected when executing either Json.decodeFromJsonElement<Test>(jsonObject)
or Json.decodeFromString<Test>(jsonObject.toString())
the output is an instance of Test(value=1)
.
If the @Serializable
annotation is removed from the class Test
and a manual KSerializer
for the class is created to handle the mapping:
class TestSerializer: KSerializer<Test> {
override val descriptor: SerialDescriptor
get() = buildClassSerialDescriptor("test") {
element<Int>("version")
}
override fun deserialize(decoder: Decoder): Test {
return decoder.decodeStructure(descriptor) {
val version = decodeIntElement(
descriptor,
descriptor.getElementIndex("version")
)
Test(version)
}
}
// .... omitting serialize() method
}
then executing Json.decodeFromJsonElement(TestSerializer(), jsonObject)
will work like before and the output is an instance of Test(value=1)
just as before.
However executing Json.decodeFromString(TestingSerializer(), jsonObject.toString())
will result in an exception:
Unexpected JSON token at offset 1: Unexpected symbol 'v' in numeric literal at path: $
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 1: Unexpected symbol 'v' in numeric literal at path: $
JSON input: {"version":1}
Is this the expected behavior, and if so what is causing the string traversal to fail while the JsonObject
is not affected?Nathan
10/03/2022, 8:03 PMinterface IFoo
@Serializable
class FooA: IFoo
@Serializable
class FooB: IFoo
val myJson = Json {
ignoreUnknownKeys = true
isLenient = true
explicitNulls = false
encodeDefaults = false
serializersModule = SerializersModule {
polymorphic(IFoo::class, FooA::class, FooA.serializer())
polymorphic(IFoo::class, FooB::class, FooB.serializer())
}
}
val listOfFoo = myJson.decodeFromString<<List<IFoo>>(...)
The above ^ works fine in the Android code, but errors out when called from iOS:
`kotlinx.serialization.SerializationException: Serializer for class ‘IFoo’ is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/Native explicitly declared serializer should be used for interfaces and enums without @Serializable annotation`Is there a step that I am missing with setting up the SerializersModule to make it compatible with iOS? Again, this works perfectly fine from the Android Versions: • Kotlin: 1.7.10 • Kotlin serialization: 1.4.0 ◦ org.jetbrains.kotlinx:kotlinx-serialization-core ◦ org.jetbrains.kotlinx:kotlinx-serialization-json EDIT: https://kotlinlang.slack.com/archives/C7A1U5PTM/p1601187298013400 I found this older post with someone asking the same question and they seemed to solve it by downgrading ?
Stylianos Gakis
10/04/2022, 11:56 AMMap<String, Any?>
and I want to turn it into JSON to then pass somewhere else.
With the type-safe builders I only see that I can do stuff like put(key:Any, value: JsonElement)
but I don’t know how to turn my Any? -> JsonElement
.
I can do stuff like
fun Any.toJsonElement() : JsonElement {
when(this) {
is Map<*, *> -> ...
is Collection<*> -> ...
}
}
and so on but I’ll most definitely do it wrong so I was looking if there’s some API I’m missing here.zt
10/04/2022, 9:08 PMzt
10/04/2022, 9:46 PM1
represents it being present, and if its null then its not present. That way it can just be a list of the present `Feature`s
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class SearchFilter(
val sortBy: SortBy? = null,
val filters: List<Filter>
) {
@Serializable
data class Filter(
val uploadDate: UploadDate? = null, // ignore these
val type: Type? = null, // ignore these
val duration: Duration? = null, // ignore these
val hd: Int? = null,
@ProtoNumber(14)
val uhd: Int? = null,
val subtitles: Int? = null,
val creativeCommons: Int? = null,
@ProtoNumber(15)
val spherical: Int? = null,
@ProtoNumber(26)
val vr: Int? = null
)
}
@Serializable
enum class Feature {
LIVE,
UHD,
HD,
SUBTITLES,
CREATIVE_COMMONS,
/** 360° video */
SPHERICAL,
VR
}
Ryunos
10/05/2022, 3:54 PMJan Skrasek
10/07/2022, 7:28 AMRyunos
10/09/2022, 4:58 PM> Task :kotlinx-serialization-core:compileKotlinJvm FAILED
e: Module java.base cannot be found in the module graph
I thought it was a problem with my configuration but I tried using version 1.8, 11 and 17 for java and nothing worked. Are there contributors out here who could help me?rattleshirt
10/11/2022, 10:58 AMtext(10)
"nameSpaces"
map(1)
text(3)
"key"
array(10)
encoded cbor data item, tag(24)
bytes(82)
...
Dylan
10/11/2022, 3:28 PMdata
object. The type of data
is defined by isTypeA
or isTypeB
within the object itself. (See sample in thread).
I was thinking about using a sealed class. Is there a way to specify using annotations that the type of the object should be defined by a value within that object?