-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Uniformly use Json format as the data format protocol of the API
- Loading branch information
1 parent
9719b2f
commit 3d40e20
Showing
15 changed files
with
145 additions
and
70 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
22 changes: 22 additions & 0 deletions
22
composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/ExchangePreKey.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,22 @@ | ||
package com.clipevery.dto.sync | ||
|
||
import com.clipevery.serializer.Base64MimeByteArraySerializer | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ExchangePreKey( | ||
@Serializable(with = Base64MimeByteArraySerializer::class) val data: ByteArray | ||
) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as ExchangePreKey | ||
|
||
return data.contentEquals(other.data) | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return data.contentHashCode() | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
composeApp/src/commonMain/kotlin/com/clipevery/serializer/Base64MimeByteArraySerializer.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,22 @@ | ||
package com.clipevery.serializer | ||
|
||
import com.clipevery.utils.base64Decode | ||
import com.clipevery.utils.base64Encode | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
object Base64MimeByteArraySerializer: KSerializer<ByteArray> { | ||
|
||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("ByteArray") {} | ||
|
||
override fun deserialize(decoder: Decoder): ByteArray { | ||
return base64Decode(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ByteArray) { | ||
encoder.encodeString(base64Encode(value)) | ||
} | ||
} |
22 changes: 16 additions & 6 deletions
22
composeApp/src/commonMain/kotlin/com/clipevery/utils/JsonUtils.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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package com.clipevery.utils | ||
|
||
import kotlinx.serialization.encodeToString | ||
import com.clipevery.serializer.Base64MimeByteArraySerializer | ||
import com.clipevery.serializer.IdentityKeySerializer | ||
import com.clipevery.serializer.PreKeyBundleSerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.serializersModuleOf | ||
import org.signal.libsignal.protocol.IdentityKey | ||
import org.signal.libsignal.protocol.state.PreKeyBundle | ||
|
||
inline fun <reified T> readJson(json: String): T { | ||
return Json.decodeFromString<T>(json) | ||
} | ||
object JsonUtils { | ||
|
||
val JSON: Json = Json { | ||
serializersModule = SerializersModule { | ||
serializersModuleOf(ByteArray::class, Base64MimeByteArraySerializer) | ||
serializersModuleOf(PreKeyBundle::class, PreKeyBundleSerializer) | ||
serializersModuleOf(IdentityKey::class, IdentityKeySerializer) | ||
} | ||
} | ||
|
||
inline fun <reified T : Any> writeJson(obj: T): String { | ||
return Json.encodeToString(obj) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
composeApp/src/desktopMain/kotlin/com/clipevery/utils/RequestUtils.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,13 @@ | ||
package com.clipevery.utils | ||
|
||
import com.clipevery.dao.sync.HostInfo | ||
import io.ktor.http.URLBuilder | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.path | ||
|
||
fun buildUrl(urlBuilder: URLBuilder, hostInfo: HostInfo, port: Int, vararg paths: String) { | ||
urlBuilder.protocol = URLProtocol.HTTP | ||
urlBuilder.port = port | ||
urlBuilder.host = hostInfo.hostAddress | ||
urlBuilder.path(*paths) | ||
} |
Oops, something went wrong.