Package: Serialization
Minimum usable: configurator
There are many great Haskell libraries. But usually it will take equally great amount of effort for people to get familiar with them.
Worstly, it seems like some examples were designed to bring more obstracle deliberately.Packagesection follows theminimum usableprinciple. Examples of this section is clear enough for reminding myself in future. Hopefully, it will also offer some help to others.
online resouces:
Notes
Both packages can be used the same ways 1. GHC extension
> {-# LANGUAGE DeriveGeneric #-}
> {-# LANGUAGE DefaultSignatures #-}
- Import corresponding libraries:
sore package
import Data.Sort
cereal package
import Data.Serialize
Rely on
GHC.Genericsimport GHC.Generics ... data DemoMap = DemoMap { toMapList :: [([String], [Float])] } deriving (Generic, Show)Declare as Instance
storepackageinstance Store DemoMapcerealpackageinstance Serialization DemoMapWrite to / Read from file by
Data.ByteString
store pakcage
import qualified Data.ByteString as BL
...
serializeDemoMap:: FilePath -> StandardMap -> IO ()
serializeDemoMap filePath sdmap = BL.writeFile filePath (encode sdmap)
deserializeDemoMap :: FilePath -> IO (Either PeekException StandardMap)
deserializeDemoMap filePath = do
bs <- BL.readFile filePath
return $ decode bs
cereal package
import qualified Data.ByteString.Lazy as BL
...
serializeDemoMap :: FilePath -> StandardMap -> IO ()
serializeDemoMap filePath sdmap = BL.writeFile filePath (encodeLazy sdmap)
deserializeDemoMap :: FilePath -> IO (Either String StandardMap)
deserializeDemoMap filePath = do
bs <- BL.readFile filePath
return $ decodeLazy bs
- The file produced by
storeis about1/3bigger than file produced bycereal. storehandles parametric data type more smoothly.- Performance test: TODO