A Fish’s state is frequently converted to JSON and back by the Pond, internally. Hence, transforming the state to a JSON-string and back must result in an object identical to the original state.
So when you are using something as your state that can not be trivially JSON-stringified, you need
deserializeState. The process is the following:
When transforming to JSON, the Pond just calls
JSON.stringify(state). Objects that don’t serialize directly to JSON must implement toJSON.
When transforming back from JSON, the Pond calls
deserializeState(JSON.parse(jsonString)). So the input to
deserializeStateis the object returned by
deserializeStatemust convert that back into the proper state type.
An Example with immutable-js
immutable-js is a great library for performant
object as defined by immutable-js:
Since the data structures provided by immutable-js already implement
toJSON, the serialization
works fine: The
List becomes a plain JS array. (If you use your own data structures or classes
that cannot be serialized, you have to implement
Now when the state is parsed, it is still an array and not a
List. Hence we implement