Skip to content

Commit bb9f94e

Browse files
committed
Add a complete example with a lot of element types (value classes, primitives, sub-structures and sub-lists)
1 parent 9407faf commit bb9f94e

File tree

3 files changed

+335
-56
lines changed

3 files changed

+335
-56
lines changed

core/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ kotlin {
3737
Require-Kotlin-Version is used to determine whether runtime library with new features can work with old compilers.
3838
In ideal case, its value should always be 1.4, but some refactorings (e.g. adding a method to the Encoder interface)
3939
may unexpectedly break old compilers, so it is left out as a safety net. Compiler plugins, starting from 1.4 are instructed
40-
to reject runtime if runtime's Require-Kotlin-Version is greater then the current compiler.
40+
to reject runtime if runtime's Require-Kotlin-Version is greater than the current compiler.
4141
*/
4242
tasks.withType<Jar>().named(kotlin.jvm().artifactsTaskName) {
4343

core/commonMain/src/kotlinx/serialization/encoding/ReorderingCompositeEncoder.kt

+6-15
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import kotlinx.serialization.modules.*
2222
* @param mapElementIndex maps the element index to a new positional zero-based index. If this mapper provides the same index for multiple elements, only the last one will be encoded as the previous ones will be overridden. The mapped index just helps to reorder the elements, but the reordered `encode*Element` method calls will still pass the original element index.
2323
*/
2424
@ExperimentalSerializationApi
25-
internal class ReorderingCompositeEncoder(
25+
public class ReorderingCompositeEncoder(
2626
structureDescriptor: SerialDescriptor,
2727
private val compositeEncoderDelegate: CompositeEncoder,
2828
private val mapElementIndex: (SerialDescriptor, Int) -> Int,
2929
) : CompositeEncoder {
30-
private var bufferedCalls = Array<BufferedCall?>(structureDescriptor.elementsCount) { null }
30+
// TODO we should use a map to have buffered calls per descriptor if the same encoder is reused for different structures
31+
// or we need to ensure that this encoder is not reused
32+
private val bufferedCalls = Array<BufferedCall?>(structureDescriptor.elementsCount) { null }
3133

3234
override val serializersModule: SerializersModule
3335
// No need to return a serializers module as it's not used during buffering
@@ -48,6 +50,7 @@ internal class ReorderingCompositeEncoder(
4850

4951
override fun endStructure(descriptor: SerialDescriptor) {
5052
encodeBufferedFields(descriptor)
53+
// TODO should we clear the buffer after encoding? Is it possible to reuse this encoder?
5154
}
5255

5356
private fun encodeBufferedFields(descriptor: SerialDescriptor) {
@@ -228,19 +231,7 @@ internal class ReorderingCompositeEncoder(
228231

229232
private fun invalidCall(methodName: String): Nothing {
230233
// This method is normally called by encodeSerializableValue or encodeNullableSerializableValue that is buffered, so we should never go here during buffering as it will be delegated to the concrete CompositeEncoder
231-
throw UnsupportedOperationException("$methodName should not be called when reordering fields, as it should be done by encodeSerializableValue or encodeNullableSerializableValue")
234+
throw UnsupportedOperationException("encodeSerializableValue or encodeNullableSerializableValue should be called before $methodName")
232235
}
233236
}
234237
}
235-
236-
/**
237-
* Encodes the [structureDescriptor] elements in a specific order provided by [elementIndexMapper].
238-
*
239-
* @param structureDescriptor descriptor of the structure being encoded and reordered
240-
* @param elementIndexMapper maps the element index to a new positional zero-based index. If this mapper provides the same index for multiple elements, only the last one will be encoded as the previous ones will be overridden. The mapped index just helps to reorder the elements, but the reordered `encode*Element` method calls will still pass the original element index.
241-
*/
242-
@ExperimentalSerializationApi
243-
public fun CompositeEncoder.encodeReorderingElements(
244-
structureDescriptor: SerialDescriptor,
245-
elementIndexMapper: (SerialDescriptor, Int) -> Int,
246-
): CompositeEncoder = ReorderingCompositeEncoder(structureDescriptor, this, elementIndexMapper)

0 commit comments

Comments
 (0)