glimr/session/payload

Session Payload

Both the file store and cookie store need to serialize session data and flash into a string and parse it back. Centralizing the JSON format here guarantees every store produces and consumes the same wire shape, so switching backends never causes decoding failures from mismatched payload formats.

Values

pub fn decode(
  payload_json: String,
) -> #(dict.Dict(String, String), dict.Dict(String, String))

Falling back to empty dicts on any parse failure means a corrupt or truncated payload degrades to a fresh session rather than crashing the request. Using optional_field for both “_data” and “_flash” handles partial payloads — old sessions written before flash support was added will still load correctly with an empty flash dict.

pub fn encode(
  data: dict.Dict(String, String),
  flash: dict.Dict(String, String),
) -> String

Data and flash are namespaced under “_data” and “_flash” keys rather than merged flat so neither can collide with the other — a session key called “_flash” won’t shadow the actual flash dict. JSON was chosen over ETF or other formats because the cookie store sends it to browsers where binary Erlang terms wouldn’t be readable.

Search Document