Last active
October 29, 2021 10:20
-
-
Save AlexeyRaga/83366153af48b2e0e5828c0ab9c4229a to your computer and use it in GitHub Desktop.
Haskell + JSON with keywords
This file contains hidden or 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 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