Anarakul
01/29/2021, 5:04 PMenum class
. I have read the appropriate documents (I think) and am using the appropriate annotations and strategies (I think), but I am still encountering a run-time issue immediately. I am also encountering a compile-time issue, I think, because the compiler doesn’t believe that there’s a serializer
method on my enum class
.Anarakul
01/29/2021, 5:05 PM1.4.21
for everything I can, and 1.0-M1-1.4.0-rc
for the run-time.Anarakul
01/29/2021, 5:06 PMAnarakul
01/29/2021, 5:06 PM@Serializable(with = TransformationRuleSerializer::class)
enum class TransformationRule(val identifier: String)
Anarakul
01/29/2021, 5:07 PM@Serializer(forClass = TransformationRule::class)
object TransformationRuleSerializer
{
override val descriptor
get() = PrimitiveSerialDescriptor(
"TransformationRuleSerializer",
PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): TransformationRule
{
val serializedName = decoder.decodeString()
return TransformationRule.values().find {
it.identifier == serializedName
}!!
}
override fun serialize(encoder: Encoder, value: TransformationRule) =
encoder.encodeString(value.identifier)
}
Anarakul
01/29/2021, 5:08 PMCaused by: kotlinx.serialization.SerializationException: Serializer for class 'TransformationRule' is not found. Mark the class as @Serializable or provide the serializer explicitly.
Anarakul
01/29/2021, 5:08 PMAnarakul
01/29/2021, 5:10 PMAnarakul
01/29/2021, 5:10 PMAnarakul
01/29/2021, 5:16 PMAnarakul
01/29/2021, 5:17 PMenum
is serialized as a property of an enclosing data class
, so I thought that this open issue might be relevant: https://github.com/Kotlin/kotlinx.serialization/issues/487Anarakul
01/29/2021, 5:18 PMAnarakul
01/29/2021, 5:19 PM/**
* A transformation encapsulates a rule and its static operands, and provides a
* simple API for applying the transformation to a [JSON element][JsonElement].
*/
@Serializable(with = TransformationSerializer::class)
data class Transformation(
val rule: TransformationRule,
val operands: List<String>
)
{
/**
* Transform the specified [JSON element][JsonElement] by applying the
* encapsulated [transformation rule][TransformationRule]. If the supplied
* JSON element does not satisfy the preconditions of the rule, then it will
* not be transformed.
*
* @param source
* The JSON element, which is expected to satisfy the prerequisites of the
* rule.
* @return
* The result of the transformation. `null` indicates that the object is
* being elided.
*/
fun transform(source: JsonElement): JsonElement? =
rule.transform(operands, source)
}
Anarakul
01/29/2021, 5:20 PM@Serializer(forClass = Transformation::class)
object TransformationSerializer
{
override val descriptor =
buildClassSerialDescriptor("TransformationSerializer") {
element<TransformationRule>("rule")
element("operands", listSerialDescriptor<String>())
}
override fun deserialize(decoder: Decoder): Transformation
{
return try
{
val ruleName = decoder.decodeString()
Transformation(
TransformationRule.values().find {
it.identifier == ruleName
}!!,
emptyList())
}
catch (e: SerializationException)
{
decoder.decodeStructure(descriptor) {
var rule: TransformationRule? = null
var operands: List<String>? = null
while (true)
{
when (val index = decodeElementIndex(descriptor))
{
0 -> rule = decodeSerializableElement(
descriptor,
index,
TransformationRuleSerializer)
1 -> operands = decodeSerializableElement(
descriptor,
index,
ListSerializer(String.serializer()))
DECODE_DONE -> break
}
}
Transformation(rule!!, operands!!)
}
}
}
override fun serialize(encoder: Encoder, value: Transformation)
{
if (value.rule.arity == 0 || value.operands.isEmpty())
{
encoder.encodeString(value.rule.identifier)
}
else
{
encoder.encodeStructure(descriptor) {
encodeSerializableElement(
descriptor,
0,
TransformationRuleSerializer,
value.rule)
encodeSerializableElement(
descriptor,
1,
ListSerializer(String.serializer()),
value.operands)
}
}
}
}
Anarakul
01/29/2021, 5:22 PMAnarakul
01/29/2021, 5:23 PM"hash"
Or:
{
name: "salt",
arguments: ["BDF0E4EA-5C5F-4FF1-822D-64CDF6FC0D15"]
}
Anarakul
01/29/2021, 5:24 PMenum class TransformationRule
with its serializer (TransformationRuleSerializer
).Anarakul
01/29/2021, 5:27 PMAnarakul
01/29/2021, 5:27 PMrnett
01/29/2021, 11:49 PM1.0.1
). There probably should be better errors if that mismatches, but the runtime is for Kotlin and plugin version 1.4.0-RC
. If that doesn't help, try without custom serializers and see if it works. Everything looks correct to me though. I think Enums serialize to their names by default in the new version, too, so you probably don't need a custom serializer there.Anarakul
01/31/2021, 11:32 PM1.0.1
in Gradle (the URL was eluding me), but that seems to have been the problem. Now it’s warning me that I’m using tons of experimental APIs, and it still crashes on startup, but with an error in my own code. Fingers crossed, but I should be able to trace it from here.Anarakul
01/31/2021, 11:33 PMAnarakul
02/01/2021, 9:05 PM