Marcin Wisniowski
04/04/2021, 4:40 PM@Serializable(with = MySerializer::class) val item: MyClass
But now I need to serialize a list of these items:
val items: List<MyClass>
How can I make it work? I cannot use the same annotation, since now it's a List
.diesieben07
04/06/2021, 3:01 PMMarcin Bak
04/06/2021, 9:16 PMkotlin("plugin.serialization")
and serialization dependency in common module I don't get generated serializers and get a warning on @Serializable
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.The main build.gradle.kts contains proper buildscript dependencies
classpath(kotlin("gradle-plugin", version = "1.4.31"))
classpath(kotlin("plugin.serialization", version = "1.4.31"))
Nothing seems to help. I've removed all unnecessary code, reimported project, invalidated caches but to no avail. Any ideas?Yardenavirav
04/08/2021, 11:04 PMJsonDecodingException
is produced BECAUSE of that property? I can workaround it by using a custom serializer for that property but it's quite an overhead. e.g - for this data class data class AA(val a: Int = 0)
and this json {"a":"N/A"}
I will get this output AA(a = 0)
despite it failed to decode it.Prayansh Srivastava
04/13/2021, 10:00 PM// MainActivity.java
public class MainActivity extends Activity {
@Serializable
static class Props {
// my POJO
int count;
String name;
}
@Override
public void onCreate() {
super.onCreate();
Props.serializer() // No such method available
}
}
I am not sure if i am misusing / misunderstanding the API here, so feel free to correct 🙂
Thx!Tomas Kormanak
04/15/2021, 1:20 PM@Serializable
data class Params(val deviceId: String = "someId")
val params = Params()
val result = Json.encodeToString(Params.serializer(), params)
console.log("params", params)
console.log("result ", result)
I get this result:Justin Tullgren
04/15/2021, 9:04 PMkotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 26: Expected '[, kind: SEALED'
Any help is appreciated.rocketraman
04/21/2021, 6:55 PMthanksforallthefish
04/23/2021, 2:25 PM@JsonAnyGetter
from jackson in kotlinx serialization? or, assuming I have a json like
{
field: value,
second: value,
another: value
}
how can I map into something a class like
@Serializable
class MyClass(val field: String, val additional: Map<String, String>)
?
bonus question, coming from jackson, I spent most of day trying to figure out how to handle val additional: Map<String, Any>
, but that is not really possible with kotlin serialization right?Yaniv Sosnovsky
04/24/2021, 3:16 PM{"drinks":[{"strDrink":"Jelly Bean","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/bglc6y1504388797.jpg","idDrink":"13775"},{"strDrink":"Turf Cocktail","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/utypqq1441554367.jpg","idDrink":"12418"}]}
but sometimes the server returns the following:
"{"drinks":"None Found"}"
Before using kotlinx I had a custom type convertor with Gson, and that worked fine, but I’m struggling with kotlin.
I tried to create a KSerializer to try to parse the result, and return an object with an empty list if an exception is thrown, but I keep getting this exception:
java.lang.IllegalStateException: Reader has not consumed the whole input: JsonReader(source='{"drinks":"None Found"}', currentPosition=22, tokenClass=1, tokenPosition=10, offset=11)
Any ideas how to solve this? I can always return a string instead of a data class, but this is ugly AFspierce7
04/26/2021, 7:21 AMDenys
04/26/2021, 1:22 PM@Serializable
data class PagedResult<T, P : PagedResult.Pagination>(val items: List<T>, val pagination: P)
Everything works fine until the moment when I enable Proguard. An error like this occurs: java.lang.IllegalArgumentException: Unable to create converter for f.a.b.d.i.g.a<> (). f.a.b.d.i.g.a is just a class PagedResult (I see it in mapping.txt) The rules for Proguard are taken from the official kotlinx.serialization repository. Can someone tell me what the problem is?Karlo Lozovina
04/28/2021, 10:17 AMdata class Foo(val foo: String)
is there an easy way to serialize it into just a string, not an object like {"foo": "SomeString"}
? without writing a custom serializer...Karlo Lozovina
04/28/2021, 6:11 PMandrewreitz
04/28/2021, 7:32 PMjava.lang.ClassCastException: class [[Ljava.lang.Object; cannot be cast to class [[Ljava.lang.Float; ([[Ljava.lang.Object; and [[Ljava.lang.Float; are in module java.base of loader 'bootstrap')
at raytracer.math.Matrix$$serializer.deserialize(Matrix.kt:9)
at raytracer.math.Matrix$$serializer.deserialize(Matrix.kt:9)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:63)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:32)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
Any ideas of where to look and how to fix that?Norbi
04/29/2021, 9:09 PM'void kotlinx.serialization.modules.SerialModuleImpl.<init>(java.util.Map, java.util.Map, java.util.Map, java.util.Map)'
java.lang.NoSuchMethodError: 'void kotlinx.serialization.modules.SerialModuleImpl.<init>(java.util.Map, java.util.Map, java.util.Map, java.util.Map)'
at kotlinx.serialization.modules.SerializersModuleKt.<clinit>(SerializersModule.kt:74)
at kotlinx.serialization.json.Json$Default.<init>(Json.kt:65)
at kotlinx.serialization.json.Json$Default.<init>(Json.kt)
at kotlinx.serialization.json.Json.<clinit>(Json.kt)
at kotlinx.serialization.json.JsonKt.Json$default(Json.kt:130)
- Kotlin version: 1.5.0
- kotlinx-serialization version: 1.2.0
- Kotlin platforms: multiplatform (JVM + JS)
- Gradle version: 6.8.3
- IDE version: 2021.1.1 RC
- Other relevant context: Java 15Have you seen anything similar? Thanks.
Ryan Simon
04/30/2021, 4:09 PMYuri Drigin
05/01/2021, 9:00 AMAdam S
05/01/2021, 6:56 PMval jsonMapper = Json {
prettyPrint = true
encodeDefaults = true
coerceInputValues = true
}
val inputPet = Pet(
name = petName,
tags = emptyList(),
photoUrls = emptyList(),
status = petStatus
)
val inputPetJson = jsonMapper.encodeToString(inputPet)
Actual :{"name":"blaaaah","status":"available"}
Expected:{"name":"blaaaah","photoUrls":[],"tags":[],"status":"available"}
I’ve tried various combinations of encodeDefaults and coerceInputValues. The problem is that Pet
has other fields that default to null. I don’t want to include those.Mikhail Buzuverov
05/03/2021, 4:21 AM1.2.0
library version):
TypeError: tmp$.serializer is not a function
at <global>.compiledSerializerImpl(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:72080)
at <global>.serializerOrNull(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:67234)
at <global>.reflectiveOrContextual(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:67226)
at <global>.builtinSerializer(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:67220)
at <global>.serializerByKTypeImpl(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:67159)
at <global>.serializer(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:67129)
at data.OperationResultSerializationTest.operationSuccessSerializationTest(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:77540)
at <global>.<unknown>(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:77588)
at Context.<anonymous>(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_490920/commons.js:57)
Or (with 1.1.0
library version):
SerializationException: Serializer for class 'OperationResult' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
at Object.captureStack(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:40068)
at SerializationException.constructor(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:40401)
at SerializationException.constructor(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:40427)
at SerializationException.constructor(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:40447)
at IllegalArgumentException.init(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:40458)
at SerializationException.init(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:67048)
at <global>.platformSpecificSerializerNotRegistered(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:72030)
at <global>.serializer(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:67125)
at data.OperationResultSerializationTest.operationSuccessSerializationTest(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:77226)
at <global>.<unknown>(C:/Users/Akela/AppData/Local/Temp/_karma_webpack_73575/commons.js:77274)
I described the issue: https://github.com/Kotlin/kotlinx.serialization/issues/1448
Could you help me? Is there a workaround?robertoestivill
05/05/2021, 11:32 AMribesg
05/07/2021, 1:22 PMDominaezzz
05/09/2021, 9:52 PMRobert Jaros
05/10/2021, 9:54 PMHelio
05/11/2021, 7:59 AM{
"status": "OneOf<Pending|Queued|InProgress|Finished|NotBuilt>"
}
If I want to serialise it to Enum, I would have:
@Serializable
enum class Status {
@SerialName("Pending") PENDING,
@SerialName("Queued") QUEUED,
@SerialName("InProgress") IN_PROGRESS,
@SerialName("Finished") FINISHED,
@SerialName("NotBuilt") NOT_BUILT,
}
@Serializable
data class ServerResponseModel(val status: Status)
However, when I get a response from the server, let’s say InProgress
all I need to do is build a new Model and return the status as being IN_PROGRESS
.
What would be the best way to achieve that? Do I need to write a custom serialisation in this case? Also, does it makes sense to create an Enum class
for ServerResponseModel
?
Thanks for your assistance.tylerwilson
05/11/2021, 12:47 PMkotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@955628This works fine with serialization 1.1.0. Should the new serialization work with Kotlin 1.4.32? Do we need an updated coroutines lib?
Michael Friend
05/12/2021, 6:51 PMtype
property on the json object somewhat overloaded where for one value i want to deserialize it into class A, but any other value is of type B and I need access to the type
value. Adding type
as a property gives an error
because it has property name that conflicts with JSON class discriminator 'type'. You can either change class discriminator in JsonConfiguration, rename property with @SerialName annotation or fall back to array polymorphism
I couldnt find any reference too array polymorphism anywhereMichael
05/13/2021, 9:43 PM@Serializable
data class Example(val content: String)
suspend fun main(args: Array<String>) {
val content2 = "a".repeat(126) + "\n"
Json.encodeToString(content2)
}
This fails if as long as the number is >= 126
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 129 out of bounds for length 129
at kotlinx.serialization.json.internal.JsonStringBuilder.appendStringSlowPath(JsonStringBuilder.kt:102)
at kotlinx.serialization.json.internal.JsonStringBuilder.appendQuoted(JsonStringBuilder.kt:61)
at kotlinx.serialization.json.internal.Composer.printQuoted(Composers.kt:42)
at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeString(StreamingJsonEncoder.kt:203)
at kotlinx.serialization.internal.StringSerializer.serialize(Primitives.kt:139)
at kotlinx.serialization.internal.StringSerializer.serialize(Primitives.kt:136)
at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:211)
at kotlinx.serialization.json.Json.encodeToString(Json.kt:80)
Peter Vegh
05/15/2021, 12:29 PMspierce7
05/16/2021, 4:38 PMMap<String, Any>
and Date
representations into Firestore’s Timestamp
.
• In JS I’d need to output js objects (Not kotlin Map’s or anything)
Assuming this is possible:
1. Would I be able to stay away from Compiler plugins and stick only to the runtime?
2. Would configuring an object to change to Firestore types present any problems to that same object being serialized to json?spierce7
05/16/2021, 4:38 PMMap<String, Any>
and Date
representations into Firestore’s Timestamp
.
• In JS I’d need to output js objects (Not kotlin Map’s or anything)
Assuming this is possible:
1. Would I be able to stay away from Compiler plugins and stick only to the runtime?
2. Would configuring an object to change to Firestore types present any problems to that same object being serialized to json?sandwwraith
05/17/2021, 9:22 AMspierce7
05/17/2021, 3:03 PMsandwwraith
05/17/2021, 3:07 PMspierce7
05/17/2021, 3:08 PMsandwwraith
05/24/2021, 10:51 AMSerialDescriptor.annotations
(for class) SD.getElementAnnotations
(for properties)@SerialInfo
itself