-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5be8c7a
commit 156c014
Showing
12 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.benwoodworth.knbt.test | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import net.benwoodworth.knbt.NbtName | ||
|
||
private class WithNbtNameSerializer<T>( // TODO Add in previous commit | ||
val serializer: KSerializer<T>, | ||
nbtName: String | ||
) : KSerializer<T> { | ||
override val descriptor: SerialDescriptor = | ||
serializer.descriptor.withNbtName(nbtName) | ||
|
||
override fun serialize(encoder: Encoder, value: T): Unit = | ||
encoder.encodeSerializableValue(serializer, value) | ||
|
||
override fun deserialize(decoder: Decoder): T = | ||
decoder.decodeSerializableValue(serializer) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private data class WithNbtNameSerialDescriptor( | ||
val serialDescriptor: SerialDescriptor, | ||
val nbtName: String, | ||
) : SerialDescriptor by serialDescriptor { | ||
override val serialName: String = "WithNbtName<${serialDescriptor.serialName}>($nbtName)" | ||
|
||
override val annotations: List<Annotation> = | ||
serialDescriptor.annotations | ||
.filter { it !is NbtName } | ||
.plus(NbtName(nbtName)) | ||
} | ||
|
||
fun <T> KSerializer<T>.withNbtName(nbtName: String): KSerializer<T> = | ||
WithNbtNameSerializer(this, nbtName) | ||
|
||
fun SerialDescriptor.withNbtName(nbtName: String): SerialDescriptor = | ||
WithNbtNameSerialDescriptor(this, nbtName) |