Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created August 10, 2013 16:15
Show Gist options
  • Save bmjames/6201025 to your computer and use it in GitHub Desktop.
Save bmjames/6201025 to your computer and use it in GitHub Desktop.
Gratuitously point-free JSON encoding
data Dog = Dog { name :: Text
, age :: Int
, likes :: Text
} deriving Show
instance ToJSON Dog where
toJSON = asObject [ name `as` "name"
, age `as` "age"
, likes `as` "likes"
]
as :: ToJSON b => (a -> b) -> Text -> a -> Pair
as getter = (. getter) . (.=)
asObject :: [a -> Pair] -> a -> Value
asObject = (object .) . sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment