1+ { {#multiplatform} }
2+ import kotlinx.serialization.*
3+ import kotlinx.serialization.descriptors.*
4+ import kotlinx.serialization.encoding.*
5+ import kotlinx.serialization.json.*
6+
7+ /**
8+ * { {{description} }}
9+ *
10+ */
11+ { {#isDeprecated} }
12+ @Deprecated(message = "This schema is deprecated.")
13+ { {/isDeprecated} }
14+ @Serializable(with = { {classname} }.{ {classname} }Serializer::class)
15+ { {#nonPublicApi} }internal { {/nonPublicApi} }{ {^nonPublicApi} }{ {#explicitApi} }public { {/explicitApi} }{ {/nonPublicApi} }data class { {classname} }(var actualInstance: Any? = null) {
16+
17+ object {{classname} }Serializer : KSerializer<{ {classname} }> {
18+ override val descriptor: SerialDescriptor = buildClassSerialDescriptor(" {{classname}}" ) {
19+ element(" type" , JsonPrimitive.serializer().descriptor)
20+ element(" actualInstance" , JsonElement.serializer().descriptor)
21+ }
22+
23+ override fun serialize(encoder: Encoder, value: { {classname} }) {
24+ val jsonEncoder = encoder as? JsonEncoder ?: throw SerializationException(" {{classname}} can only be serialized with Json" )
25+
26+ when (val instance = value.actualInstance) {
27+ {{#composedSchemas} }
28+ { {#anyOf} }
29+ { {#isPrimitiveType} }
30+ { {#isString} }
31+ is kotlin.String -> jsonEncoder.encodeString(instance)
32+ { {/isString} }
33+ { {#isBoolean} }
34+ is kotlin.Boolean -> jsonEncoder.encodeBoolean(instance)
35+ { {/isBoolean} }
36+ { {#isInteger} }
37+ is kotlin.Int -> jsonEncoder.encodeInt(instance)
38+ { {/isInteger} }
39+ { {#isNumber} }
40+ { {#isDouble} }
41+ is kotlin.Double -> jsonEncoder.encodeDouble(instance)
42+ { {/isDouble} }
43+ { {#isFloat} }
44+ is kotlin.Float -> jsonEncoder.encodeFloat(instance)
45+ { {/isFloat} }
46+ { {/isNumber} }
47+ { {/isPrimitiveType} }
48+ { {^isPrimitiveType} }
49+ is { {{dataType} }} -> jsonEncoder.encodeSerializableValue({ {{dataType} }}.serializer(), instance)
50+ { {/isPrimitiveType} }
51+ { {/anyOf} }
52+ { {/composedSchemas} }
53+ null -> jsonEncoder.encodeJsonElement(JsonNull)
54+ else -> throw SerializationException("Unknown type in actualInstance: ${ instance::class} ")
55+ }
56+ }
57+
58+ override fun deserialize(decoder: Decoder): { {classname} } {
59+ val jsonDecoder = decoder as? JsonDecoder ?: throw SerializationException(" {{classname}} can only be deserialized with Json" )
60+ val jsonElement = jsonDecoder.decodeJsonElement()
61+
62+ val errorMessages = mutableListOf< String> ()
63+
64+ {{#composedSchemas} }
65+ { {#anyOf} }
66+ try {
67+ val instance = jsonDecoder.json.decodeFromJsonElement< {{{dataType} }}>(jsonElement)
68+ return { {classname} }(actualInstance = instance)
69+ } catch (e: Exception) {
70+ errorMessages.add(" Failed to deserialize as {{{dataType}}}: ${e.message}" )
71+ }
72+ { {/anyOf} }
73+ { {/composedSchemas} }
74+
75+ throw SerializationException("Cannot deserialize { {classname} }. Tried: ${ errorMessages.joinToString(" , " )} ")
76+ }
77+ }
78+ }
79+ { {/multiplatform} }
0 commit comments