Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Created June 9, 2017 17:02
Show Gist options
  • Select an option

  • Save dwhitney/ef474427191c135e9977c090ac30c6da to your computer and use it in GitHub Desktop.

Select an option

Save dwhitney/ef474427191c135e9977c090ac30c6da to your computer and use it in GitHub Desktop.
Toy example of purescript argonaut generic encoding
module Main where
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (log, CONSOLE)
import Data.Argonaut.Core (Json, stringify)
import Data.Argonaut.Generic.Aeson (encodeJson)
import Data.Generic
import Data.Maybe (Maybe(..))
import Prelude (($), Unit)
newtype EmailAddress = EmailAddress String
derive instance genericEmailAddressGeneric EmailAddress
newtype Email = Email{
to EmailAddress
, from EmailAddress
, subject Maybe String
, content Maybe String
}
derive instance genericEmailGeneric Email
newtype BatchEmails = BatchEmails{
emails :: Array Email
}
derive instance batchBatchEmailsGeneric BatchEmails
aBatchOfEmails :: BatchEmails
aBatchOfEmails = BatchEmails{
emails : [
Email{
to : EmailAddress "[email protected]"
, from: EmailAddress "[email protected]"
, subject: Nothing
, content : Just "Hello, World!"
}
, Email{
to : EmailAddress "[email protected]"
, from: EmailAddress "[email protected]"
, subject: Just "This is a subject"
, content : Nothing
}
]
}
anEncodedBatchOfEmails :: Json
anEncodedBatchOfEmails = encodeJson aBatchOfEmails
main :: eff . Eff (console CONSOLE | eff) Unit
main = log $ stringify anEncodedBatchOfEmails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment