-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function for changes encoder for formats. fix InlineEncoder…
…. add tests for InlineEncoder
- Loading branch information
1 parent
0931531
commit e5de268
Showing
12 changed files
with
257 additions
and
170 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/commonMain/kotlin/io/github/edmondantes/serialization/BinaryFormatDelegate.kt
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,42 @@ | ||
/* | ||
* Copyright (c) 2023. Ilia Loginov | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.edmondantes.serialization | ||
|
||
import io.github.edmondantes.serialization.decoding.CustomDeserializationStrategy | ||
import io.github.edmondantes.serialization.encoding.CustomSerializationStrategy | ||
import kotlinx.serialization.BinaryFormat | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
public class BinaryFormatDelegate( | ||
private val delegate: BinaryFormat, | ||
private val serializationModify: Encoder.() -> Encoder = { this }, | ||
private val deserializationModify: Decoder.() -> Decoder = { this }, | ||
) : BinaryFormat by delegate { | ||
|
||
override fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray = | ||
delegate.encodeToByteArray(CustomSerializationStrategy(serializer) { it.serializationModify() }, value) | ||
|
||
override fun <T> decodeFromByteArray(deserializer: DeserializationStrategy<T>, bytes: ByteArray): T = | ||
delegate.decodeFromByteArray(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, bytes) | ||
} | ||
|
||
public fun BinaryFormat.modifySerializer(block: Encoder.() -> Encoder): BinaryFormat = | ||
BinaryFormatDelegate(this, serializationModify = block) | ||
|
||
public fun BinaryFormat.modifyDeserializer(block: Decoder.() -> Decoder): BinaryFormat = | ||
BinaryFormatDelegate(this, deserializationModify = block) |
42 changes: 42 additions & 0 deletions
42
src/commonMain/kotlin/io/github/edmondantes/serialization/StringFormatDelegate.kt
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,42 @@ | ||
/* | ||
* Copyright (c) 2023. Ilia Loginov | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.edmondantes.serialization | ||
|
||
import io.github.edmondantes.serialization.decoding.CustomDeserializationStrategy | ||
import io.github.edmondantes.serialization.encoding.CustomSerializationStrategy | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.StringFormat | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
public class StringFormatDelegate( | ||
private val delegate: StringFormat, | ||
private val serializationModify: Encoder.() -> Encoder = { this }, | ||
private val deserializationModify: Decoder.() -> Decoder = { this }, | ||
) : StringFormat by delegate { | ||
|
||
override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String = | ||
delegate.encodeToString(CustomSerializationStrategy(serializer) { it.serializationModify() }, value) | ||
|
||
override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T = | ||
delegate.decodeFromString(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, string) | ||
} | ||
|
||
public fun StringFormat.modifySerializer(block: Encoder.() -> Encoder): StringFormat = | ||
StringFormatDelegate(this, serializationModify = block) | ||
|
||
public fun StringFormat.modifyDeserializer(block: Decoder.() -> Decoder): StringFormat = | ||
StringFormatDelegate(this, deserializationModify = block) |
27 changes: 27 additions & 0 deletions
27
...Main/kotlin/io/github/edmondantes/serialization/decoding/CustomDeserializationStrategy.kt
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,27 @@ | ||
/* | ||
* Copyright (c) 2023. Ilia Loginov | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.edmondantes.serialization.decoding | ||
|
||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.encoding.Decoder | ||
|
||
public class CustomDeserializationStrategy<T>( | ||
private val delegate: DeserializationStrategy<T>, | ||
private val decoderTransformer: (Decoder) -> Decoder, | ||
) : DeserializationStrategy<T> by delegate { | ||
|
||
override fun deserialize(decoder: Decoder): T = | ||
delegate.deserialize(decoderTransformer(decoder)) | ||
} |
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
Oops, something went wrong.