Skip to content

Instantly share code, notes, and snippets.

@xe-4
Created November 25, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xe-4/27c685e6d22e05251c1a653fec791926 to your computer and use it in GitHub Desktop.
Save xe-4/27c685e6d22e05251c1a653fec791926 to your computer and use it in GitHub Desktop.
> :set -XDeriveGeneric
> :set -XGeneralizedNewtypeDeriving
> import GHC.Generics
> data Bar = Bar { baz :: Int, qux :: Int } deriving (Generic, Eq, Show)
> instance ToJSON Bar
> instance FromJSON Bar
> toJSON $ Bar 1 2
Object (fromList [("baz",Number 1.0),("qux",Number 2.0)])
> newtype Foo = Foo Bar deriving (Generic, Eq, Show)
> instance ToJSON Foo
> instance FromJSON Foo
> toJSON . Foo $ Bar 1 2
Object (fromList [("baz",Number 1.0),("qux",Number 2.0)])
> :set -XDerivingStrategies
> :{
newtype Zoo = Zoo Bar
deriving stock Generic
deriving newtype (Eq, Show)
:}
> instance ToJSON Zoo
> instance FromJSON Zoo
> toJSON . Zoo $ Bar 1 2
Object (fromList [("baz",Number 1.0),("qux",Number 2.0)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment