liveBook · Manning

Serialization

As explained in Functional Design and Architecture: Examples in Haskell

Serialization is the transformation of structured data into a representation suitable for storage or transmission and later reconstruction.

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

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Descends to field parsing, type-class contracts, decoding failures, and domain reconstruction, but does not develop serialization algorithms internally.
Examples
Uses Sentry records, parsed timestamps, Meteor entities, database keys, encoded values, and generic loadEntity as concrete examples.
What it assumes
Assumes familiarity with typed data, algebraic data types, pattern matching, and type classes while teaching representation boundaries through examples.
What you can do after
Apply it via conversion functions and type-class contracts to persist entities or populate service-specific records.
How it connects
Connects serialization with structured logging, type classes, JSON, ByteString, key–value databases, and domain-model conversion.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.