bjonnh
01/20/2021, 6:49 PMAlexander
01/20/2021, 7:47 PMimport kotlinx.serialization.*
import kotlinx.serialization.json.Json
// Uncomment to disable encoding default values
// @Serializable
internal class Action(
val id: String,
val routing: String? = null,
)
@Serializer(forClass = Action::class)
object ActionSerializer : KSerializer<Action>
fun main(args: Array<String>) {
val action = Action(
id = "1",
)
println(Json.encodeToString(ActionSerializer, action))
}
This code produces:
{
"id": "1",
"routing": null
}
If you uncomment @Serializable
annotation default values will be ignored.
Is this behavior a bug or not?fkrauthan
01/21/2021, 8:38 PMpolymorphism
it currently uses the type
attribute. Is there any way to change the name? In my case I need it to look at the name
attribute (which is the same as type
just different name)Nikky
01/23/2021, 11:49 PMpajatopmr
01/24/2021, 1:27 PMMegan Teahan
01/29/2021, 4:43 PMAny
in a nested Map and I'm using the @contextual method defined in this post:
@Serializable
data class Media(
val id: String? = null,
val title: String? = null,
val tracking: Map<String, Map<String, @Contextual Any>> = mapOf(),
)
val json = Json {
ignoreUnknownKeys = true
serializersModule = SerializersModule {
contextual(String.serializer()) // could be an Int
contextual(Int.serializer()) // could be a String
contextual(Boolean.serializer()) // could be a Boolean
}
}
val media = json.decodeFromString(Media.serializer(), MediaResponse.json) //MediaResponse.json is a string
I'm still getting this error
kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
Am I missing something?Anarakul
01/29/2021, 5:04 PMenum class
. I have read the appropriate documents (I think) and am using the appropriate annotations and strategies (I think), but I am still encountering a run-time issue immediately. I am also encountering a compile-time issue, I think, because the compiler doesn’t believe that there’s a serializer
method on my enum class
.RedTahr1
02/02/2021, 12:49 PMFailed to resolve: org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0.1
Isn't this available on jcenter? or what should I do?Ben Woodworth
02/02/2021, 10:24 PMbeginCollection
to require descriptor.elementsCount == 1
, but I'm not sure if that guarantees homogeneity in all cases. (I'm pretty sure it'll work for the standard collection serializers, but besides that I'm not sure)Karlo Lozovina
02/02/2021, 11:17 PM"-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi"
in build.gradle.ktsJérôme Gully
02/05/2021, 10:58 AMCommonEnumSerializer
/ EnumSerializer
. I try to use openapi-generator KMP export to generate data classes from json swagger and generated models uses unresolved CommonEnumSerializer
, for example
@Serializable(with = Env.Serializer::class)
enum class Env(val value: kotlin.String){
notDeployed("not_deployed"),
inDeployment("in_deployment"),
deployed("deployed");
object Serializer : CommonEnumSerializer<Env>("Env", values(), values().map { it.value.toString() }.toTypedArray())
}
What is the current way for doing that ? Or how to find/use CommonEnumSerializer
🤓pajatopmr
02/05/2021, 3:13 PMbod
02/06/2021, 2:49 PMSerializer
for this? (in particular, what SerialDescriptor
can I use?) Thanks!Tiago Nunes
02/08/2021, 11:07 AM{
"d": {
"id": 3,
"name": "Tiago"
}
}
Is there any way to make it so that I don't have to make a wrapper class in every Serializable class? Something like @path, with XML Parser:
@Path("Body/GetAllVehicleBrandsResponse")
private List<InstallerVehicleBrand> allVehicleBrandsResult;
(Here I didn't need to make a Body class)Tiago Nunes
02/08/2021, 8:11 PMval jsonObject = jsonElement.jsonObject
How do I add a new entry to the object? (something like this:)
jsonObject.addProperty(name, value)
(name and value are both Strings)pajatopmr
02/09/2021, 2:48 PM@Serializable(with = ????) val list: List<Stuff>
. But what ???? is is eluding me.irus
02/09/2021, 4:58 PMStringFormat
so it has methods like encodeToString/decodeFromString
but is somehow available direct serialization/deserialization to ByteArray?
Comparing to jackson that has writeValueAsBytes
(create string with encodeToString
and than convert to com.google.protobuf.ByteString, my case for PubSub):
Benchmark Mode Cnt Score Error Units
JsonTest.jackson thrpt 25 459596.078 ± 31782.902 ops/s
JsonTest.kotlin thrpt 25 301485.307 ± 19990.942 ops/s
irus
02/10/2021, 11:11 AMJson.encodeToByteArray
– why this not available? Any plans to implement direct encoding to ByteArray
?RedTahr1
02/10/2021, 2:54 PMkotlinx.serialization.SerializationException: Class 'ArrayList' is not registered for polymorphic serialization in the scope of 'Any'.
jaqxues
02/10/2021, 10:29 PMAndy Victors
02/10/2021, 10:54 PMdecoder.decodeStructure(descriptor)
Karlo Lozovina
02/11/2021, 9:02 AMList<T>
how does it pick what concrete type to use? ArrayList, LinkedList, etc...?tylerwilson
02/11/2021, 5:29 PMCould not find org.jetbrains.kotlin:kotlin-stdlib-common:1.4.30-270.
Required by:
project :salonbiz-library > org.jetbrains.kotlinx:kotlinx-serialization-core:1.1.0-RC
project :salonbiz-library > org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0-RC
Do I perhaps have to add an explicit dependency to stdlib, or something else going on?Tiago Nunes
02/12/2021, 12:11 PMAndy Victors
02/12/2021, 4:49 PMcoerceInputValues
is true. (offset 22 is begin of first null)
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 22: Expected end of the array or comma
JSON input: {"rows":[["Connected",null,null,null,null,null,null,.....
Dariusz Kuc
02/12/2021, 9:37 PMKSerializer
that behaves similar to the built-in ones for primitives? e.g. I can deserialize Double
from Number (Int/Float) or String representation
var kotlinxDouble: Double = format.decodeFromString("""
0
""".trimIndent())
println(kotlinxDouble)
kotlinxDouble = format.decodeFromString("""
1.0
""".trimIndent())
println(kotlinxDouble)
kotlinxDouble = format.decodeFromString("""
"2.0"
""".trimIndent())
println(kotlinxDouble)
tried
object BigDecimalStringSerializer : KSerializer<BigDecimal> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BigDecimal", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: BigDecimal) {
encoder.encodeString(value.toString())
}
override fun deserialize(decoder: Decoder): BigDecimal {
val raw = decoder.decodeString() // <-- blows up when not a String representation, i.e. on 0 and 1.0 values from above
return BigDecimal(raw)
}
}
Is there something like decoder.getRawValue
or something like that?Dariusz Kuc
02/15/2021, 6:44 PM@Serializable
data class GraphQLResponse<T>(
val data: T? = null,
val errors: List<GraphQLError>? = null,
val extensions: Map<Any, Any>? = null // complains about missing serializer for Any
)
madsbf
02/17/2021, 9:24 AM@Serializable
sealed class Style {
@SerialName("default")
object Default : Style()
@SerialName("dark")
class Dark(...) : Style()
}
Now in order to future-proof the above in case new styles are introduced, we want to use the Default style in case a new type (“light” f.x.) is introduced.
Is there some idiomatic way to do this - besides using a custom serializer?Ivan Đorđević
02/17/2021, 11:40 AM@Serialize(with = CustomSerializer::class)
?Ivan Đorđević
02/18/2021, 12:37 PM