{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

{- |
Module      : JSONRPC
Description : JSON-RPC 2.0 types and type classes
Copyright   : (c) 2026 DPella AB
License     : MPL-2.0
Maintainer  : matti@dpella.io, lobo@dpella.io
Stability   : experimental
Portability : GHC

An implementation of the [JSON-RPC 2.0](https://www.jsonrpc.org/specification)
protocol types for Haskell, with Aeson serialisation.

= Core types

The four message kinds defined by the specification:

* 'JSONRPCRequest'      — a method call expecting a response
* 'JSONRPCResponse'     — a successful response
* 'JSONRPCError'        — an error response
* 'JSONRPCNotification' — a one-way message (no response)

'JSONRPCMessage' is a tagged union of all four, useful when reading messages
off the wire without knowing the kind in advance.

= Deriving support

'IsJSONRPCRequest' and 'IsJSONRPCNotification' let you define strongly-typed
request\/notification types and derive their JSON-RPC encoding via
@DerivingVia@:

@
data PingRequest = PingRequest
    { id :: RequestId, params :: Maybe () }
    deriving (ToJSON, FromJSON) via ViaJSONRPCRequest PingRequest

instance IsJSONRPCRequest PingRequest where
    requestMethod _ = \"ping\"
@
-}
module JSONRPC (
    -- * Request identifiers
    RequestId (..),

    -- * Type classes
    IsJSONRPCRequest (..),
    IsJSONRPCNotification (..),

    -- * Deriving-via wrappers
    ViaJSONRPCRequest (..),
    ViaJSONRPCNotification (..),

    -- * JSON-RPC message types
    JSONRPCRequest (..),
    JSONRPCResponse (..),
    JSONRPCError (..),
    JSONRPCNotification (..),
    JSONRPCMessage (..),
    JSONRPCErrorInfo (..),

    -- * Helpers
    EmptyParams,

    -- * Standard JSON-RPC 2.0 error codes
    pARSE_ERROR,
    iNVALID_REQUEST,
    mETHOD_NOT_FOUND,
    iNVALID_PARAMS,
    iNTERNAL_ERROR,

    -- * Constants
    rPC_VERSION,
) where

import Control.Applicative ((<|>))
import Data.Aeson
import Data.Aeson qualified as Aeson
import Data.Data (Proxy (..), Typeable, typeRep)
import Data.Kind (Type)
import Data.Text (Text)
import GHC.Generics
import GHC.Records (HasField (getField))
import GHC.TypeError (ErrorMessage (..), TypeError)

-- ---------------------------------------------------------------------------
-- RequestId
-- ---------------------------------------------------------------------------

{- | A uniquely identifying ID for a request in JSON-RPC.

The JSON-RPC 2.0 specification requires that each request has a unique
identifier.  The ID can be a string, number, or null value.  Numbers
SHOULD NOT contain fractional parts.
-}
newtype RequestId = RequestId Value
    deriving stock (Int -> RequestId -> ShowS
[RequestId] -> ShowS
RequestId -> String
(Int -> RequestId -> ShowS)
-> (RequestId -> String)
-> ([RequestId] -> ShowS)
-> Show RequestId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RequestId -> ShowS
showsPrec :: Int -> RequestId -> ShowS
$cshow :: RequestId -> String
show :: RequestId -> String
$cshowList :: [RequestId] -> ShowS
showList :: [RequestId] -> ShowS
Show, RequestId -> RequestId -> Bool
(RequestId -> RequestId -> Bool)
-> (RequestId -> RequestId -> Bool) -> Eq RequestId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RequestId -> RequestId -> Bool
== :: RequestId -> RequestId -> Bool
$c/= :: RequestId -> RequestId -> Bool
/= :: RequestId -> RequestId -> Bool
Eq)
    deriving newtype ([RequestId] -> Value
[RequestId] -> Encoding
RequestId -> Bool
RequestId -> Value
RequestId -> Encoding
(RequestId -> Value)
-> (RequestId -> Encoding)
-> ([RequestId] -> Value)
-> ([RequestId] -> Encoding)
-> (RequestId -> Bool)
-> ToJSON RequestId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: RequestId -> Value
toJSON :: RequestId -> Value
$ctoEncoding :: RequestId -> Encoding
toEncoding :: RequestId -> Encoding
$ctoJSONList :: [RequestId] -> Value
toJSONList :: [RequestId] -> Value
$ctoEncodingList :: [RequestId] -> Encoding
toEncodingList :: [RequestId] -> Encoding
$comitField :: RequestId -> Bool
omitField :: RequestId -> Bool
ToJSON, Maybe RequestId
Value -> Parser [RequestId]
Value -> Parser RequestId
(Value -> Parser RequestId)
-> (Value -> Parser [RequestId])
-> Maybe RequestId
-> FromJSON RequestId
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser RequestId
parseJSON :: Value -> Parser RequestId
$cparseJSONList :: Value -> Parser [RequestId]
parseJSONList :: Value -> Parser [RequestId]
$comittedField :: Maybe RequestId
omittedField :: Maybe RequestId
FromJSON)

-- ---------------------------------------------------------------------------
-- Constants
-- ---------------------------------------------------------------------------

{- | JSON-RPC protocol version string (@\"2.0\"@).

Every request, response, and notification includes a @jsonrpc@ field
that MUST be exactly this value.
-}
rPC_VERSION :: Text
rPC_VERSION :: Text
rPC_VERSION = Text
"2.0"

-- ---------------------------------------------------------------------------
-- EmptyParams
-- ---------------------------------------------------------------------------

{- | Placeholder type for methods that take no parameters.

'EmptyParams' serialises to JSON @null@ and always fails to parse
(it is only used as a type-level marker; the 'FromJSON' instance for
'JSONRPCRequest' defaults missing params to 'Null').
-}
data EmptyParams
    deriving stock ((forall x. EmptyParams -> Rep EmptyParams x)
-> (forall x. Rep EmptyParams x -> EmptyParams)
-> Generic EmptyParams
forall x. Rep EmptyParams x -> EmptyParams
forall x. EmptyParams -> Rep EmptyParams x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EmptyParams -> Rep EmptyParams x
from :: forall x. EmptyParams -> Rep EmptyParams x
$cto :: forall x. Rep EmptyParams x -> EmptyParams
to :: forall x. Rep EmptyParams x -> EmptyParams
Generic)

instance Show EmptyParams where
    show :: EmptyParams -> String
    show :: EmptyParams -> String
show EmptyParams
_ = Value -> String
forall a. Show a => a -> String
show Value
Aeson.Null

instance Eq EmptyParams where
    (==) :: EmptyParams -> EmptyParams -> Bool
    EmptyParams
_ == :: EmptyParams -> EmptyParams -> Bool
== EmptyParams
_ = Bool
True

instance ToJSON EmptyParams where
    toJSON :: EmptyParams -> Value
    toJSON :: EmptyParams -> Value
toJSON EmptyParams
_ = Value
Aeson.Null

instance FromJSON EmptyParams where
    parseJSON :: Value -> Parser EmptyParams
parseJSON Value
_ = String -> Parser EmptyParams
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"EmptyParams can only be used as a placeholder for empty params"

-- ---------------------------------------------------------------------------
-- JSON-RPC error information
-- ---------------------------------------------------------------------------

{- | Structured error information inside a JSON-RPC error response.

@
{ "code": -32601, "message": "Method not found", "data": ... }
@

The @code@ and @message@ fields are mandatory; @data@ is optional and may
contain any additional information about the error.
-}
data JSONRPCErrorInfo = JSONRPCErrorInfo
    { JSONRPCErrorInfo -> Int
code :: Int
    -- ^ A number indicating the error type that occurred.
    , JSONRPCErrorInfo -> Text
message :: Text
    -- ^ A short description of the error.
    , JSONRPCErrorInfo -> Maybe Value
errorData :: Maybe Value
    -- ^ Additional information about the error (serialised as @\"data\"@ in JSON).
    }
    deriving stock (Int -> JSONRPCErrorInfo -> ShowS
[JSONRPCErrorInfo] -> ShowS
JSONRPCErrorInfo -> String
(Int -> JSONRPCErrorInfo -> ShowS)
-> (JSONRPCErrorInfo -> String)
-> ([JSONRPCErrorInfo] -> ShowS)
-> Show JSONRPCErrorInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCErrorInfo -> ShowS
showsPrec :: Int -> JSONRPCErrorInfo -> ShowS
$cshow :: JSONRPCErrorInfo -> String
show :: JSONRPCErrorInfo -> String
$cshowList :: [JSONRPCErrorInfo] -> ShowS
showList :: [JSONRPCErrorInfo] -> ShowS
Show, JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
(JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool)
-> (JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool)
-> Eq JSONRPCErrorInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
== :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
$c/= :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
/= :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
Eq, (forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x)
-> (forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo)
-> Generic JSONRPCErrorInfo
forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
from :: forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
$cto :: forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
to :: forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
Generic)

-- | Custom 'ToJSON' that maps the Haskell field @errorData@ to the JSON key @\"data\"@
-- as required by the JSON-RPC 2.0 specification.
instance ToJSON JSONRPCErrorInfo where
    toJSON :: JSONRPCErrorInfo -> Value
toJSON (JSONRPCErrorInfo Int
c Text
m Maybe Value
d) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$
            [ Key
"code" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Int
c
            , Key
"message" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m
            ]
                [Pair] -> [Pair] -> [Pair]
forall a. Semigroup a => a -> a -> a
<> [Pair] -> (Value -> [Pair]) -> Maybe Value -> [Pair]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Value
v -> [Key
"data" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Value
v]) Maybe Value
d

-- | Custom 'FromJSON' that reads the JSON key @\"data\"@ into the Haskell field @errorData@.
instance FromJSON JSONRPCErrorInfo where
    parseJSON :: Value -> Parser JSONRPCErrorInfo
parseJSON = String
-> (Object -> Parser JSONRPCErrorInfo)
-> Value
-> Parser JSONRPCErrorInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCErrorInfo" ((Object -> Parser JSONRPCErrorInfo)
 -> Value -> Parser JSONRPCErrorInfo)
-> (Object -> Parser JSONRPCErrorInfo)
-> Value
-> Parser JSONRPCErrorInfo
forall a b. (a -> b) -> a -> b
$ \Object
o ->
        Int -> Text -> Maybe Value -> JSONRPCErrorInfo
JSONRPCErrorInfo
            (Int -> Text -> Maybe Value -> JSONRPCErrorInfo)
-> Parser Int -> Parser (Text -> Maybe Value -> JSONRPCErrorInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"code"
            Parser (Text -> Maybe Value -> JSONRPCErrorInfo)
-> Parser Text -> Parser (Maybe Value -> JSONRPCErrorInfo)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"message"
            Parser (Maybe Value -> JSONRPCErrorInfo)
-> Parser (Maybe Value) -> Parser JSONRPCErrorInfo
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"data"

-- ---------------------------------------------------------------------------
-- Core message types
-- ---------------------------------------------------------------------------

{- | A JSON-RPC request that expects a response.

@
{ "jsonrpc": "2.0", "id": 1, "method": "foo", "params": { ... } }
@
-}
data JSONRPCRequest = JSONRPCRequest
    { JSONRPCRequest -> Text
jsonrpc :: Text
    -- ^ Always @\"2.0\"@.
    , JSONRPCRequest -> RequestId
id :: RequestId
    -- ^ A unique identifier for the request.
    , JSONRPCRequest -> Text
method :: Text
    -- ^ The method to invoke.
    , JSONRPCRequest -> Value
params :: Value
    -- ^ The method parameters (defaults to 'Null' if omitted).
    }
    deriving stock (Int -> JSONRPCRequest -> ShowS
[JSONRPCRequest] -> ShowS
JSONRPCRequest -> String
(Int -> JSONRPCRequest -> ShowS)
-> (JSONRPCRequest -> String)
-> ([JSONRPCRequest] -> ShowS)
-> Show JSONRPCRequest
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCRequest -> ShowS
showsPrec :: Int -> JSONRPCRequest -> ShowS
$cshow :: JSONRPCRequest -> String
show :: JSONRPCRequest -> String
$cshowList :: [JSONRPCRequest] -> ShowS
showList :: [JSONRPCRequest] -> ShowS
Show, JSONRPCRequest -> JSONRPCRequest -> Bool
(JSONRPCRequest -> JSONRPCRequest -> Bool)
-> (JSONRPCRequest -> JSONRPCRequest -> Bool) -> Eq JSONRPCRequest
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCRequest -> JSONRPCRequest -> Bool
== :: JSONRPCRequest -> JSONRPCRequest -> Bool
$c/= :: JSONRPCRequest -> JSONRPCRequest -> Bool
/= :: JSONRPCRequest -> JSONRPCRequest -> Bool
Eq, (forall x. JSONRPCRequest -> Rep JSONRPCRequest x)
-> (forall x. Rep JSONRPCRequest x -> JSONRPCRequest)
-> Generic JSONRPCRequest
forall x. Rep JSONRPCRequest x -> JSONRPCRequest
forall x. JSONRPCRequest -> Rep JSONRPCRequest x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCRequest -> Rep JSONRPCRequest x
from :: forall x. JSONRPCRequest -> Rep JSONRPCRequest x
$cto :: forall x. Rep JSONRPCRequest x -> JSONRPCRequest
to :: forall x. Rep JSONRPCRequest x -> JSONRPCRequest
Generic)
    deriving anyclass ([JSONRPCRequest] -> Value
[JSONRPCRequest] -> Encoding
JSONRPCRequest -> Bool
JSONRPCRequest -> Value
JSONRPCRequest -> Encoding
(JSONRPCRequest -> Value)
-> (JSONRPCRequest -> Encoding)
-> ([JSONRPCRequest] -> Value)
-> ([JSONRPCRequest] -> Encoding)
-> (JSONRPCRequest -> Bool)
-> ToJSON JSONRPCRequest
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCRequest -> Value
toJSON :: JSONRPCRequest -> Value
$ctoEncoding :: JSONRPCRequest -> Encoding
toEncoding :: JSONRPCRequest -> Encoding
$ctoJSONList :: [JSONRPCRequest] -> Value
toJSONList :: [JSONRPCRequest] -> Value
$ctoEncodingList :: [JSONRPCRequest] -> Encoding
toEncodingList :: [JSONRPCRequest] -> Encoding
$comitField :: JSONRPCRequest -> Bool
omitField :: JSONRPCRequest -> Bool
ToJSON)

-- | Custom 'FromJSON' instance that treats a missing @params@ key as 'Null'.
-- JSON-RPC 2.0 allows the @params@ field to be omitted.
instance FromJSON JSONRPCRequest where
    parseJSON :: Value -> Parser JSONRPCRequest
parseJSON = String
-> (Object -> Parser JSONRPCRequest)
-> Value
-> Parser JSONRPCRequest
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCRequest" ((Object -> Parser JSONRPCRequest)
 -> Value -> Parser JSONRPCRequest)
-> (Object -> Parser JSONRPCRequest)
-> Value
-> Parser JSONRPCRequest
forall a b. (a -> b) -> a -> b
$ \Object
o ->
        Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest
            (Text -> RequestId -> Text -> Value -> JSONRPCRequest)
-> Parser Text
-> Parser (RequestId -> Text -> Value -> JSONRPCRequest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"jsonrpc"
            Parser (RequestId -> Text -> Value -> JSONRPCRequest)
-> Parser RequestId -> Parser (Text -> Value -> JSONRPCRequest)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser RequestId
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
            Parser (Text -> Value -> JSONRPCRequest)
-> Parser Text -> Parser (Value -> JSONRPCRequest)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
            Parser (Value -> JSONRPCRequest)
-> Parser Value -> Parser JSONRPCRequest
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null

{- | A successful (non-error) response to a request.

@
{ "jsonrpc": "2.0", "id": 1, "result": { ... } }
@
-}
data JSONRPCResponse = JSONRPCResponse
    { JSONRPCResponse -> Text
jsonrpc :: Text
    -- ^ Always @\"2.0\"@.
    , JSONRPCResponse -> RequestId
id :: RequestId
    -- ^ Must match the @id@ of the corresponding request.
    , JSONRPCResponse -> Value
result :: Value
    -- ^ The result of the method invocation.
    }
    deriving stock (Int -> JSONRPCResponse -> ShowS
[JSONRPCResponse] -> ShowS
JSONRPCResponse -> String
(Int -> JSONRPCResponse -> ShowS)
-> (JSONRPCResponse -> String)
-> ([JSONRPCResponse] -> ShowS)
-> Show JSONRPCResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCResponse -> ShowS
showsPrec :: Int -> JSONRPCResponse -> ShowS
$cshow :: JSONRPCResponse -> String
show :: JSONRPCResponse -> String
$cshowList :: [JSONRPCResponse] -> ShowS
showList :: [JSONRPCResponse] -> ShowS
Show, JSONRPCResponse -> JSONRPCResponse -> Bool
(JSONRPCResponse -> JSONRPCResponse -> Bool)
-> (JSONRPCResponse -> JSONRPCResponse -> Bool)
-> Eq JSONRPCResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCResponse -> JSONRPCResponse -> Bool
== :: JSONRPCResponse -> JSONRPCResponse -> Bool
$c/= :: JSONRPCResponse -> JSONRPCResponse -> Bool
/= :: JSONRPCResponse -> JSONRPCResponse -> Bool
Eq, (forall x. JSONRPCResponse -> Rep JSONRPCResponse x)
-> (forall x. Rep JSONRPCResponse x -> JSONRPCResponse)
-> Generic JSONRPCResponse
forall x. Rep JSONRPCResponse x -> JSONRPCResponse
forall x. JSONRPCResponse -> Rep JSONRPCResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCResponse -> Rep JSONRPCResponse x
from :: forall x. JSONRPCResponse -> Rep JSONRPCResponse x
$cto :: forall x. Rep JSONRPCResponse x -> JSONRPCResponse
to :: forall x. Rep JSONRPCResponse x -> JSONRPCResponse
Generic)
    deriving anyclass (Maybe JSONRPCResponse
Value -> Parser [JSONRPCResponse]
Value -> Parser JSONRPCResponse
(Value -> Parser JSONRPCResponse)
-> (Value -> Parser [JSONRPCResponse])
-> Maybe JSONRPCResponse
-> FromJSON JSONRPCResponse
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser JSONRPCResponse
parseJSON :: Value -> Parser JSONRPCResponse
$cparseJSONList :: Value -> Parser [JSONRPCResponse]
parseJSONList :: Value -> Parser [JSONRPCResponse]
$comittedField :: Maybe JSONRPCResponse
omittedField :: Maybe JSONRPCResponse
FromJSON, [JSONRPCResponse] -> Value
[JSONRPCResponse] -> Encoding
JSONRPCResponse -> Bool
JSONRPCResponse -> Value
JSONRPCResponse -> Encoding
(JSONRPCResponse -> Value)
-> (JSONRPCResponse -> Encoding)
-> ([JSONRPCResponse] -> Value)
-> ([JSONRPCResponse] -> Encoding)
-> (JSONRPCResponse -> Bool)
-> ToJSON JSONRPCResponse
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCResponse -> Value
toJSON :: JSONRPCResponse -> Value
$ctoEncoding :: JSONRPCResponse -> Encoding
toEncoding :: JSONRPCResponse -> Encoding
$ctoJSONList :: [JSONRPCResponse] -> Value
toJSONList :: [JSONRPCResponse] -> Value
$ctoEncodingList :: [JSONRPCResponse] -> Encoding
toEncodingList :: [JSONRPCResponse] -> Encoding
$comitField :: JSONRPCResponse -> Bool
omitField :: JSONRPCResponse -> Bool
ToJSON)

{- | A response to a request that indicates an error occurred.

@
{ "jsonrpc": "2.0", "id": 1, "error": { "code": -32601, "message": "..." } }
@
-}
data JSONRPCError = JSONRPCError
    { JSONRPCError -> Text
jsonrpc :: Text
    -- ^ Always @\"2.0\"@.
    , JSONRPCError -> RequestId
id :: RequestId
    -- ^ Must match the @id@ of the corresponding request (or 'Null' for parse errors).
    , JSONRPCError -> JSONRPCErrorInfo
error :: JSONRPCErrorInfo
    -- ^ Structured error information.
    }
    deriving stock (Int -> JSONRPCError -> ShowS
[JSONRPCError] -> ShowS
JSONRPCError -> String
(Int -> JSONRPCError -> ShowS)
-> (JSONRPCError -> String)
-> ([JSONRPCError] -> ShowS)
-> Show JSONRPCError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCError -> ShowS
showsPrec :: Int -> JSONRPCError -> ShowS
$cshow :: JSONRPCError -> String
show :: JSONRPCError -> String
$cshowList :: [JSONRPCError] -> ShowS
showList :: [JSONRPCError] -> ShowS
Show, JSONRPCError -> JSONRPCError -> Bool
(JSONRPCError -> JSONRPCError -> Bool)
-> (JSONRPCError -> JSONRPCError -> Bool) -> Eq JSONRPCError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCError -> JSONRPCError -> Bool
== :: JSONRPCError -> JSONRPCError -> Bool
$c/= :: JSONRPCError -> JSONRPCError -> Bool
/= :: JSONRPCError -> JSONRPCError -> Bool
Eq, (forall x. JSONRPCError -> Rep JSONRPCError x)
-> (forall x. Rep JSONRPCError x -> JSONRPCError)
-> Generic JSONRPCError
forall x. Rep JSONRPCError x -> JSONRPCError
forall x. JSONRPCError -> Rep JSONRPCError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCError -> Rep JSONRPCError x
from :: forall x. JSONRPCError -> Rep JSONRPCError x
$cto :: forall x. Rep JSONRPCError x -> JSONRPCError
to :: forall x. Rep JSONRPCError x -> JSONRPCError
Generic)
    deriving anyclass (Maybe JSONRPCError
Value -> Parser [JSONRPCError]
Value -> Parser JSONRPCError
(Value -> Parser JSONRPCError)
-> (Value -> Parser [JSONRPCError])
-> Maybe JSONRPCError
-> FromJSON JSONRPCError
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser JSONRPCError
parseJSON :: Value -> Parser JSONRPCError
$cparseJSONList :: Value -> Parser [JSONRPCError]
parseJSONList :: Value -> Parser [JSONRPCError]
$comittedField :: Maybe JSONRPCError
omittedField :: Maybe JSONRPCError
FromJSON, [JSONRPCError] -> Value
[JSONRPCError] -> Encoding
JSONRPCError -> Bool
JSONRPCError -> Value
JSONRPCError -> Encoding
(JSONRPCError -> Value)
-> (JSONRPCError -> Encoding)
-> ([JSONRPCError] -> Value)
-> ([JSONRPCError] -> Encoding)
-> (JSONRPCError -> Bool)
-> ToJSON JSONRPCError
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCError -> Value
toJSON :: JSONRPCError -> Value
$ctoEncoding :: JSONRPCError -> Encoding
toEncoding :: JSONRPCError -> Encoding
$ctoJSONList :: [JSONRPCError] -> Value
toJSONList :: [JSONRPCError] -> Value
$ctoEncodingList :: [JSONRPCError] -> Encoding
toEncodingList :: [JSONRPCError] -> Encoding
$comitField :: JSONRPCError -> Bool
omitField :: JSONRPCError -> Bool
ToJSON)

{- | A notification which does not expect a response.

@
{ "jsonrpc": "2.0", "method": "update", "params": { ... } }
@

Notifications have no @id@ field.  The server MUST NOT reply to a notification.
-}
data JSONRPCNotification = JSONRPCNotification
    { JSONRPCNotification -> Text
jsonrpc :: Text
    -- ^ Always @\"2.0\"@.
    , JSONRPCNotification -> Text
method :: Text
    -- ^ The method name.
    , JSONRPCNotification -> Value
params :: Value
    -- ^ The method parameters (omitted from JSON when 'Null').
    }
    deriving stock (Int -> JSONRPCNotification -> ShowS
[JSONRPCNotification] -> ShowS
JSONRPCNotification -> String
(Int -> JSONRPCNotification -> ShowS)
-> (JSONRPCNotification -> String)
-> ([JSONRPCNotification] -> ShowS)
-> Show JSONRPCNotification
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCNotification -> ShowS
showsPrec :: Int -> JSONRPCNotification -> ShowS
$cshow :: JSONRPCNotification -> String
show :: JSONRPCNotification -> String
$cshowList :: [JSONRPCNotification] -> ShowS
showList :: [JSONRPCNotification] -> ShowS
Show, JSONRPCNotification -> JSONRPCNotification -> Bool
(JSONRPCNotification -> JSONRPCNotification -> Bool)
-> (JSONRPCNotification -> JSONRPCNotification -> Bool)
-> Eq JSONRPCNotification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCNotification -> JSONRPCNotification -> Bool
== :: JSONRPCNotification -> JSONRPCNotification -> Bool
$c/= :: JSONRPCNotification -> JSONRPCNotification -> Bool
/= :: JSONRPCNotification -> JSONRPCNotification -> Bool
Eq, (forall x. JSONRPCNotification -> Rep JSONRPCNotification x)
-> (forall x. Rep JSONRPCNotification x -> JSONRPCNotification)
-> Generic JSONRPCNotification
forall x. Rep JSONRPCNotification x -> JSONRPCNotification
forall x. JSONRPCNotification -> Rep JSONRPCNotification x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCNotification -> Rep JSONRPCNotification x
from :: forall x. JSONRPCNotification -> Rep JSONRPCNotification x
$cto :: forall x. Rep JSONRPCNotification x -> JSONRPCNotification
to :: forall x. Rep JSONRPCNotification x -> JSONRPCNotification
Generic)

-- | Custom 'ToJSON' instance that omits @params@ when it is 'Null'.
instance ToJSON JSONRPCNotification where
    toJSON :: JSONRPCNotification -> Value
toJSON (JSONRPCNotification Text
j Text
m Value
Null) =
        [Pair] -> Value
object [Key
"jsonrpc" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
j, Key
"method" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m]
    toJSON (JSONRPCNotification Text
j Text
m Value
p) =
        [Pair] -> Value
object [Key
"jsonrpc" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
j, Key
"method" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m, Key
"params" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Value
p]

-- | Custom 'FromJSON' instance that treats a missing @params@ key as 'Null'.
-- JSON-RPC 2.0 allows notifications to omit the @params@ field entirely.
instance FromJSON JSONRPCNotification where
    parseJSON :: Value -> Parser JSONRPCNotification
parseJSON = String
-> (Object -> Parser JSONRPCNotification)
-> Value
-> Parser JSONRPCNotification
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCNotification" ((Object -> Parser JSONRPCNotification)
 -> Value -> Parser JSONRPCNotification)
-> (Object -> Parser JSONRPCNotification)
-> Value
-> Parser JSONRPCNotification
forall a b. (a -> b) -> a -> b
$ \Object
o ->
        Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification
            (Text -> Text -> Value -> JSONRPCNotification)
-> Parser Text -> Parser (Text -> Value -> JSONRPCNotification)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"jsonrpc"
            Parser (Text -> Value -> JSONRPCNotification)
-> Parser Text -> Parser (Value -> JSONRPCNotification)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
            Parser (Value -> JSONRPCNotification)
-> Parser Value -> Parser JSONRPCNotification
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null

{- | Any valid JSON-RPC message that can be decoded off the wire.

Useful when reading from a transport where the message kind is unknown.
-}
data JSONRPCMessage
    = RequestMessage JSONRPCRequest
    | ResponseMessage JSONRPCResponse
    | ErrorMessage JSONRPCError
    | NotificationMessage JSONRPCNotification
    deriving stock (Int -> JSONRPCMessage -> ShowS
[JSONRPCMessage] -> ShowS
JSONRPCMessage -> String
(Int -> JSONRPCMessage -> ShowS)
-> (JSONRPCMessage -> String)
-> ([JSONRPCMessage] -> ShowS)
-> Show JSONRPCMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCMessage -> ShowS
showsPrec :: Int -> JSONRPCMessage -> ShowS
$cshow :: JSONRPCMessage -> String
show :: JSONRPCMessage -> String
$cshowList :: [JSONRPCMessage] -> ShowS
showList :: [JSONRPCMessage] -> ShowS
Show, JSONRPCMessage -> JSONRPCMessage -> Bool
(JSONRPCMessage -> JSONRPCMessage -> Bool)
-> (JSONRPCMessage -> JSONRPCMessage -> Bool) -> Eq JSONRPCMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCMessage -> JSONRPCMessage -> Bool
== :: JSONRPCMessage -> JSONRPCMessage -> Bool
$c/= :: JSONRPCMessage -> JSONRPCMessage -> Bool
/= :: JSONRPCMessage -> JSONRPCMessage -> Bool
Eq, (forall x. JSONRPCMessage -> Rep JSONRPCMessage x)
-> (forall x. Rep JSONRPCMessage x -> JSONRPCMessage)
-> Generic JSONRPCMessage
forall x. Rep JSONRPCMessage x -> JSONRPCMessage
forall x. JSONRPCMessage -> Rep JSONRPCMessage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCMessage -> Rep JSONRPCMessage x
from :: forall x. JSONRPCMessage -> Rep JSONRPCMessage x
$cto :: forall x. Rep JSONRPCMessage x -> JSONRPCMessage
to :: forall x. Rep JSONRPCMessage x -> JSONRPCMessage
Generic)

instance ToJSON JSONRPCMessage where
    toJSON :: JSONRPCMessage -> Value
toJSON (RequestMessage JSONRPCRequest
r) = JSONRPCRequest -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCRequest
r
    toJSON (ResponseMessage JSONRPCResponse
r) = JSONRPCResponse -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCResponse
r
    toJSON (ErrorMessage JSONRPCError
e) = JSONRPCError -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCError
e
    toJSON (NotificationMessage JSONRPCNotification
n) = JSONRPCNotification -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCNotification
n

instance FromJSON JSONRPCMessage where
    parseJSON :: Value -> Parser JSONRPCMessage
parseJSON Value
v =
        (JSONRPCRequest -> JSONRPCMessage
RequestMessage (JSONRPCRequest -> JSONRPCMessage)
-> Parser JSONRPCRequest -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCRequest
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
            Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCResponse -> JSONRPCMessage
ResponseMessage (JSONRPCResponse -> JSONRPCMessage)
-> Parser JSONRPCResponse -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCResponse
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
            Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCError -> JSONRPCMessage
ErrorMessage (JSONRPCError -> JSONRPCMessage)
-> Parser JSONRPCError -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCError
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
            Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCNotification -> JSONRPCMessage
NotificationMessage (JSONRPCNotification -> JSONRPCMessage)
-> Parser JSONRPCNotification -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCNotification
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)

-- ---------------------------------------------------------------------------
-- IsJSONRPCRequest
-- ---------------------------------------------------------------------------

-- | Type family to extract the type of the @params@ field from a generic representation.
type family RequestParamType (rep :: Type -> Type) :: Type where
    RequestParamType (D1 _ (C1 _ (S1 _ (K1 _ RequestId) :*: S1 _ (K1 _ p)))) = p
    RequestParamType (D1 ('MetaData nm _ _ _) _) =
        TypeError
            ( ('Text "Error when defining IsJSONRPCRequest for " :<>: 'Text nm :<>: 'Text ":")
                :$$: 'Text "The request datatype must be a record with fields 'id' and 'params'"
            )

{- | Type class for Haskell types that represent a specific JSON-RPC request method.

Instances define the method name via 'requestMethod'.  Serialisation and
deserialisation are provided by default using GHC Generics: the type must
be a two-field record with @id :: RequestId@ and @params :: SomeParamsType@.

Use 'ViaJSONRPCRequest' for @DerivingVia@-based 'ToJSON'\/'FromJSON' instances.
-}
class
    ( HasField "id" a RequestId
    , HasField "params" a (RequestParams a)
    , Typeable a
    , ToJSON (RequestParams a)
    , FromJSON (RequestParams a)
    ) =>
    IsJSONRPCRequest a
    where
    -- | The type of the @params@ field.
    type RequestParams a
    type RequestParams a = RequestParamType (Rep a)

    -- | The JSON-RPC method name (e.g. @\"initialize\"@, @\"tools\/list\"@).
    requestMethod :: Proxy a -> Text

    -- | Convert a typed request to a raw 'JSONRPCRequest'.
    toJSONRPCRequest :: a -> JSONRPCRequest
    toJSONRPCRequest (a
req :: a) =
        Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest Text
rPC_VERSION (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"id" a
req) (Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) (RequestParams a -> Value
forall a. ToJSON a => a -> Value
toJSON (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"params" a
req))

    -- | Parse a raw 'JSONRPCRequest' into a typed request.
    fromJSONRPCRequest :: JSONRPCRequest -> Either String a
    default fromJSONRPCRequest ::
        ( Generic a
        , Rep a ~ D1 c0 (C1 i0 (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a))))
        ) =>
        JSONRPCRequest ->
        Either String a
    fromJSONRPCRequest JSONRPCRequest{id :: JSONRPCRequest -> RequestId
id = RequestId
req_id, params :: JSONRPCRequest -> Value
params = Value
req_params} =
        case forall a. FromJSON a => Value -> Result a
fromJSON @(RequestParams a) Value
req_params of
            Success RequestParams a
p -> a -> Either String a
forall a b. b -> Either a b
Right (a -> Either String a) -> a -> Either String a
forall a b. (a -> b) -> a -> b
$ Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> Rep a Any -> a
forall a b. (a -> b) -> a -> b
$ M1
  C
  i0
  (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a)))
  Any
-> M1
     D
     c0
     (C1
        i0 (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a))))
     Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 ((:*:)
  (S1 s0 (K1 i1 RequestId)) (S1 s1 (K1 i30 (RequestParams a))) Any
-> M1
     C
     i0
     (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a)))
     Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (K1 i1 RequestId Any -> M1 S s0 (K1 i1 RequestId) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (RequestId -> K1 i1 RequestId Any
forall k i c (p :: k). c -> K1 i c p
K1 RequestId
req_id) M1 S s0 (K1 i1 RequestId) Any
-> M1 S s1 (K1 i30 (RequestParams a)) Any
-> (:*:)
     (S1 s0 (K1 i1 RequestId)) (S1 s1 (K1 i30 (RequestParams a))) Any
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: K1 i30 (RequestParams a) Any
-> M1 S s1 (K1 i30 (RequestParams a)) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (RequestParams a -> K1 i30 (RequestParams a) Any
forall k i c (p :: k). c -> K1 i c p
K1 RequestParams a
p)))
            Aeson.Error String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err

