Extensible serialization in Haskell -
i'm trying make serialization/deserialization using read , show (which not problem per se), extensible in sense data type can extended (but not shrunk).
suppose have type:
data foo = { bar :: int } deriving (show, read)
and list:
foos = [foo 1, foo 2]
i can deserialize file:
hputstrln filehand . ppshow $ foos
then can serialize back:
!str <- hgetcontents filehand let foosfromfile = frommaybe [] $ (readmaybe :: string -> maybe [foo]) str
but suppose months later want add 'baz' field foo type. direct serialization old-format file no longer work read, need convert file (which don't want).
so, there elegant (without putting explicit versioning logic in program itself) solution still serialize data file, , filling-in missing fields default values? maybe types tricks?
thanks.
this might not looking since want avoid explicit versioning i'd still point out safecopy
go-to solution versioned serialization , @ least makes painless.
i don't think there's way use default show
, read
instances while supporting adding arbitrary amount of new fields, can of course write own read
instance hand handles missing record fields. however, think that's more laborious , error prone using safecopy
.
Comments
Post a Comment