-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for kotlin serialization (#224)
- Loading branch information
1 parent
fc4d9d0
commit 1b26150
Showing
7 changed files
with
121 additions
and
11 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
36 changes: 36 additions & 0 deletions
36
sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/KtSerdes.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,36 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.kotlin | ||
|
||
import dev.restate.sdk.common.Serde | ||
import java.nio.charset.StandardCharsets | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
|
||
object KtSerdes { | ||
|
||
/** Creates a [Serde] implementation using the `kotlinx.serialization` json module. */ | ||
inline fun <reified T> json(): Serde<T> { | ||
return json(serializer()) | ||
} | ||
|
||
/** Creates a [Serde] implementation using the `kotlinx.serialization` json module. */ | ||
fun <T> json(serializer: KSerializer<T>): Serde<T> { | ||
return object : Serde<T> { | ||
override fun serialize(value: T?): ByteArray { | ||
return Json.encodeToString(serializer, value!!).encodeToByteArray() | ||
} | ||
|
||
override fun deserialize(value: ByteArray?): T { | ||
return Json.decodeFromString(serializer, String(value!!, StandardCharsets.UTF_8)) | ||
} | ||
} | ||
} | ||
} |
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