-- | Newtype wrapper for deriving 'ToJSON' and 'FromJSON' via 'IsJSONRPCRequest'.
newtype ViaJSONRPCRequest a = ViaJSONRPCRequest {forall a. ViaJSONRPCRequest a -> a
unViaJSONRPCRequest :: a}

instance (IsJSONRPCRequest a) => ToJSON (ViaJSONRPCRequest a) where
    toJSON :: ViaJSONRPCRequest a -> Value
toJSON = JSONRPCRequest -> Value
forall a. ToJSON a => a -> Value
toJSON (JSONRPCRequest -> Value)
-> (ViaJSONRPCRequest a -> JSONRPCRequest)
-> ViaJSONRPCRequest a
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> JSONRPCRequest
forall a. IsJSONRPCRequest a => a -> JSONRPCRequest
toJSONRPCRequest (a -> JSONRPCRequest)
-> (ViaJSONRPCRequest a -> a)
-> ViaJSONRPCRequest a
-> JSONRPCRequest
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViaJSONRPCRequest a -> a
forall a. ViaJSONRPCRequest a -> a
unViaJSONRPCRequest

instance (IsJSONRPCRequest a) => FromJSON (ViaJSONRPCRequest a) where
    parseJSON :: Value -> Parser (ViaJSONRPCRequest a)
