Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Last active October 29, 2021 10:20
Show Gist options
  • Save AlexeyRaga/83366153af48b2e0e5828c0ab9c4229a to your computer and use it in GitHub Desktop.
Save AlexeyRaga/83366153af48b2e0e5828c0ab9c4229a to your computer and use it in GitHub Desktop.
Haskell + JSON with keywords
{-# LANGUAGE DerivingVia, DataKinds, DeriveGeneric, TypeApplications #-}
module Main where
import Control.Lens ((^.))
import Data.Aeson
import Deriving.Aeson
import GHC.Generics (Generic)
import Network.Wreq (Response, asJSON, get, responseBody)
data User = User
{ userId :: Int
, userName :: String
}
deriving (Show, Generic)
deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "user", CamelToSnake]] User
data Payload = Payload
{ payloadData :: [User]
}
deriving (Show, Generic)
deriving (FromJSON, ToJSON) via CustomJSON '[FieldLabelModifier '[StripPrefix "payload", CamelToSnake]] Payload
printResponse :: Response Payload -> IO ()
printResponse r = print (r ^. responseBody)
main :: IO ()
main = do
response <- get "https://gorest.co.in/public/v1/users" >>= asJSON
printResponse response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment