Serialization transforms structured values into representations suitable for storage or transmission, then reconstructs them when needed. It provides a boundary between typed domain data and external formats such as database bytes or service-specific records. section 10.1.4 section 11.2.4
A practical design keeps domain code typed while concentrating representation-specific work behind conversion functions. This limits coupling, but incorrect mappings can still introduce bugs; smart constructors and explicit conversion contracts reduce that risk. section 10.1.4 section 11.2.4
Common serialization boundaries include:
- Map structured logger attributes into a blank Sentry record, parsing fields such as event identifiers and timestamps before registration. section 10.1.4
- Convert database keys and values to ByteString, then parse stored values back through a type-class contract. section 11.2.4
- Separate database representations from domain values through AsValueEntity conversions during entity loading. section 11.2.4
Loading a serialized database entity
A generic loader converts a typed key with toDBKey, retrieves raw bytes, applies fromDBValue, reports decoding failure when parsing returns Nothing, and converts the result into a domain value. section 11.2.4
Listing 11.11 Generic loadEntity function
loadEntity
:: DBEntity db entity #1
=> AsValueEntity entity dst #2
=> KeyEntity entity #3
-> KVDBL db (DBResult dst) #4
loadEntity key = do
eRawVal <- load (toDBKey key) #5
let dst = case eRawVal of
Left err -> Left err
Right rawVal -> case fromDBValue rawVal of #6
Nothing -> Left DecodingFailed
Just val -> Right (fromValueEntity key val of) #7
pure dst
Follow the generic loadEntity pipeline from database-key conversion through decoding and domain-value reconstruction. section 11.2.4























