Created
August 3, 2020 00:02
-
-
Save ddellacosta/960ba8b624aef82bd3e4d57e8b8e8c18 to your computer and use it in GitHub Desktop.
record names via Aeson a la dsal
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
{-# LANGUAGE DeriveGeneric #-} | |
module Foo where | |
import qualified GHC.Generics as G | |
import Data.Aeson | |
import Data.Aeson.Lens | |
import Data.HashMap.Strict | |
import Control.Lens | |
data Color = Red | Yellow | Blue | |
deriving (Show, G.Generic) | |
data Foo = Foo { | |
name :: String | |
, id :: Int | |
, favoriteColor :: Color | |
} deriving (Show, G.Generic) | |
instance ToJSON Color where | |
toEncoding = genericToEncoding defaultOptions | |
instance FromJSON Color | |
instance ToJSON Foo where | |
toEncoding = genericToEncoding defaultOptions | |
instance FromJSON Foo | |
getRecFieldnames rec = (toJSON rec) ^.. _Object . to keys . traversed | |
-- | |
-- λ> getRecFieldnames (Foo "Bob" 1 Red) | |
-- ["name","id","favoriteColor"] | |
-- λ> | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment