altavir
04/11/2020, 5:34 PMrharter
04/15/2020, 3:13 PMInvocationTargetException
due to a NoClassDefFoundError
when trying to invoke the serializer() method on a @Serializable
class. The class and method are found (via reflection using Ktor’s kotlinx.serialization content negotiation feature) and set accessible, but this still happens. The annotated data class is public, but in a dependent module from the actual server that is executed. Any idea what might be wrong?fkrauthan
04/16/2020, 9:57 PMSrSouza
04/17/2020, 3:41 PM@Abs val num: Int
=> {"num": -3}
that deserialize to 3.
I already create my owns annotation for Kotlinx.serialization, I know that supports it, but was for a new Enconder of mine, not from a existing one like Json.Sam Garfinkel
04/17/2020, 9:31 PMserializer
from a generic T
at compile-time? I know about KClass<T: Any>.serializer()
but that relies on reflection. Likely doesn’t exist, but it would be cool to be able to mark a generic reified argument as @Serializable
too (maybe limited to internal
or private
so that the constraint could be validated at compile time) so the compiler can validate it’s not used improperly and also to allow compile-time optimization of the inlined code to get the Serializer companion/object that’s available.SrSouza
04/18/2020, 9:58 PMCaused by: java.lang.ClassCastException: kotlin.collections.EmptyList cannot be cast to java.lang.String
How can I make my Serializer works in List and Map?
My currently serializer.
@Serializer(forClass = String::class)
object ChangeColor : KSerializer<String> { ... }
Usage that is causing this exception:
@Serializable(with = ChangeColor::class)
val messages: List<String> = listOf()
SrSouza
04/19/2020, 9:42 PM.serializer()
to this new that I created.ghosalmartin
04/22/2020, 11:27 AMAntanas A.
04/22/2020, 11:36 AMAntoine Gagnon
04/22/2020, 2:00 PMpatch
method generate by the @Serializable annotation. I can’t seem to figure out how to get a decoder to test it?crummy
04/23/2020, 10:53 PMJavalinJson.toJsonMapper = object : ToJsonMapper {
override fun map(obj: Any) = { /* Your implementation here */ }
}
I tried this:
JavalinJson.fromJsonMapper = object : FromJsonMapper {
override fun <T> map(json: String, targetClass: Class<T>): T {
return Json.parse(T::class.serializer(), json)
}
}
But get Unresolved reference: serializer
. Is this how to go about it?crummy
04/26/2020, 1:38 AMfun getAll(): Collection<User>
I get:
kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class LinkedValues. For generic classes, such as lists, please provide serializer explicitly.
I think I need to somehow indicate that this method should deserialize with User.serializer().list
. But I'm not sure how to do that.crummy
04/28/2020, 5:27 AM@Serializable(with = LocalDateSerializer::class)
(LocalDateSerializer is a class I've created.)
However, now I'm returning a Map<LocalDate, foo>
elsewhere and getting an error Can't locate argument-less serializer for class LocalDate
. How do I annotate this?Animesh Sahu
04/28/2020, 2:09 PMfun <T : Any> store(key: String, value: T, serializer: KSerializer<T>) {
json.stringify(serializer, value)
}
//To, maybe reified
fun <T : SomeSuperTypeForSerializable> store(key: String, value: T) {
json.stringify(T.serializer(), value)
}
basher
05/01/2020, 7:51 PMnull
, instead of having the key, value pair not present (in a dict)?Paul Woitaschek
05/04/2020, 10:19 AM@Serializable(with = LocalDateSerializer::class)
dagomni
05/05/2020, 4:30 PMdagomni
05/06/2020, 8:54 AMdagomni
05/06/2020, 12:45 PM@Serializable
sealed class Response<T> {
@Serializable
data class ResponseData<T>(
@SerialName("data")
val data: T
) : Response<T>()
@Serializable
data class AuthData(
@SerialName("data")
val data: AdditionalAuthorization?
) : Response<AdditionalAuthorization?>()
}
I get this error
Unexpected JSON token at offset 0: Expected '[, kind: SEALED'.
It fails even with the simplest json. Apart from sealed classes I've also tried interface, open, abstract - same error, different kind given in message. Is what I'm trying to achieve supported by kotlinx.serialization?Shan
05/14/2020, 5:59 AMJsonDecodingException
? What is strange is that it works in certain cases, but fails in others. When I get back this response:
{
"id": 1,
"status": 1,
"info": 0
}
It works fine and returns IN_PROGRESS
. When I get this response:
{
"id": 3,
"status": 3,
"info": 0
}
I encounter this error:
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 28: Expected string or non-null literal
Which is thrown from takeString()
in JsonReader.kt
after trying to call decodeInt()
. If both of them were failing I would be a little less confused, but with only one of them failing I am at a loss. Is it interpreting the 3
as a byte or something, and thus failing the TC_OTHER
and TC_STRING
check, whereas the 1
is interpreted as a TC_STRING
somehow? All I can think of 🤔bsimmons
05/14/2020, 2:45 PMMrPowerGamerBR
05/18/2020, 10:00 PM@Serializable
to enums? I'm having issues with enums without the @Serializable
annotation in JavaScript (haven't tested in the JVM yet)Franco
05/21/2020, 4:33 AMtype is not registered for polymorphic serialization in the scope of class
This is my code:
@Serializable
sealed class TestClass {
@Serializable
@SerialName("TEST_1")
data class TestClass1(
val value1: String,
val value2: Int
) : TestClass()
@Serializable
@SerialName("TEST_2")
data class TestClass2(
val value1: String
) : TestClass()
}
I found this link where the issue is discussed: https://github.com/Kotlin/kotlinx.serialization/issues/457 and seems like a fix is ready (but not yet released).
I was wondering if there is a workaround that allows us to make it work until the fix is released? (I know that I can do this using a Serializer but not sure how to build it in a way that I can remove it once the fix for this is released and nothing will break).Kroppeb
05/21/2020, 12:02 PMEncoder
/ Decoder
and I'm confused why there is a Encoder
and a CompisiteEncoder
. Why does there need to be a destiction?Sam Garfinkel
05/21/2020, 5:16 PMcommonMain
at the built-ins there, but there are some other types that the IDE isn’t complaining about, like java.nio.Path
.Patrick
05/22/2020, 12:43 PMCannot access 'Serializable': it is internal in '<http://kotlin.io|kotlin.io>'
Patrick
05/25/2020, 8:42 AMinterface ExampleInterface
@Serializable
enum class Example: ExampleInterface {
TEST,
TEST2
}
@Serializable
enum class Example2: ExampleInterface {
WOW,
WOW2
}
@Serializable
data class ExampleWrapper(val m: ExampleInterface)
fun main() {
val module = SerializersModule {
polymorphic(ExampleInterface::class) {
Example::class with Example.serializer()
Example2::class with Example2.serializer()
}
}
val json = Json(JsonConfiguration.Stable, context = module)
val jsonData = json.stringify(ExampleWrapper.serializer(), ExampleWrapper(Example.TEST))
println(jsonData)
}
When I run this, the following error message is displayed:
Uncaught Kotlin exception: kotlin.IllegalStateException: Enums cannot be serialized polymorphically with 'type' parameter. You can use 'JsonConfiguration.useArrayPolymorphism' instead
Now if I do what the error message says, I will get back something like this:
{
"m": [
"barcodeParser.Example",
"TEST"
]
}
But I would rather have it look like this:
{
"m": "TEST"
}
How can I achieve this?Patrick
05/26/2020, 9:51 AMedenman
05/26/2020, 9:24 PMFoo
that i want to deserialize to null if the server gives me {}
for the json. started to implement a JsonTransformingSerializer
but it has a type bound <T: Any>
which appears to mean I can’t create a `JsonTransformingSerializer<Foo?>`…I’ve worked around it for now by adding the custom serialization logic on every object that has a Foo, but that’s far from idealSean Keane
05/28/2020, 9:20 PM@Serializable
enum class Error(@SerialName("code") val code: String? = null) {
UNEXPECTED("0001")
}
If I pass it a json string of the following I want it to create an enum response.
{
"code": "0001"
}
I used to use CommonEnumSerializer but its been deprecated. Is there an alternative?Sean Keane
05/28/2020, 9:20 PM@Serializable
enum class Error(@SerialName("code") val code: String? = null) {
UNEXPECTED("0001")
}
If I pass it a json string of the following I want it to create an enum response.
{
"code": "0001"
}
I used to use CommonEnumSerializer but its been deprecated. Is there an alternative?ankushg
05/28/2020, 10:16 PMSean Keane
05/29/2020, 12:23 AMSean Keane
05/29/2020, 1:54 PMError.values().first { it.errorCode == "0001" }
Kroppeb
05/29/2020, 5:47 PMSean Keane
05/29/2020, 6:03 PMPatrick
06/02/2020, 10:26 AMSean Keane
06/02/2020, 12:55 PMreturn YOURENUMCLASS.values().first { it.errorCode == code }