Looking at part of the servant
example, I see:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Prelude ()
import Prelude.Compat
import Control.Monad.Except
import Control.Monad.Reader
import Data.Aeson.Types
import Data.Attoparsec.ByteString
import Data.ByteString (ByteString)
import Data.List
import Data.Maybe
import Data.String.Conversions
import Data.Time.Calendar
import GHC.Generics
import Lucid
import Network.HTTP.Media ((//), (/:))
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
import System.Directory
import Text.Blaze
import Text.Blaze.Html.Renderer.Utf8
import qualified Data.Aeson.Parser
import qualified Text.Blaze.Html
type UserAPI1 = "users" :> Get '[JSON] [User]
data User = User
{ name :: String
, age :: Int
, email :: String
, registration_date :: Day
} deriving (Eq, Show, Generic)
instance ToJSON User
When I removed the deriving
of Generic
, I got the following error:
• No instance for (Generic User)
arising from a use of ‘aeson-1.1.2.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
So, it appears that the Generic
typeclass instance for User
enables instance ToJSON User
to, I'm assuming, create a JSON Encoder
for User
.
What's the machinery of instance ToJSON User
, i.e. type signature, if that's the right word?
I'm trying to look at its type from the stack ghci
, i.e. REPL, but failing:
λ: >:t instance
<interactive>:1:1: error: parse error on input ‘instance’
λ: >:i instance
<interactive>:1:1: error: parse error on input ‘instance’
Let's look at the source for
ToJSON
:The
ToJSON
class has a defaulttoJSON
implementation with additional type constraints (includingGeneric
, as you've noticed). This requires theDefaultSignatures
extension; notice at the top of that module you can seeThe other constraint,
GToJSON Zero (Rep a)
, imposes some further restrictions on the structure ofa
, and so not every type with aGeneric
instance will satisfy this signature.Regarding your question about GHCi:
instance
is a Haskell keyword. InspectingtoJSON
may be what you want instead. This will show you the same information we saw in the source: