dimitar_
06/14/2021, 6:59 PMMBegemot
06/15/2021, 4:40 PMdimitar_
06/16/2021, 3:00 PMobject LongRangeSerializer: KSerializer<LongRange> {
override val descriptor = buildClassSerialDescriptor("LongRange") {
element<Long>("first")
element<Long>("last")
}
override fun deserialize(decoder: Decoder): LongRange {
var first = 0L
var last = 0L
decoder.decodeStructure(descriptor) {
first = decodeLongElement(descriptor, 0)
last = decodeLongElement(descriptor, 1)
}
return first..last
}
override fun serialize(encoder: Encoder, value: LongRange) {
encoder.encodeStructure(descriptor) {
encodeLongElement(descriptor, 0, value.first)
encodeLongElement(descriptor, 1, value.last)
}
}
}
And I tried:
@Serializable class Container(@Serializable(with = LongRangeSerializer::class) val ranges: Array<LongRange>)
with no luck.
Can't seem to find any pointers anywhere. Tried using a List also, tried also with @Contextual
- same issue.
The error is: Kotlin: Serializer has not been found for type 'LongRange'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
marzelwidmer
06/16/2021, 5:36 PM@JsonFormat(pattern = "yyyy-MM-dd")
Matthew Cachia
06/17/2021, 11:54 AMsendoav
06/18/2021, 8:32 AMNick
06/18/2021, 7:32 PMkotlinx.serialization.SerializationException: Serializer for class 'Pagination' is not found.
My data class:
import kotlinx.serialization.Serializable
@Serializable
data class Pagination<T>(
val count: Int,
val next: String? = null,
val previous: String? = null,
val results: List<T>
)
Build.gradle:
plugins {
id("com.android.application")
kotlin("android")
id("kotlin-android-extensions")
kotlin("plugin.serialization") version Versions.kotlin
}
Rainer Schlonvoigt
06/21/2021, 2:33 PM@Serializable(with=...)
on a Set of a custom class?
More specifically, my scenario looks like this:
@Serializable
data class MyClass(
@Serializable(with=???)
val mySet: Set<UUID>
)
where UUID is from java.util so i can’t annotate itjeggy
06/21/2021, 6:56 PM@Serializable
for libraries?christophsturm
06/22/2021, 8:09 AMit.mapValues { (_, value) ->
when (value) {
is JsonPrimitive -> value.content
JsonNull -> null
is JsonObject -> TODO()
is JsonArray -> value.map { it.jsonPrimitive.content }
}
}
spierce7
06/24/2021, 1:41 PMMustafa Ozhan
06/25/2021, 3:43 PMval jvmJar by tasks.getting(Jar::class) {
doFirst {
manifest {
attributes["Main-Class"] = "${ProjectSettings.packageName}.backend.BackendAppKt"
from(
configurations
.getByName("runtimeClasspath")
.map { if (it.isDirectory) it else zipTree(it) }
)
}
}
}
this task is failing now with this error
> Task :backend:jvmJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':backend:jvmJar'.
> Cannot convert the provided notation to a File or URI: ZIP '/Users/mustafa.ozhan/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-serialization/1.6.0/8377a04c8a0e11d31ef17f1c0c06bda6c7b2fdc4/ktor-serialization-jvm-1.6.0.jar'.
The following types/formats are supported:
- A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
- A String or CharSequence URI, for example 'file:/usr/include'.
- A File instance.
- A Path instance.
- A Directory instance.
- A RegularFile instance.
- A URI or URL instance.
- A TextResource instance.
any idea how we can fix that ?Rohen Giralt
06/29/2021, 2:28 PMval json = Json {
contextual(MyInterface::class, MyInterfaceSerializer)
}
but since that works on the KClass of the underlying type, I get an
kotlinx.serialization.SerializationException: Serializer for class 'MyInterfaceImpl' is not found.
I could use the encodeToString
method explicitly, rather than using contextual serialization, but since I’m using Ktor’s ContentNegotiation feature, I wouldn’t be able to retain any of its features (such as a correct Content-Type header, etc.) without a bunch more work.
Finally, I could also register each subtype polymorphically, but that seems like
• a lot of repeated code (since all serializers would be the same)
• a little bit of a breach of encapsulation (I’d like to have the serializer in a separate module, since the interface and its implementations don’t need to know about how they’re being displayed to the user, and vice versa)
Any ideas?Lilly
06/29/2021, 10:19 PMitnoles
06/30/2021, 2:06 AMPhilip S
06/30/2021, 9:02 AMWaqas Tahir
07/01/2021, 2:20 PMWaqas Tahir
07/02/2021, 1:24 AMYuri Drigin
07/06/2021, 12:39 PMCaused by: java.lang.AssertionError: Couldn't load KotlinClass
@Serializable
data class SocketMessage<T>(
val timestamp: Long?,
val message: T
)
How can I fix it? kkotlinx-serialization 1.2.1
Thanksbenkuly
07/07/2021, 10:54 AM@Contextual
does not work with IR compiler and generic classes or do I something wrong? Here is a minmal example: https://gitlab.com/benkuly/trixnity/-/blob/ir-compiler-issues/trixnity-core/src/co[…]in/kotlin/net/folivo/trixnity/core/model/events/UnsignedData.ktJason Ankers
07/08/2021, 4:03 AM@Ref
and have the serializer only serialize the id
property of the referenced type. Is this possible? I.e.
data class Video(
val name: String,
@Ref val user: User,
)
// serialized to:
{
name: "example",
user: {
id: "123"
}
}
Mark Allanson
07/08/2021, 2:40 PMkotlinx.serialization
when all you're doing is serializing with no care for deserialization later, or allow specification of another field instead of type as the discriminatorsnowe
07/09/2021, 1:36 AM@Serializable
data class ChangeOrderInput(
val oldProposal: OldProposal,
val newProposals: List<NewProposal>
)
@Serializable
data class OldProposal(
val product: Product
)
@Serializable
data class NewProposal(
val id: String,
val product: Product,
val changeOrderType: ChangeOrderType? = null
)
@Serializable(with = ProductSerializer::class)
sealed class Product {
abstract val type: ProductType
abstract val bundle: ProductBundle
}
object ProductSerializer : JsonContentPolymorphicSerializer<Product>(Product::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out Product> {
val typeString = element.jsonObject["type"]?.jsonPrimitive?.content ?: error("`type` wasn't provided for `product`")
return when (ProductType.valueOf(typeString)) {
ProductType.Cash -> CashProduct.serializer()
<http://ProductType.Loan|ProductType.Loan> -> LoanProduct.serializer()
}
}
}
@Serializable
data class CashProduct(
override val type: ProductType = ProductType.Cash,
override val bundle: ProductBundle,
val totalPrice: BigDecimal
) : Product()
@Serializable
data class LoanProduct(
override val type: ProductType = <http://ProductType.Loan|ProductType.Loan>,
override val bundle: ProductBundle,
val vendor: String,
val term: BigDecimal,
val apr: BigDecimal,
val totalPrice: BigDecimal
) : Product()
@Serializable
data class ExampleResponse(
val version: String = "",
val outputObjects: List<NewProposal>? = listOf()
)
When I serialize a ChangeOrderInput object:
val inputJsonString = input.reader().use { it.readText() }
val request = Serialization.json.decodeFromString(ChangeOrderInput.serializer(), inputJsonString)
val response = runBlocking {
changeOrderWrapper.handleRequest(request)
}
val outputJsonString = Serialization.json.encodeToString(response)
output.writer().use { it.write(outputJsonString) }
it serializes both the oldProposal and newProposal correctly (see request object in the image).
But when I try to deserialize the ExampleResponse
object, it deserializes everything except the NewProposal#type
field.
I just now thought that maybe this was due to the classDiscriminator property on the Serializer, but that is not it. I tried changing it to #class
and it still has the same result.
Anyone know what is going on here?akuleshov7
07/12/2021, 10:59 AMkubele
07/14/2021, 5:13 PM@Serializable
data class Foo(
val list: List<String> = emptyList()
)
val foo = Json.encodeToString(Foo())
println(foo)
prints
{}
but this implementation:
@Serializable
data class Foo(
val list: List<String>
)
val foo = Json.encodeToString(Foo(emptyList()))
println(foo)
prints
{
"list": []
}
I have no idea why it’s not
{
"list": []
}
in both casesPhạm Minh Tuấn
07/15/2021, 2:32 AM{
"accountNo": "",
"address": "4",
"amount": 1719000,
"bankCode": "950012",
"channel": "6015",
"customerCode": "Evn15",
"customerName": "EXIM",
"localDateTime": "20210713203042",
"providerCode": "196000",
"senderInfo": "Evn15",
"serviceCode": "000010",
"sign": "",
"trace": "000295",
"vnpayDateTime": "20210713204024"
}
but when json parsed, it throw error
Unexpected JSON token at offset 300: Expected quotation mark '\"', but had '\"' instead\nJSON input: .....gn\": \"\",\n\t\"trace\": \"000295\",\n\t\"vnpayDateTi
that is normal json i dont know why it throw error, please helpaltavir
07/15/2021, 9:21 AMkotlinx.serialization.MissingFieldException: Field 'properties' is required for type with serial name 'solid.box', but it was missing.
The propertiese
field has default null value and is declared like
public var properties: Config? = null
protected set
The error is thrown when trying deserialize descendant that has the default null value. Seems like a bug to me.rsetkus
07/20/2021, 4:14 PMkotlinx.serialization.json.internal.JsonDecodingException: Expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of kotlinx.serialization.Polymorphic<List>, but had class kotlinx.serialization.json.JsonArray (Kotlin reflection is not available)
Json: [ //internal structure of json object ]
Container data class: List<R>
Function used for deserialization:
internal inline fun <reified R : Any> String.convertToDataClass() =
Json {
ignoreUnknownKeys = true
}.decodeFromString(R::class.serializer(), this)
Code example: "[…]".convertToDataClass<List<SomeDataClass>()
When going through Ktor pipeline everything works fine but it is breaking on attempt to deserialize the same response body cached as string to object.
Any ideas how to fix this?ankushg
07/21/2021, 8:42 PMIt also uses Kotlin 1.5.20 as default.Is there a way to ignore this "default" and use v1.2.2 with Kotlin 1.5.10? (Encountered a bug that's fixed in 1.2.2, but can't bump past 1.5.10 yet because of another issue I'm trying to chase down)
Dario Valdespino
07/25/2021, 8:56 PM@Serializable
annotation). But what about primitives like Int
and String
? Surely there must be a better way than manually keeping a list of all natively serializable types, right?