This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
-
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.
Merge pull request #59 from boswelja/serialization-improvements
Serialization improvements
- Loading branch information
Showing
32 changed files
with
407 additions
and
194 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
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,64 @@ | ||
# serializers | ||
|
||
This module provides some basic serialization support for all supported hosts. | ||
|
||
## Data Serialization | ||
|
||
### Creating a serializer | ||
|
||
To create your own serializer, simply extend `MessageSerializer` | ||
|
||
```kotlin | ||
object MySerializer : MessageSerializer<MyData>( | ||
messagePaths = setOf( | ||
"path-1", | ||
"path-2" | ||
) | ||
) { | ||
override suspend fun serialize(data: MyData): ByteArray { | ||
// Serialize MyData to ByteArray | ||
} | ||
|
||
override suspend fun deserialize(bytes: ByteArray?): MyData { | ||
// Deserialize ByteArray to MyData | ||
} | ||
} | ||
``` | ||
|
||
The list of message paths specified will be used to filter incoming messages for the serializer to handle. | ||
|
||
### Sending and receiving typed data | ||
|
||
Typed messages are managed by a [MessageHandler](https://github.com/boswelja/WatchConnectionLib/blob/main/serialization/src/commonMain/kotlin/com/boswelja/watchconnection/serialization/MessageHandler.kt). | ||
`MessageHandler` accepts a base `MessageClient`, as well as a single `MessageSerializer`. | ||
|
||
```kotlin | ||
val messageHandler = MessageHandler<MyData>( | ||
MySerializer, | ||
messageClient | ||
) | ||
``` | ||
|
||
#### Sending messages | ||
|
||
Call `messageHandler.send` and provide the target device UID, along with a `Message` with a data type matching your serializer data type. | ||
|
||
```kotlin | ||
messageHandler.send( | ||
device.uid, | ||
Message( | ||
"path-1", | ||
MyData() | ||
) | ||
) | ||
``` | ||
|
||
#### Receiving messages | ||
|
||
Messages are received from a `Flow`. Collecting from the Flow automatically filters and deserializes received messages. | ||
|
||
```kotlin | ||
messageHandler.incomingMessages().collect { message -> | ||
// message.data is MyData | ||
} | ||
``` |
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 @@ | ||
<manifest package="com.boswelja.watchconnection.serialization" /> |
Oops, something went wrong.