parseJSON = String
-> (Object -> Parser (ViaJSONRPCRequest a))
-> Value
-> Parser (ViaJSONRPCRequest a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) ((Object -> Parser (ViaJSONRPCRequest a))
 -> Value -> Parser (ViaJSONRPCRequest a))
-> (Object -> Parser (ViaJSONRPCRequest a))
-> Value
-> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Text
m <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
        RequestId
req_id <- Object
o Object -> Key -> Parser RequestId
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
        if Text
m Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)
            then do
                Value
p <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
                case JSONRPCRequest -> Either String a
forall a. IsJSONRPCRequest a => JSONRPCRequest -> Either String a
fromJSONRPCRequest (Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest Text
rPC_VERSION RequestId
req_id Text
m Value
p) of
                    Right a
r -> ViaJSONRPCRequest a -> Parser (ViaJSONRPCRequest a)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ViaJSONRPCRequest a
forall a. a -> ViaJSONRPCRequest a
ViaJSONRPCRequest a
r)
                    Left String
err -> String -> Parser (ViaJSONRPCRequest a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCRequest a))
-> String -> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse params for " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TypeRep -> String
forall a. Show a => a -> String
show (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err
            else String -> Parser (ViaJSONRPCRequest a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCRequest a))
-> String -> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ String
"Expected method '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show (Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'"

