Robert Jaros
07/27/2020, 3:16 PMERROR
between subsequent releases misses the point of the deprecation at all 😉Robert Jaros
07/27/2020, 3:46 PMcustomSerializers: Map<KClass<*>, KSerializer<*>>
. In 0.20.0 I've used this function, which is now deprecated:
@Deprecated(
level = DeprecationLevel.ERROR,
message = "This method was removed during serialization 1.0 API stabilization, " +
"please use SerializersModule builder instead" // No replacement deliberately
)
public fun serializersModuleOf(map: Map<KClass<*>, KSerializer<*>>): SerializersModule = noImpl()
How to use SerializersModule builder to create a SerializersModule from my customSerializers map?Marcin Wisniowski
07/28/2020, 11:29 PMList<MyType>.serializer()
obviously doesn't work.Emil Orvik Kollstrøm
07/29/2020, 5:29 AM{
"thread": {
"name": "Kotlin serialization question",
"messages": {
"xSDkj3402": {
"text": "How would you guys..."
"author" "kollstrom"
},
"-3krdjdfs03": {
"text": "I see! The solution to all of your problems is..."
"author": "helpful_person"
}
}
}
}
Since the objects in “messages” in fact work like a map, how do you deserialize this into:
data class Thread(
val name: String,
val messages: Map<String, Message>
)
data class Message(
val text: String,
val author: String
)
Now I know how to deserialize top-level objects into a map, but whenever I try to do the same thing on lower-level objects I always get strange behavior it seems. I’ve tinkered the most with Jackson, but open to #klaxon, kotlinx.serialization, Gson or whatever library that could make something like this work.
Am I thinking about this weirdly? Is it easier if I don’t use maps but a class called Messages? I’ve tried that too, but still getting weird errors or not getting it to work correctly while going down that path, so very welcome to ideas.Mouaad
07/29/2020, 2:28 PMChristian Sousa
07/29/2020, 3:53 PMprivate var parser = kotlinx.serialization.json.Json(JsonConfiguration.Stable.copy(ignoreUnknownKeys = true))
var stringToParse = "{\"gvlSpecificationVersion\":2}"
var parsedReturn: VendorList = parser.parse(VendorList.serializer(), stringToParse)
My structure is the following:
@Serializable
abstract class VendorList(
open var lastUpdated: String?,
open var gvlSpecificationVersion: Int?,
open var vendorListVersion: Int?,
open var tcfPolicyVersion: Int?,
open var vendors: Map<String, Vendor>?,
@Transient override var purposes: Map<String, Purpose>? = null,
@Transient override var features: Map<String, Feature>? = null,
@Transient override var specialFeatures: Map<String, Feature>? = null,
@Transient override var specialPurposes: Map<String, Purpose>? = null,
@Transient override var stacks: Map<String, Stack>? = null
): Declarations(
purposes = purposes,
features = features,
specialFeatures = specialFeatures,
specialPurposes = specialPurposes,
stacks = stacks
)
@Serializable
open class Declarations(
open var purposes: Map<String, Purpose>?,
open var specialPurposes: Map<String, Purpose>?,
open var features: Map<String, Feature>?,
open var specialFeatures: Map<String, Feature>?,
open var stacks: Map<String, Stack>?
)
@Serializable
data class Overflow (
var httpGetLimit: Int
)
@Serializable
data class Vendor(
var purposes: List<Int>,
var legIntPurposes: List<Int>,
var flexiblePurposes: List<Int>,
var specialPurposes: List<Int>,
var features: List<Int>,
var specialFeatures: List<Int>,
var policyUrl: String,
var deletedDate: @ContextualSerialization Any?,
var overflow: Overflow?,
override var id: Int,
override var name: String
): GVLMapItem
interface GVLMapItem {
var id: Int
var name: String
}
@Serializable
data class Purpose(
var description: String,
var descriptionLegal: String,
override var id: Int,
override var name: String
): GVLMapItem
@Serializable
data class Feature(
var description: String,
var descriptionLegal: String,
override var id: Int,
override var name: String
): GVLMapItem
@Serializable
data class Stack(
var purposes: List<Int>,
var specialFeatures: List<Int>,
var description: String,
override var id: Int,
override var name: String
): GVLMapItem
I’m getting the following:
kotlin.Throwable: Key type is missing in the map.
Any help would mean a lot!
Thanks!efemoney
07/30/2020, 11:14 PMinterface Field<Value> {...}
interface InputField<InputT> : Field<InputT> {...}
interface SelectorField<ValueT> : Field<ValueT> {...}
abstract class NumberField : InputField<Number> {...}
abstract class BooleanField : SelectorField<Boolean> {...}
@Serializable
data class NumberFieldImpl(...): NumberField()
@Serializable
data class BooleanFieldImpl(...): BooleanField()
Also I have a custom _map_:
// Need to add serializable on base class else compiler complains
@Serializable
abstract class MapBackedContainer<K, V>(val storage: MutableMap<K, V>) : MutableMap<K, V> by storage
@Serializable // Currently fails compile, message below
class FieldContainer : MapBackedContainer<String, Field<*>>
@Serializable // This one has no errors, ActionImpl is non generic and annotated with @Serializable
class ActionContainer : MapBackedContainer<String, ActionImpl>
FieldContainer
currently fails compile with the message “Serializer for element of type Any? has not been found. To use context serializer as fallback, explicitly annotate element with @Contextual”.
I tried adding @Serializable
on the intermediate abstract classes of Field<*>
but the issue is the same. Same with adding @Contextual
on the Field<*> generic parameter of FieldContainer.janvladimirmostert
08/02/2020, 10:10 PMListSerializer(ListingStateVO.serializer()).serialize(
encoder = ???,
value = it
)
andylamax
08/02/2020, 10:51 PMilyagulya
08/03/2020, 11:46 AM{
"data": {
"type1": [
{
"id": 1,
"payload": {
"fieldA": "123",
"fieldB": "321"
}
}
],
"type2": [
{
"id": 1,
"payload": {
"fieldC": "789",
"fieldD": "101112"
}
}
]
}
}
And I want to get this as result:
Map<String, List<Item>>
where key is type1, type2
and value list item is:
data class Item(
val id: Int,
val payload: ItemPayload
)
sealed class ItemPayload {
data class PayloadOne(
val fieldA: String,
val fieldB: String
)
data class PayloadTwo(
val fieldC: String
val fieldD: String
)
}
Right now I'm thinking about to deserialize the JSON to intermediate data class (which has JsonObject
instead of ItemPayload
), then determine which serializer do I need to use on each particular item and then deserialize the JsonObject
to exact ItemPayload
based on map key (type1, type2
)
But I don't understand how can I deserialize JsonObject
inside the KSerializer
.Narek Mailian
08/03/2020, 7:46 PMsavrov
08/04/2020, 2:33 PMApiResponse<T>
with kotlinx.serialization
. So i’ve created a ApiResponseSerializer
class, with help of docs. But now i don’t understand how to use that serializer, since none of provided methods doesn’t seem to suit to me. That is the place where i need a help.
My ApiResponse<T> is defined like:
data class ApiResponse<T>(
val data: T? = null,
val error: ApiError? = null
)
And the regular usage (in KTOR server) is
respond(
HttpStatusCode.Created,
ApiResponse(data = response.data)
)
I don’t know where i can “attach” my serializer to data
field of my ApiResponse
class.
Thank you in advance.Brett Best
08/05/2020, 6:09 AMe: java.lang.NoClassDefFoundError: org/jetbrains/kotlin/com/intellij/openapi/util/io/JarUtil
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.VersionReader.getVersionsFromManifest(VersionReader.kt:34)
Has someone here seen this before?efemoney
08/06/2020, 11:33 AMdata class Reference(val id: String)
// appears in the json as just String
...
"reference" : "the-reference-id"
...
altavir
08/06/2020, 4:07 PMDaniele B
08/07/2020, 4:18 PMKarlo Lozovina
08/13/2020, 2:02 PMfrank
08/14/2020, 3:34 AMbuildJson
but is immutable and I can't add new JsonElements Any mutable alternative in Kotlin-serialization lib?
My Scratch code:
val files = buildJsonArray {
ficheros.forEach {
add(buildJsonObject {
put("name", JsonPrimitive(it))
})
}
}
Karlo Lozovina
08/14/2020, 1:43 PM@Serializable(with = ...)
annotation on every field? Can I somehow specify my custom serializer for a class in a global way?frank
08/14/2020, 3:13 PMjson{}
to buildJsonObject{}
@sandwwraith Why was the change made for a more verbose option? Is there any plan to make it less verbose?
Sample:
jsonArray { //version 0.20.0
+"fic.sld"
+json {
"age" to 123
}
}
buildJsonArray { //version 1.0
add("fic.sld")
add(buildJsonObject {
put("age", JsonPrimitive(123))
})
}
Karlo Lozovina
08/14/2020, 5:55 PMsomeField: SomeType? = null
, is there a way without specifying null
as default? For a nullable field, when it's missing I always want it set to null
, so it seems a bit redundant...Jeremy
08/14/2020, 9:16 PMursus
08/17/2020, 3:42 AMTwoClocks
08/18/2020, 4:41 AMgildor
08/18/2020, 7:57 AMkotlinx-serialization-core
is not format agnostic? All other formats were moved to own dependencies except Json and it’s never was clear for me why, so now it’s impossible to replace bundled Jsom implementation with own version, they will always available side by side, or if json is not used at all. I understand it’s not a big deal in terms of bytecode, it more about API surface, even if it 95% cases used only as json, why not just create dependency for it: kotlinx-serialization-json
It’s rc already, probably too late, but maybe keep kotlinx-serialization-core
and reintroduce something like kotlinx-serialization
or again runtime
which would include only annotations and basic utilities, no formatsKarlo Lozovina
08/18/2020, 3:02 PMimport kotlinx.serialization.json.JsonDecodingException
been moved or renamed in 1.0.0-RC?Karlo Lozovina
08/18/2020, 3:17 PMBack-end (JVM) Internal error: wrong bytecode generated
errors... any idea where to start with that one? (Java/Kotlin newb here)Antoine Gagnon
08/18/2020, 3:38 PMYour current kotlinx.serialization core version is too low, while current Kotlin compiler plugin 1.4.0 requires at least 1.0-M1-SNAPSHOT. Please update your kotlinx.serialization runtime dependency.
Any idea what’s going on?Ian
08/18/2020, 4:31 PMByteArraySegment
- this is what I've tried (I suspect the issue may be the descriptor
I'm using, but I'm not sure how I should be doing it):
@Serializer(forClass = ByteArraySegment::class)
companion object : KSerializer<ByteArraySegment> {
override val descriptor: SerialDescriptor
get() = ByteArraySerializer().descriptor
override fun serialize(encoder: Encoder, value: ByteArraySegment) {
encoder.encode(ByteArraySerializer(), value.asArray)
}
override fun deserialize(decoder: Decoder): ByteArraySegment {
return ByteArraySegment(decoder.decode(ByteArraySerializer()))
}
}
I've explained the problem in more detail on Reddit but nobody has been able to suggest a solution yet, would greatly appreciate any suggestions.jw
08/18/2020, 6:58 PMIt is safe to operate with instances ofJust to be clear, this means so long as we're simply accepting aand call its methods.StringFormat
StringFormat
in an API it's safe to OptIn
to this experimental annotation to disable the warning and this is guaranteed to be compatible? It's only experimental to subtype StringFormat
(and BinaryFormat
)?jw
08/18/2020, 6:58 PMIt is safe to operate with instances ofJust to be clear, this means so long as we're simply accepting aand call its methods.StringFormat
StringFormat
in an API it's safe to OptIn
to this experimental annotation to disable the warning and this is guaranteed to be compatible? It's only experimental to subtype StringFormat
(and BinaryFormat
)?Vsevolod Tolstopyatov [JB]
08/19/2020, 11:13 AM