Skip to content

Instantly share code, notes, and snippets.

@ddellacosta
Created August 3, 2020 00:02
Show Gist options
  • Save ddellacosta/960ba8b624aef82bd3e4d57e8b8e8c18 to your computer and use it in GitHub Desktop.
Save ddellacosta/960ba8b624aef82bd3e4d57e8b8e8c18 to your computer and use it in GitHub Desktop.
record names via Aeson a la dsal
{-# 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