-- ---------------------------------------------------------------------------
-- IsJSONRPCNotification
-- ---------------------------------------------------------------------------

-- | Type family to extract the type of the @params@ field from a notification's generic representation.
type family NotificationParamType (rep :: Type -> Type) :: Type where
    NotificationParamType (D1 _ (C1 _ (S1 _ (K1 _ p)))) = p
    NotificationParamType (D1 ('MetaData nm _ _ _) _) =
        TypeError
            ( ('Text "Error when defining IsJSONRPCNotification for " :<>: 'Text nm :<>: 'Text ":")
                :$$: 'Text "The notification datatype must be a record with a single field 'params'"
            )

{- | Type class for Haskell types that represent a specific JSON-RPC notification method.

Similar to 'IsJSONRPCRequest' but for notifications (no @id@ field).
Use 'ViaJSONRPCNotification' for @DerivingVia@-based instances.
-}
class
    ( HasField "params" a (NotificationParams a)
    , Typeable a
    , ToJSON (NotificationParams a)
    , FromJSON (NotificationParams a)
    ) =>
    IsJSONRPCNotification a
    where
    -- | The type of the @params@ field.
    type NotificationParams a
    type NotificationParams a = NotificationParamType (Rep a)

    -- | The JSON-RPC method name.
    notificationsMethod :: Proxy a -> Text

    -- | Convert a typed notification to a raw 'JSONRPCNotification'.
    toJSONRPCNotification :: a -> JSONRPCNotification
    toJSONRPCNotification (a
req :: a) =
        Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification Text
rPC_VERSION (Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) (NotificationParams a -> Value
forall a. ToJSON a => a -> Value
toJSON (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"params" a
req))

    -- | Parse a raw 'JSONRPCNotification' into a typed notification.
    fromJSONRPCNotification :: JSONRPCNotification -> Either String a
    default fromJSONRPCNotification ::
        ( Generic a
        , Rep a ~ D1 c0 (C1 i0 (S1 s1 (K1 i30 (NotificationParams a))))
        ) =>
        JSONRPCNotification ->
        Either String a
    fromJSONRPCNotification JSONRPCNotification{params :: JSONRPCNotification -> Value
params = Value
notification_params} =
        case forall a. FromJSON a => Value -> Result a
fromJSON @(NotificationParams a) Value
notification_params of
            Success NotificationParams a
p -> a -> Either String a
forall a b. b -> Either a b
Right (a -> Either String a) -> a -> Either String a
forall a b. (a -> b) -> a -> b
$ Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> Rep a Any -> a
forall a b. (a -> b) -> a -> b
$ M1 C i0 (S1 s1 (K1 i30 (NotificationParams a))) Any
-> M1 D c0 (C1 i0 (S1 s1 (K1 i30 (NotificationParams a)))) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (M1 S s1 (K1 i30 (NotificationParams a)) Any
-> M1 C i0 (S1 s1 (K1 i30 (NotificationParams a))) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (K1 i30 (NotificationParams a) Any
-> M1 S s1 (K1 i30 (NotificationParams a)) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (NotificationParams a -> K1 i30 (NotificationParams a) Any
forall k i c (p :: k). c -> K1 i c p
K1 NotificationParams a
p)))
            Aeson.Error String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err

-- | Newtype wrapper for deriving 'ToJSON' and 'FromJSON' via 'IsJSONRPCNotification'.
newtype ViaJSONRPCNotification a = ViaJSONRPCNotification {forall a. ViaJSONRPCNotification a -> a
unViaJSONRPCNotification :: a}

instance (IsJSONRPCNotification a) => ToJSON (ViaJSONRPCNotification a) where
    toJSON :: ViaJSONRPCNotification a -> Value
toJSON = JSONRPCNotification -> Value
forall a. ToJSON a => a -> Value
toJSON (JSONRPCNotification -> Value)
-> (ViaJSONRPCNotification a -> JSONRPCNotification)
-> ViaJSONRPCNotification a
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> JSONRPCNotification
forall a. IsJSONRPCNotification a => a -> JSONRPCNotification
toJSONRPCNotification (a -> JSONRPCNotification)
-> (ViaJSONRPCNotification a -> a)
-> ViaJSONRPCNotification a
-> JSONRPCNotification
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViaJSONRPCNotification a -> a
forall a. ViaJSONRPCNotification a -> a
unViaJSONRPCNotification

instance (IsJSONRPCNotification a) => FromJSON (ViaJSONRPCNotification a) where
    parseJSON :: Value -> Parser (ViaJSONRPCNotification a)
parseJSON = String
-> (Object -> Parser (ViaJSONRPCNotification a))
-> Value
-> Parser (ViaJSONRPCNotification a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) ((Object -> Parser (ViaJSONRPCNotification a))
 -> Value -> Parser (ViaJSONRPCNotification a))
-> (Object -> Parser (ViaJSONRPCNotification a))
-> Value
-> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Text
m <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
        if Text
m Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)
            then do
                Value
p <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
                case JSONRPCNotification -> Either String a
forall a.
IsJSONRPCNotification a =>
JSONRPCNotification -> Either String a
fromJSONRPCNotification (Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification Text
rPC_VERSION Text
m Value
p) of
                    Right a
r -> ViaJSONRPCNotification a -> Parser (ViaJSONRPCNotification a)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ViaJSONRPCNotification a
forall a. a -> ViaJSONRPCNotification a
ViaJSONRPCNotification a
r)
                    Left String
err -> String -> Parser (ViaJSONRPCNotification a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCNotification a))
-> String -> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse params for " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TypeRep -> String
forall a. Show a => a -> String
show (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err
            else String -> Parser (ViaJSONRPCNotification a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCNotification a))
-> String -> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ String
"Expected method '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show (Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'"

-- ---------------------------------------------------------------------------
-- Standard JSON-RPC 2.0 error codes
-- ---------------------------------------------------------------------------

{- | Parse error (@-32700@).

Invalid JSON was received by the server.
-}
pARSE_ERROR :: Int
pARSE_ERROR :: Int
pARSE_ERROR = -Int
32700

{- | Invalid request (@-32600@).

The JSON sent is not a valid Request object.
-}
iNVALID_REQUEST :: Int
iNVALID_REQUEST :: Int
iNVALID_REQUEST = -Int
32600

{- | Method not found (@-32601@).

The method does not exist or is not available.
-}
mETHOD_NOT_FOUND :: Int
mETHOD_NOT_FOUND :: Int
mETHOD_NOT_FOUND = -Int
32601

{- | Invalid params (@-32602@).

Invalid method parameter(s).
-}
iNVALID_PARAMS :: Int
iNVALID_PARAMS :: Int
iNVALID_PARAMS = -Int
32602

{- | Internal error (@-32603@).

Internal JSON-RPC error.
-}
iNTERNAL_ERROR :: Int
iNTERNAL_ERROR :: Int
iNTERNAL_ERROR = -Int
32603