Dariusz Kuc
07/21/2022, 5:38 PMorg.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration Variables
File being compiled: (35,3) in <Foo.kt>
The root cause java.lang.AssertionError was thrown at: org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor.getModality(LazyClassDescriptor.java:564)
...
Caused by: java.lang.AssertionError: Recursion detected in a lazy value under LockBasedStorageManager@2f5a443e (TopDownAnalyzer for JVM)
Remo
07/23/2022, 1:32 PMsealed interface Convertible {
fun convertUserInput(value : String): String
}
@Serializable
@SerialName("CustomConvertible")
class CustomConvertible(): Convertible {
override fun convertUserInput(value : String): String {
return ""
}
}
@Serializable
class DTOAttribute(val convertibles : List<Convertible> = emptyList())
Later on, I’d like to encode the DTOAttribute
with val string = Json.encodeToString(dtoAttr)
Calling this, gives me the following exception:
kotlinx.serialization.SerializationException: Class 'CustomConvertible' is not registered for polymorphic serialization in the scope of 'Convertible'.
Mark the base class as 'sealed' or register the serializer explicitly.
What am I doing wrong?mytrile
07/24/2022, 5:48 PMMR3Y
07/26/2022, 9:02 PM{
"responseHeader": {
"a": 0,
"b": 407,
"c": {
....
}
},
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"foo": "foo",
"bar": "bar"
},
{
"foo": "foo",
"bar": "bar"
}
]
},
"unimportant": {
"unimportant": []
}
}
I'm interested only in the response
part, so, Instead of deserializing the entire response, I want to just deserialize this part. is this possible with kotlinx.serialization? Of course I can deserialize the whole response & get rid of the irrelevant parts on the client side, but this would be a waste of time. So, it would be nice if there is a built-in way to do this.alllex
07/27/2022, 7:58 AMimport kotlinx.serialization.*
import kotlinx.serialization.json.Json
@Serializable
sealed class ApiResponse<T> {
@Serializable
@SerialName("result")
data class Ok<T>(val result: T) : ApiResponse<T>()
@Serializable
@SerialName("error")
data class Error<T>(val error: ErrorResponse) : ApiResponse<T>()
}
@Serializable
data class VersionResponse(val version: String)
@Serializable
data class ErrorResponse(val description: String)
fun main() {
val data: ApiResponse<VersionResponse> = ApiResponse.Ok(VersionResponse("v42"))
println(Json.encodeToString(data)) // prints {"type":"result","result":{"version":"v42"}}
}
The last line mentions the result I want to achieve: ApiResponse
is discriminated using type
, and the result
field is just serialized as it is.
However, running this with Kotlin 1.7.10 and kotlinx.serialization 1.3.3 (json jvm) produces this error:
Exception in thread "main" kotlinx.serialization.SerializationException: Class 'VersionResponse' is not registered for polymorphic serialization in the scope of 'Any'.
Mark the base class as 'sealed' or register the serializer explicitly.
at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
Could you please help figuring this out?
I tried in many ways to configure polymorphic serialization, but none of them worked.vqrs
07/28/2022, 3:48 PMVampire
07/11/2022, 2:02 AMoverride val descriptor = SerialDescriptor("...", CONTEXTUAL)
for the KSerializer
.
How would I port that to 1.3.x as the function does not expect a kind anymore?cb
07/29/2022, 9:33 AMnavigate()
, therefore performance is important. Most of these Args
classes we have are @Serializable object MyScreenArgs: ArgsInterface
, and some are data classes if they contain some inner values.
We've seen that creating a serializer()
for the object
classes is extremely slow:
7,137ns benchmarkDataClassContentViewArgsToBundle
69,204ns benchmarkObjectContentViewArgsToBundle
5,301ns benchmarkDataClassBundleToContentViewArgs
67,981ns benchmarkObjectBundleToContentViewArgs
(tests using AndroidX Microbenchmark on a Google Pixel 1)
It doesn't matter if we use the reflection or non-reflection versions of serializer()
, they're both very similar in terms of time. In fact, the generated serializer is usually slightly slower to retrieve for some reason.
I've profiled our tests, and 90% of the time is spent retrieving the serializer()
, the conversion to/from JSON is around 3ms for all benchmarks. Anyone know why the object serializer could be so slow?y9san9
08/02/2022, 1:30 PMjoschi
08/02/2022, 8:40 AMKSerializer
) configurable?
Specifically I'm looking for a way to make a serializer for BigDecimal
configurable regarding the scale of the BigDecimal
.
Contextual serialization doesn't seem to be a fit because it would only work per class and not per field, right?jmfayard
08/02/2022, 4:21 PMUserProfileEntity
(sql) to UserProfile
who have the same properties
And I'm bored to write those mappers manually
In the .Net world, there is http://automapper.org/ for that
I wonder I could just leverage kotlinx.serialization
to transform UserProfileEntity
to JsonElement
and back to UserProfile
Arnab
08/03/2022, 11:36 AM@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
sealed class Principal
object AnonymousUser : Principal() {
val _empty: String? = null
}
// other implementations of Principal
fun main() {
// jacksonObjectMapper() is a function in jackson which creates an object mapper with the Kotlin module
val input = jacksonObjectMapper().writeValueAsString(AnonymousUser)
val principal = jacksonObjectMapper().readValue(input, Principal::class.java)
println(principal)
}
This gives me the following error:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "_empty" (class com.example.hellojavabin.graphql.context.Principal$AnonymousUser), not marked as ignorable (0 known properties: ])
at [Source: (String)"{"@type":"Principal$AnonymousUser","_empty":null}"; line: 1, column: 49] (through reference chain: com.example.hellojavabin.graphql.context.Principal$AnonymousUser["_empty"])
What am I missing here? I also tried casting the input string directly to AnonymousUser
but that fails with the same issue. Has anyone done something similar?Tower Guidev2
08/03/2022, 1:48 PMkotlin.Throwable
?
and while i am here (Asking fro help) how do i resolved this duplicate name issue ?andylamax
08/03/2022, 2:32 PMAyfri
08/03/2022, 5:05 PMJSONTransformingSerializer
to replace all keys with a snake_case
instead of a camelCase
that works for any data class ?zt
08/04/2022, 3:13 AMPaul Woitaschek
08/05/2022, 8:28 AMSerializer
annotation?Joaco
08/07/2022, 3:13 PMval foo = Json.decodeFromString<Project>(string)
I see that the performance drops much more than others like jackson and GSON.
instead, if I instantiate the serializer and then reuse it, the performance works much better,
val serializer = serializer(typeOf<Project>()) as KSerializer<Project>
val foo = Json.decodeFromString(serializer, string)
Is there a problem with doing it this way?
more details in https://github.com/jcaromiq/serde-benchmarkbbaldino
08/11/2022, 5:08 PMsealed class MessagePayload ...
data class PayloadOne(val foo: String) : MessagePayload ...
class Message(val msgType: MessageType, val payload: MessagePayload) { ... }
and I was hoping to get it to serialize as:
{
"msgType": "payloadOne",
"payload": {
"foo": "..."
}
}
that is: have the discriminant for the 'payload' field actually be at a layer "above", is that possible via just annotations? or would it require custom serializer/deserializer?zt
08/11/2022, 10:00 PMJsonContentPolymorphicSerializer
can I skip serializing an element? So if I dont have any serializers I can just skip it and it wont be included in the listakowal
08/15/2022, 12:23 PMdata class(val foo: String, ...)
and in one context I need field foo
to be serialized as bar
(don’t need deserialization), but in
other cases it should be de-/serialized as usual. How is it possible to achieve this?Lukasz Kalnik
08/16/2022, 11:10 AMimport kotlinx.serialization.decodeFromString
Angelo Rüggeberg
08/17/2022, 9:50 PMdata class Envelope<T> (
val response: T
)
this is the method where i need to instance the generic typed method:
override fun responseBodyConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *>? {
I cannot find any way to create a parameterized Class with type as Type
parameter. I'd need something like this:
val response : Envelope<type> = json.decodeFromString(...)
Angelo Rüggeberg
08/17/2022, 9:52 PMval typed = Types.newParameterizedType(Envelope::class.java, type)
however I'd like to avoid pulling moshi in just to be able to work around the reflection definitiondawidhyzy
08/19/2022, 9:34 AMksp
instead of kapt
? I have a module that usess ksp
only and fails after adding kapt
Christian Lutnik
08/23/2022, 6:27 AMbenkuly
08/25/2022, 7:54 AMcompileKotlinJs
task. Any idea how to get more insights? --debug
does not give more information.Rohde Fischer
08/26/2022, 10:14 AMJan Skrasek
08/26/2022, 8:01 PMJsonEncoder
? I think I should return JsonEncoder
instance (JsonTreeEncoder
?) in my beginStructure
method, but everything related to Json encoding seems to be internal.
Is there a way? Thank you.Gabriel Quéré
08/27/2022, 8:12 AM