Created
June 9, 2017 17:47
-
-
Save dwhitney/31b48a66f6af9a790463633f24bdf22b to your computer and use it in GitHub Desktop.
same example as previous, but now with a EncodeJson type class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Generic | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (log, CONSOLE) | |
import Data.Argonaut.Core (Json, stringify) | |
import Data.Argonaut.Encode.Class (class EncodeJson) | |
import Data.Argonaut.Generic.Aeson (encodeJson) as A | |
import Data.Argonaut.Encode (encodeJson) | |
import Data.Maybe (Maybe(..)) | |
import Prelude (($), Unit) | |
newtype EmailAddress = EmailAddress String | |
derive instance genericEmailAddress ∷ Generic EmailAddress | |
instance encodeEmailAddressJson :: EncodeJson EmailAddress where | |
encodeJson = A.encodeJson | |
newtype Email = Email{ | |
to ∷ EmailAddress | |
, from ∷ EmailAddress | |
, subject ∷ Maybe String | |
, content ∷ Maybe String | |
} | |
derive instance genericEmail ∷ Generic Email | |
instance encodeEmail :: EncodeJson Email where | |
encodeJson = A.encodeJson | |
newtype BatchEmails = BatchEmails{ | |
emails :: Array Email | |
} | |
derive instance batchBatchEmails ∷ Generic BatchEmails | |
instance encodeBatchEmails :: EncodeJson BatchEmails where | |
encodeJson = A.encodeJson | |
aBatchOfEmails :: BatchEmails | |
aBatchOfEmails = BatchEmails{ | |
emails : [ | |
Email{ | |
to : EmailAddress "dustin@gp.com" | |
, from: EmailAddress "agustin@gp.com" | |
, subject: Nothing | |
, content : Just "Hello, World!" | |
} | |
, Email{ | |
to : EmailAddress "dustin@gp.com" | |
, from: EmailAddress "agustin@gp.com" | |
, subject: Just "This is a subject" | |
, content : Nothing | |
} | |
] | |
} | |
anEncodedBatchOfEmails :: Json | |
anEncodedBatchOfEmails = A.encodeJson aBatchOfEmails | |
logToJsonString :: ∀ a eff. EncodeJson a => a -> Eff (console ∷ CONSOLE | eff) Unit | |
logToJsonString a = log $ stringify (encodeJson a) | |
main :: ∀ eff . Eff (console ∷ CONSOLE | eff) Unit | |
main = logToJsonString aBatchOfEmails |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment