tylerwilson
03/12/2020, 2:56 PMShan
03/13/2020, 6:55 PMJson(context = serializationContext)
where my serializationContext
is a SerializersModule
which contains all of my contextual serializer mappings, to stringify/parse values, but need it to able to adapt to situations where the data model has changed server-side for older app versions that don't have an updated model. I could use Json.nonstrict
for parsing/writing, but is there any way to use it with my serializationContext
and have it fall back to nonstrict
if the models don't match? Or is there a better way to do this that I'm missing?altavir
03/14/2020, 4:37 PMdata class NodeItem<M : Meta>(val node: M) : MetaItem<M>() {
override fun toString(): String = node.toString()
@Serializer(NodeItem::class)
companion object : KSerializer<NodeItem<out Meta>> {
...
}
}
M
is the recursive generics type. It is actually ignored in the serializer since we always use top type.
When I use it, I get class hep.dataforge.meta.MetaBuilder is not registered for polymorphic serialization in the scope of class hep.dataforge.meta.Meta
. Is it possible to somehow bypass polymorphic serializers and actually use interface type for serialization?Paulius Ruminas
03/16/2020, 1:57 PMkotlinx.serialization.internal.CommonEnumSerializer
to serialize an enum:
object DayTypeSerializer : CommonEnumSerializer<DayType>(
"DayType",
DayType.values(),
DayType.values().map { it.type.toString() }.toTypedArray()
)
@Serializable(with = DayTypeSerializer::class)
enum class Day(val type: Int) {
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
SUNDAY(7),
HOLIDAY(8),
}
After 0.20.0 update CommonEnumSerializer
is removed. Is there another way to achieve this?napperley
03/16/2020, 11:59 PMLuoqiaoyou
03/17/2020, 3:49 AMEdward Taylor
03/22/2020, 11:39 PM{
fooProp1,
fooProp2,
barProp1,
barProp2
}
And I'm structuring more OO in Kotlin, such as:
data class MyThing(val prop1: String, val prop2: String)
data class Combined(val foo: MyThing, val bar: MyThing)
The data structures have a lot more repetition than shown in this example so it would be great to find something where I can take the automatically generated serializers and simply tell the combined deserializer to deserialize MyThing by prepending the name of its property name. This would probably be done in the Combined serializer, maybe giving it a modified decoder instance or something?
We're wanting to target K/N in the future so want to avoid reflection, currently I'm using reflection in a custom Serializer class which iterates over the reflected object properties and uses those to figure out the keys.
Any direction would be helpful, I just can't seem to figure out this usecase of wanting to extend the generated serializers, documentation or an article would help too.dagomni
03/23/2020, 5:15 PMjava.lang.AssertionError: org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationResolveExtension caused LinkageError Caused by: java.lang.NoSuchMethodError: org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl.createWithDefaultBound(Lorg/jetbrains/kotlin/descriptors/DeclarationDescriptor;Lorg/jetbrains/kotlin/descriptors/annotations/Annotations;ZLorg/jetbrains/kotlin/types/Variance;Lorg/jetbrains/kotlin/name/Name;ILorg/jetbrains/kotlin/storage/StorageManager;)
I'm also using Compose and its plugin, I'm mentioning it just in case it has something to do with it because I've read here and there that it could be incompatible in some cases but I don't know to what extent.
Environment
• Kotlin version: 1.3.61/1.3.70
• Library version: 0.14.0/0.20.0
• Kotlin platforms: JVM/Android (Multiplatform Project)
• Gradle version: 6.2.1, AGP 4.1.0-alpha02
• IDE version: Android Studio 4.1.0-alpha02Paul Woitaschek
03/23/2020, 5:27 PMcoletz
03/23/2020, 9:11 PMInvalid record (Producer: 'LLVM8.0.0svn' Reader: 'LLVM APPLE_1_1100.0.33.16_0')
. Seems like this is caused by having an inner class with Serializable annotation. My actual structure is something like a serializable data class, with an inner class which is serializable itself (and is used by the data class itself)madhead
03/24/2020, 11:59 AMRequest {
id: int | string,
name: string,
// other fields
}
How do I do that with kotlinx.serialization? I only need to serialize my classes to JSON, no deserialization.thana
03/24/2020, 2:23 PMMap<Strin, Any>
but im struggeling how to write the descriptor for it. How do i tell the framework, that an arbitray length sequence of encodeString/encodeSerializableElement
calls will follow?Christian Sousa
03/26/2020, 11:50 AMAny
type?
I’m building a data class that has a field that can either come as a String
or List<String>
, and I don’t have any control over that. But when I need to serialize it, it will break because of typing issues.
Has anyone ever faced this issue and if yes, what’s a possible solution? (edited)gabin
03/27/2020, 11:16 AM@Serializable
data class MyUser(
val fullName: String,
val middleName: String? = null,
val age: Int = 42
)
I want to achieve the result when following object MyUser("John Doe")
will lead to json:
{"fullName": "John Doe", age=42}
From what I understand Json(encodeDefaults = false)
will disable adding default age. Please give an adviceSrSouza
03/30/2020, 3:12 PM@Serializable
data class IBeacon(
override val uuid: String,
override val major: Short,
override val minor: Short
) : IBeaconFilter
interface IBeaconFilter {
val uuid: String
val major: Short?
val minor: Short?
}
Jakub Pi
03/30/2020, 4:39 PM@Serializer(forClass = Map::class)
class MapWithLazyValue<K,V>(private val keySerializer : KSerializer<K>, private val valueSerializer : KSerializer<V>) : KSerializer<Map<K, Lazy<V>>> {
override val descriptor: SerialDescriptor = PrimitiveDescriptor("map", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Map<K, Lazy<V>> {
return decoder.decodeSerializableValue(MapSerializer(keySerializer, valueSerializer)).mapValues { lazy { it.value } }
}
override fun serialize(encoder: Encoder, value: Map<K, Lazy<V>>) {
encoder.encodeSerializableValue(MapSerializer(keySerializer, valueSerializer),
value.filterValues { it.isInitialized() }.mapValues { it.value.value })
}
}
However, when I try to build my project, I get:
D:\IdeaProject\build\tmp\kapt3\stubs\main\com\collection\ArchiveHashCache.java:13: error: incompatible types: Class<MapWithLazyValue> cannot be converted to Class<? extends KSerializer<?>>
@kotlinx.serialization.Serializable(with = com.collection.MapWithLazyValue.class)
[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: arrow.core.extensions.ExtensionProcessor (NON_INCREMENTAL), arrow.fold.AutoFoldProcessor (NON_INCREMENTAL), arrow.generic.CoproductProcessor (NON_INCREMENTAL), arrow.generic.ProductProcessor (NON_INCREMENTAL), arrow.higherkinds.HigherKindsProcessor (NON_INCREMENTAL), arrow.optics.OpticsProcessor (NON_INCREMENTAL).
I realize this may be a bug in kapt/arrow, but have I taken the right approach to writing my custom serializer or have I done something wrong?Ahmed Mourad
04/02/2020, 4:17 PMmelatonina
04/03/2020, 8:41 AMkotlinx_serialization_runtime_version=0.14.0
In both cases the serialization plugin is selected with classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
in the top level build.gradle file where kotlin_version=1.3.71
This is the build.gradle of the module that contains the class above: https://gist.github.com/mel4tonin4/9bf05d82f8901613b131998162250495
What's the reason for this difference?crummy
04/04/2020, 9:52 PM{
"values": [
[
"Plums",
5
],
[
"Apples"
4
]
]
}
If the quantities were strings I'd do values: List<List<String>>
but this fails with Expected string literal
. Can I do this?Kiryushin Andrey
04/07/2020, 11:18 AMIvan Brko
04/08/2020, 1:37 PMdata class Data(val a: String, val b: String)
Now, I know I could add @Serializable and have everything generated for me. But what if I can't annotate this class, it is not in my code? Do i need to write the serialization manually or is there a way to automatically generate KSerializer for it?fkrauthan
04/08/2020, 4:55 PMSam Garfinkel
04/08/2020, 9:34 PM@Serializable(with=Foo::class)
where Foo
is initialized at runtime work? Or do I need to use a ContextualSerializer in this case?Sean Keane
04/09/2020, 1:49 PMkotlinx.serialization.internal.CommonEnumSerializer
but it doesnt seem to be available anymore? Am I missing something?Sam Garfinkel
04/09/2020, 2:58 PM@Serializable(with=SomeSerializer::class)
still cause a default serializer to be generated?diego-gomez-olvera
04/10/2020, 8:36 AM@Serializable
entitiesIvan Brko
04/10/2020, 10:31 AMdata class User(
val username: String,
val email: String,
val firstName: String,
val lastName: String,
val password: String,
val role: Role,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime
)
So, in my file I create a serializer for it like this:
@Serializer(forClass = User::class)
object UserSerializer {}
Now, everything seems to work OK when deserializing the data, however when KMongo tries to serialize it and send it to DB, I get a nasty java exception stack with the following message:
java.lang.IndexOutOfBoundsException: com.example.domain.users.User descriptor has only 8 elements, index: 0
After going through code, I found that in file PluginGeneratedSerialDescriptor.kt there is an overriden fun getElementDescriptor in which property generatedSerializer is trying to get accessed, but it is null and this causes the exception.
Does anyone know if this is a bug, or I'm doing something wrong here?Lamberto Basti
04/10/2020, 2:40 PM@Serializable
data class PagedData<T>(val data: T, val page: Int, val totalPages: Int, val pageSize: Int)
Where you know for sure that T
is a class that has the annotations @Serializable
or a list of a type that is annotated with @Serializable
.
Why the serialization runtime is not able to infer what to use? Also, I am using Ktor, it would be kind of pointless to explicitly build a serializer before every response as such.altavir
04/10/2020, 5:48 PMaltavir
04/11/2020, 12:05 PMaltavir
04/11/2020, 12:05 PMDominaezzz
04/11/2020, 3:18 PMMapSerializer(String.serializer(), PolymorphicSerializer(YourClass::class))
?altavir
04/11/2020, 4:54 PM