Skip to content

Commit b81e9db

Browse files
committed
[docs] Improve serialisation doc
1 parent 5d43c4f commit b81e9db

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

docs/pages/persistence.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ title: Persistence
44
---
55

66
# Persistence
7+
78
{: .no_toc }
89

910
## Table of contents
11+
1012
{: .no_toc .text-delta }
1113

1214
- TOC
13-
{:toc}
15+
{:toc}
1416

1517
* **Persist** `StateMachine` - means transform it into serializable representation, such as `Serializable` object or
16-
JSON text, and possibly saving it into some persistent storage like file or sending by network.
18+
JSON text, and possibly saving it into some persistent storage like a file or sending by a network.
1719
* **Restoration** - is a process of restoring the `StateMachine` from the serializable representation.
1820

1921
There are several kinds or levels of `StateMachine` persistence (serialization). Let's look at sample use cases:
@@ -48,31 +50,58 @@ val machine = createStateMachine(
4850
}
4951
```
5052

51-
When the machine had processed necessary events, and you want to save its state configuration, first you have to
52-
get the recorded events:
53+
When the machine had processed your business logic events, and you want to save its state configuration, first you have
54+
to get the recorded events:
5355

5456
```kotlin
5557
val recordedEvents = machine.eventRecorder.getRecordedEvents()
5658
```
5759

58-
`RecordedEvents` object now is ready to be serialized. The library provides an implementation
60+
`RecordedEvents` object now is ready to be serialized. The library provides an implementation
5961
of serialization process using `kotlinx.serialization` library starting from `KStateMachine` version `v0.32.0`.
60-
Alternatively you can use some other serialization library to serialize `RecordedEvents` object by your own.
62+
63+
Initialize serialization format (JSON for instance):
64+
65+
```kotlin
66+
val jsonFormat = Json {
67+
// use special, library provided SerializersModule for RecordedEvents and its internals
68+
// from kstatemachine-serialization artifact
69+
serializersModule = KStateMachineSerializersModule + SerializersModule { /* ... */ }
70+
}
71+
```
72+
73+
And encode (serialize) `RecordedEvents` object:
74+
75+
```kotlin
76+
val recordedEventsJson = jsonFormat.encodeToString(recordedEvents)
77+
```
78+
79+
### Custom serialization library
80+
81+
Alternatively you can use some other serialization library to serialize `RecordedEvents` class by your own
82+
(you will also need to serialize `SerializableGeneratedEvent` class for internal events generated by the library
83+
itself).
6184

6285
## Restoring StateMachine
6386

6487
When a user wants to restore the StateMachine, he deserializes `RecordedEvents` object and
65-
creates StateMachine instance having exactly the same structure as original one.
88+
creates StateMachine instance having exactly the same structure as original one.
6689
Typically, both instances are created by the same code.
6790

91+
```kotlin
92+
val restoredRecordedEvents = jsonFormat.decodeFromString<RecordedEvents>(recordedEventsJson)
93+
```
94+
6895
Calling `restoreByRecordedEvents()` or its blocking analog `restoreByRecordedEventsBlocking()` will process
6996
recorded events over just created StateMachine instance.
7097

7198
```kotlin
72-
machine.restoreByRecordedEvents(recordedEvents)
99+
machine2.restoreByRecordedEvents(restoredRecordedEvents)
73100
```
74101

75102
`restoreByRecordedEvents()` method will start the machine if necessary.
76103
You can configure restoration process by `restoreByRecordedEvents()` arguments.
104+
The machine should not process any events before its restoration (in such case exception will be thrown) as
105+
it can possibly lead to incorrect restoration result.
77106

78107
See [Event recording sample](https://github.com/KStateMachine/kstatemachine/tree/master/samples/src/commonMain/kotlin/ru/nsk/samples/SerializationEventRecordingSample.kt)

kstatemachine-serialization/src/commonMain/kotlin/ru/nsk/kstatemachine/serialization/persistence/RecordedEventsSerializer.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,10 @@ val KStateMachineSerializersModule = SerializersModule {
3131
polymorphic(Event::class) {
3232
subclass(SerializableGeneratedEvent::class, SerializableGeneratedEventSerializer)
3333
}
34-
35-
contextual(SerializableGeneratedEventEventTypeStartSerializer)
3634
polymorphic(EventType::class) {
3735
subclass(EventType.Start::class, SerializableGeneratedEventEventTypeStartSerializer)
3836
subclass(EventType.Stop::class, SerializableGeneratedEventEventTypeStopSerializer)
39-
subclass(
40-
EventType.Destroy::class,
41-
SerializableGeneratedEventEventTypeDestroySerializer
42-
)
37+
subclass(EventType.Destroy::class, SerializableGeneratedEventEventTypeDestroySerializer)
4338
}
4439
}
4540

samples/src/commonMain/kotlin/ru/nsk/samples/SerializationEventRecordingSample.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ private suspend fun CoroutineScope.restoreStep(jsonFormat: Json, recordedEventsJ
8080
*/
8181
fun main(): Unit = runBlocking {
8282
val jsonFormat = Json {
83-
ignoreUnknownKeys = true
8483
// use special, library provided SerializersModule for RecordedEvents and its internals
8584
// from kstatemachine-serialization artifact
8685
serializersModule = KStateMachineSerializersModule + SerializersModule {

0 commit comments

Comments
 (0)