bidirectional: Simple bidirectional serialization and deserialization

[ bsd3, codec, library ] [ Propose Tags ]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.10.0.0 && <4.14), profunctors (>=5 && <5.6) [details]
License BSD-3-Clause
Author Mats Rauhala
Maintainer mats.rauhala@iki.fi
Category Codec
Bug tracker https://github.com/MasseR/bidirectional/issues
Source repo head: git clone https://github.com/MasseR/bidirectional
Uploaded by MasseR at 2020-10-07T19:22:16Z
Distributions
Downloads 188 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-10-07 [all 1 reports]

Readme for bidirectional-0.1.0.0

[back to package description]

Bidirectional - bidirectional parsing

This library is based on the blog by Lysxia.

In it they define a bidirectional parser that can both generate and consume values.

Imagine that you have a record Person and you want to serialize it into a list and deserialize it back.

data Person = Person { name :: String, age :: Int }

The parser is parameterized over the parsing and generation contexts, these needs to be selected carefully when implementing the parsers. It could be for example a RowParser Writer [SQLData] combination for sqlite-simple, or a simple state and writer for our running example.

So, let's figure out the context for our example. We want to encode into a list and then decode the list back into a value.

type SimpleParser a = IParser (StateT [String] Maybe) (Writer [String]) a a

Then we create the parsers using the parser function.

int :: SimpleParser Int
int =
  parser
    (StateT $ \(x:xs) -> (,xs) <$> readMaybe x)
    (\x -> x <$ tell [show x])

string :: SimpleParser String
string =
  parser
    (StateT $ \(x:xs) -> Just (x,xs))
    (\x -> x <$ tell [x])

One small detail for creating records, is that the encoder gets the full record structure by default, so you need to focus in on a specific part of the record using the (.=) function.

person :: SimpleParser Person
person =
  Person <$> name .= string
         <*> age .= int

And now you can use your encoders and decoders

> execWriter (encode person (Person "foo" 30))
["foo", "30"]

> evalStateT (decode person ["foo", "30"])
Just (Person { name = "foo", age = 30 })