Kris Wong
12/10/2019, 3:36 PMankushg
12/11/2019, 3:54 AMexpect typealias
as a member of a data class
that can be serialized?dambakk
12/11/2019, 12:35 PMid("kotlinx-serialization") version "1.3.61"
to the plugins block and implementation(serialization("-common"))
dependency to commonMain. Then I try to annotate a data class with @Serializable
and IDEA says it fine, but when running gradle build it says Cannot access 'Serializable': it is internal in '<http://kotlin.io|kotlin.io>'
. It seems similar to this issue: https://github.com/Kotlin/kotlinx.serialization/issues/395 but here the solution was to add the plugin to the plugin block - which I have already done. Any suggestion what might cause this?ankushg
12/11/2019, 7:33 PMactual typealias MyClass = String
for some platforms, while using actual data class MyClass(val someVal: String)
on other platforms. We also have an expect val MyClassSerializer: KSerializer<MyClassSerializer>
which we set equal to either StringSerializer
or the generated serializer for the data class. We declare contextual(MyClass::class, MyClassSerializer)
in our common serialization context.
On platforms where we typealias to Strings, we get a SerializerAlreadyRegisteredException: Serializer for class String already registered in this module
, even though our MyClassSerializer
points to the built-in StringSerializer
.
Is there a way to avoid this error?
• I see the overwriteWith
option, but it feels scary to use this since I'd be overwriting behavior for ALL Strings. It seems easy to declare a custom serializer for a type without realizing that this might change behavior for every String in your app.
• My understanding is that using an inline class instead of typealias might help here, but I'm not sure about the state of inline classes in multiplatform.
• Can I contextually register a serializer if-and-only-if there isn't already one registered for the type?
• Is it possible for the contextual registration to do an equality check on the KSerializer
and not throw an error if we're just redeclaring the preexisting association?marstran
12/12/2019, 10:50 PMShan
12/15/2019, 11:13 PMtylerwilson
12/16/2019, 3:04 PM@Serializable(with = CustomDoubleSerializer::class)
since there are a large number of these formatted doubles. Thank you.Nagarajan
12/16/2019, 5:01 PMclass kotlin.Pair is not registered for polymorphic serialization in the scope of class kotlin.Any
Find my sample code here
@Serializable
sealed class Fruit(@Serializable(OptionSerializer::class) var meta: Option<@Polymorphic Any>) {
@Serializable
data class Blueberry(@Serializable(OptionSerializer::class) val metaData: Option<Pair<String, String>> = Option.empty()) :
Fruit(metaData)
}
@Serializable
data class Basket(
val blueberry: Fruit.Blueberry = Fruit.Blueberry()
)
@Test
fun `Test Serialize Fruit`() {
val basket = Basket(
Fruit.Blueberry(Option.just(Pair("firstVal", "SecondVal")))
)
validateSerialize(basket, Basket.serializer())
}
jef
12/16/2019, 10:16 PMjimn
12/17/2019, 9:42 AM@Serializable
data class RowBinMeta(val name: String, val coord: Array<Int>, val typ: /*an enum*/ TypeMemento)
val meta: List<RowBinMeta> = RowBinMeta.RowBinMetaList(elements)
val out = Json(JsonConfiguration.Default).toJson( meta )
Exception in thread "main" kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class kotlin.collections.List. For generic classes, such as lists, please provide serializer explicitly.
what am i missing?Sujit
12/18/2019, 12:14 AM@Serializable
class with lots of members like this:
@Serializable
class TooManyMembers {
val mem1: Int? = null
val mem2: Int? = null
.
.
.
val mem248: Int? = null
}
That's a class with 248 members. Instantiating this member with val obj = TooManyMembers()
throws this error at runtime:
Too many arguments in method signature in class file SerializationTests$TooManyMembers
java.lang.ClassFormatError: Too many arguments in method signature in class file SerializationTests$TooManyMembers
If I remove just one member, no more of that error. Is that really a limit (247) with numbers of members one can have in a @Serializable
class? What can I do if I have an object with more members? 247 seems way too low. This is with 0.14
and kotlin 1.3.61
lewis
12/20/2019, 6:00 PMMap
? I've tried just passing HashMap has a type but I'm just seeing Can't locate argument-less serializer for class java.util.HashMap. For generic classes, such as lists, please provide serializer explicitly.
nrobi
12/23/2019, 2:20 PMkotlinx-serialization
? I’m using 0.14.0
, and I’m gettin pretty randomly java.lang.NoClassDefFoundError:
, some time it works, some time it doesn’t 🤷molikuner
12/23/2019, 2:25 PMclass InlineSerializerTest {
@Test
fun `simple TestClass serialization test`() {
val json = "\"test\""
val parsed = Json.parse(TestClass.Companion, json)
assertEquals(
TestClass("test"),
parsed
)
}
}
@Serializable(with = TestClass.Companion::class)
data class TestClass(val a: String) {
@Serializer(forClass = TestClass::class)
companion object : TestSerializer {
override val descriptor: SerialDescriptor = StringDescriptor
// when removing this function the serialization crashes.
override fun deserialize(decoder: Decoder): TestClass {
return TestClass(decoder.decodeString())
}
override fun serialize(encoder: Encoder, obj: TestClass) = throw UnsupportedOperationException()
}
}
interface TestSerializer: KSerializer<TestClass> {
override fun deserialize(decoder: Decoder): TestClass {
return TestClass(decoder.decodeString())
}
}
As you can see in the companion object, I redefined the deserialize function with the same body as the extending interface. When removing this implementation the deserialization crashes:
Invalid JSON at 0: Expected '{, kind: CLASS'
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 0: Expected '{, kind: CLASS'
at kotlinx.serialization.json.internal.JsonReader.fail(JsonReader.kt:293)
at kotlinx.serialization.json.internal.StreamingJsonInput.beginStructure(StreamingJsonInput.kt:39)
... // if needed I can provide the full stack later
Any idea how to resolve this? Whats really weird is that the error says that a class
kind descriptor would be used, but String is definetly not a class kind type.Kris Wong
12/23/2019, 5:26 PMjeggy
12/30/2019, 12:30 AMJsonObjectBuilder
doesn't have any suspend
functions.
What would then be the recomended way of building a JSON object across multiple coroutines?Gurupad Mamadapur [FH]
01/03/2020, 1:43 PM@Serializable
class BaseResponse<T> {
val data: T? = null
@SerialName("status")
val isSuccess: Boolean = false
@SerialName("session")
val isSessionExpire: Boolean = false
@SerialName("message")
val message: String = ""
}
Can this be serialized - BaseResponse<String>
? There is an error logged ->
java.lang.UnsupportedOperationException: The only generic classes supported for now are standard collections
got class class dto.BaseResponse (Kotlin reflection is not available)
I did read the documentation regarding requiring a custom serializer in case of generics but wasn't able to understand it. Any help would be much appreciated.Kris Wong
01/07/2020, 3:05 PMHauke Radtki
01/08/2020, 8:19 PMjw
01/13/2020, 6:43 PMagta1991
01/18/2020, 7:06 PMUnknownFieldException: Unknown field for index -2
I'm getting this when I try to use Mapper.unmap(serializer, map) call. I made a minimal reproduction code snipet.Edward Taylor
01/20/2020, 9:00 PMsealed class ManyHandsData: JSAction() {
@Serializable
data class handError(val message: String, val errorCode: Int): ManyHandsData()
@Serializable
data class DATA_INPUT(val openSensorInput: Int, val closeSensorInput: Int): ManyHandsData()
@Serializable
data class currentGripDidChangeExtended(val currentGrip: Int, val buttonSelected: Int, val currentButtonIndex: Int): ManyHandsData()
}
I've tried this:
internal val handDataActionSerializers = listOf(
JSAction.ManyHandsData.handError.serializer(),
JSAction.ManyHandsData.DATA_INPUT.serializer(),
JSAction.ManyHandsData.currentGripDidChangeExtended.serializer()
).associateBy { it.descriptor.name}
which doesn't have the simple name.
I was thinking maybe Delegates are the way to go (doing this for mapping js methods) but I'm struggling to think how to integrate that with data classes..
The goal would a mapping similar to above which allows me to retrieve a serializer by the string of an identifier (eg "DATA_INPUT")ribesg
01/27/2020, 10:12 AMBig Chungus
01/27/2020, 2:24 PMdambakk
01/28/2020, 2:46 PMval userIds: List<MemberInput> = listOf(...)
val json: JsonElement = Json.nonstrict.toJson(MemberInput.serializer().list, userIds)
<http://client.post|client.post><HttpResponse> {
header("Content-Type", "application/json")
header("Accept", "*/*")
body = json
}
This gives me an exception when setting it as the body to the request: java.lang.ClassCastException: kotlinx.serialization.json.JsonObject cannot be cast to java.lang.String
. (I should be able to set the userIds
as body out of the box, but for debugging purposes…)
First; is this a kotlinx.serialization or a ktor issue? Secondly; what’s wrong? 🙂 I appreciate any help! 🙂turansky
02/04/2020, 7:31 AMSerialClassDescImpl("MyClass")
in 0.14.0-1.3.70-eap-134
?Shan
02/04/2020, 6:59 PMSerializersModule
?vanniktech
02/05/2020, 9:41 AMnapperley
02/06/2020, 10:57 PMMarc Knaup
02/10/2020, 8:35 AMfun serializer() =
instead of val serializer get() =
/ val serializer =
?
I’m working on a library which stands before a similar decision and would love to hear it :)