| Copyright | (c) 2026 DPella AB |
|---|---|
| License | MPL-2.0 |
| Maintainer | matti@dpella.io, lobo@dpella.io |
| Stability | experimental |
| Portability | GHC |
| Safe Haskell | None |
| Language | Haskell2010 |
JSONRPC
Description
An implementation of the JSON-RPC 2.0 protocol types for Haskell, with Aeson serialisation.
Core types
The four message kinds defined by the specification:
JSONRPCRequest— a method call expecting a responseJSONRPCResponse— a successful responseJSONRPCError— an error responseJSONRPCNotification— 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"
Synopsis
- newtype RequestId = RequestId Value
- class (HasField "id" a RequestId, HasField "params" a (RequestParams a), Typeable a, ToJSON (RequestParams a), FromJSON (RequestParams a)) => IsJSONRPCRequest a where
- type RequestParams a
- requestMethod :: Proxy a -> Text
- toJSONRPCRequest :: a -> JSONRPCRequest
- fromJSONRPCRequest :: JSONRPCRequest -> Either String a
- class (HasField "params" a (NotificationParams a), Typeable a, ToJSON (NotificationParams a), FromJSON (NotificationParams a)) => IsJSONRPCNotification a where
- type NotificationParams a
- notificationsMethod :: Proxy a -> Text
- toJSONRPCNotification :: a -> JSONRPCNotification
- fromJSONRPCNotification :: JSONRPCNotification -> Either String a
- newtype ViaJSONRPCRequest a = ViaJSONRPCRequest {
- unViaJSONRPCRequest :: a
- newtype ViaJSONRPCNotification a = ViaJSONRPCNotification {}
- data JSONRPCRequest = JSONRPCRequest {}
- data JSONRPCResponse = JSONRPCResponse {}
- data JSONRPCError = JSONRPCError {}
- data JSONRPCNotification = JSONRPCNotification {}
- data JSONRPCMessage
- data JSONRPCErrorInfo = JSONRPCErrorInfo {}
- data EmptyParams
- pARSE_ERROR :: Int
- iNVALID_REQUEST :: Int
- mETHOD_NOT_FOUND :: Int
- iNVALID_PARAMS :: Int
- iNTERNAL_ERROR :: Int
- rPC_VERSION :: Text
Request identifiers
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.
Type classes
class (HasField "id" a RequestId, HasField "params" a (RequestParams a), Typeable a, ToJSON (RequestParams a), FromJSON (RequestParams a)) => IsJSONRPCRequest a where Source #
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.
Minimal complete definition
Associated Types
type RequestParams a Source #
The type of the params field.
type RequestParams a = RequestParamType (Rep a)
Methods
requestMethod :: Proxy a -> Text Source #
The JSON-RPC method name (e.g. "initialize", "tools/list").
toJSONRPCRequest :: a -> JSONRPCRequest Source #
Convert a typed request to a raw JSONRPCRequest.
fromJSONRPCRequest :: JSONRPCRequest -> Either String a Source #
Parse a raw JSONRPCRequest into a typed request.
class (HasField "params" a (NotificationParams a), Typeable a, ToJSON (NotificationParams a), FromJSON (NotificationParams a)) => IsJSONRPCNotification a where Source #
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.
Minimal complete definition
Associated Types
type NotificationParams a Source #
The type of the params field.
type NotificationParams a = NotificationParamType (Rep a)
Methods
notificationsMethod :: Proxy a -> Text Source #
The JSON-RPC method name.
toJSONRPCNotification :: a -> JSONRPCNotification Source #
Convert a typed notification to a raw JSONRPCNotification.
fromJSONRPCNotification :: JSONRPCNotification -> Either String a Source #
Parse a raw JSONRPCNotification into a typed notification.
Deriving-via wrappers
newtype ViaJSONRPCRequest a Source #
Newtype wrapper for deriving ToJSON and FromJSON via IsJSONRPCRequest.
Constructors
| ViaJSONRPCRequest | |
Fields
| |
Instances
| IsJSONRPCRequest a => FromJSON (ViaJSONRPCRequest a) Source # | |
Defined in JSONRPC Methods parseJSON :: Value -> Parser (ViaJSONRPCRequest a) # parseJSONList :: Value -> Parser [ViaJSONRPCRequest a] # omittedField :: Maybe (ViaJSONRPCRequest a) # | |
| IsJSONRPCRequest a => ToJSON (ViaJSONRPCRequest a) Source # | |
Defined in JSONRPC Methods toJSON :: ViaJSONRPCRequest a -> Value # toEncoding :: ViaJSONRPCRequest a -> Encoding # toJSONList :: [ViaJSONRPCRequest a] -> Value # toEncodingList :: [ViaJSONRPCRequest a] -> Encoding # omitField :: ViaJSONRPCRequest a -> Bool # | |
newtype ViaJSONRPCNotification a Source #
Newtype wrapper for deriving ToJSON and FromJSON via IsJSONRPCNotification.
Constructors
| ViaJSONRPCNotification | |
Fields | |
Instances
| IsJSONRPCNotification a => FromJSON (ViaJSONRPCNotification a) Source # | |
Defined in JSONRPC Methods parseJSON :: Value -> Parser (ViaJSONRPCNotification a) # parseJSONList :: Value -> Parser [ViaJSONRPCNotification a] # omittedField :: Maybe (ViaJSONRPCNotification a) # | |
| IsJSONRPCNotification a => ToJSON (ViaJSONRPCNotification a) Source # | |
Defined in JSONRPC Methods toJSON :: ViaJSONRPCNotification a -> Value # toEncoding :: ViaJSONRPCNotification a -> Encoding # toJSONList :: [ViaJSONRPCNotification a] -> Value # toEncodingList :: [ViaJSONRPCNotification a] -> Encoding # omitField :: ViaJSONRPCNotification a -> Bool # | |
JSON-RPC message types
data JSONRPCRequest Source #
A JSON-RPC request that expects a response.
{ "jsonrpc": "2.0", "id": 1, "method": "foo", "params": { ... } }
Constructors
| JSONRPCRequest | |
Instances
data JSONRPCResponse Source #
A successful (non-error) response to a request.
{ "jsonrpc": "2.0", "id": 1, "result": { ... } }
Constructors
| JSONRPCResponse | |
Instances
| FromJSON JSONRPCResponse Source # | |||||
Defined in JSONRPC Methods parseJSON :: Value -> Parser JSONRPCResponse # parseJSONList :: Value -> Parser [JSONRPCResponse] # | |||||
| ToJSON JSONRPCResponse Source # | |||||
Defined in JSONRPC Methods toJSON :: JSONRPCResponse -> Value # toEncoding :: JSONRPCResponse -> Encoding # toJSONList :: [JSONRPCResponse] -> Value # toEncodingList :: [JSONRPCResponse] -> Encoding # omitField :: JSONRPCResponse -> Bool # | |||||
| Generic JSONRPCResponse Source # | |||||
Defined in JSONRPC Associated Types
Methods from :: JSONRPCResponse -> Rep JSONRPCResponse x # to :: Rep JSONRPCResponse x -> JSONRPCResponse # | |||||
| Show JSONRPCResponse Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> JSONRPCResponse -> ShowS # show :: JSONRPCResponse -> String # showList :: [JSONRPCResponse] -> ShowS # | |||||
| Eq JSONRPCResponse Source # | |||||
Defined in JSONRPC Methods (==) :: JSONRPCResponse -> JSONRPCResponse -> Bool # (/=) :: JSONRPCResponse -> JSONRPCResponse -> Bool # | |||||
| type Rep JSONRPCResponse Source # | |||||
Defined in JSONRPC type Rep JSONRPCResponse = D1 ('MetaData "JSONRPCResponse" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (C1 ('MetaCons "JSONRPCResponse" 'PrefixI 'True) (S1 ('MetaSel ('Just "jsonrpc") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: (S1 ('MetaSel ('Just "id") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 RequestId) :*: S1 ('MetaSel ('Just "result") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Value)))) | |||||
data JSONRPCError Source #
A response to a request that indicates an error occurred.
{ "jsonrpc": "2.0", "id": 1, "error": { "code": -32601, "message": "..." } }
Constructors
| JSONRPCError | |
Instances
| FromJSON JSONRPCError Source # | |||||
Defined in JSONRPC | |||||
| ToJSON JSONRPCError Source # | |||||
Defined in JSONRPC Methods toJSON :: JSONRPCError -> Value # toEncoding :: JSONRPCError -> Encoding # toJSONList :: [JSONRPCError] -> Value # toEncodingList :: [JSONRPCError] -> Encoding # omitField :: JSONRPCError -> Bool # | |||||
| Generic JSONRPCError Source # | |||||
Defined in JSONRPC Associated Types
| |||||
| Show JSONRPCError Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> JSONRPCError -> ShowS # show :: JSONRPCError -> String # showList :: [JSONRPCError] -> ShowS # | |||||
| Eq JSONRPCError Source # | |||||
Defined in JSONRPC | |||||
| type Rep JSONRPCError Source # | |||||
Defined in JSONRPC type Rep JSONRPCError = D1 ('MetaData "JSONRPCError" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (C1 ('MetaCons "JSONRPCError" 'PrefixI 'True) (S1 ('MetaSel ('Just "jsonrpc") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: (S1 ('MetaSel ('Just "id") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 RequestId) :*: S1 ('MetaSel ('Just "error") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 JSONRPCErrorInfo)))) | |||||
data JSONRPCNotification Source #
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.
Constructors
| JSONRPCNotification | |
Instances
| FromJSON JSONRPCNotification Source # | Custom | ||||
Defined in JSONRPC Methods parseJSON :: Value -> Parser JSONRPCNotification # parseJSONList :: Value -> Parser [JSONRPCNotification] # | |||||
| ToJSON JSONRPCNotification Source # | |||||
Defined in JSONRPC Methods toJSON :: JSONRPCNotification -> Value # toEncoding :: JSONRPCNotification -> Encoding # toJSONList :: [JSONRPCNotification] -> Value # toEncodingList :: [JSONRPCNotification] -> Encoding # omitField :: JSONRPCNotification -> Bool # | |||||
| Generic JSONRPCNotification Source # | |||||
Defined in JSONRPC Associated Types
Methods from :: JSONRPCNotification -> Rep JSONRPCNotification x # to :: Rep JSONRPCNotification x -> JSONRPCNotification # | |||||
| Show JSONRPCNotification Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> JSONRPCNotification -> ShowS # show :: JSONRPCNotification -> String # showList :: [JSONRPCNotification] -> ShowS # | |||||
| Eq JSONRPCNotification Source # | |||||
Defined in JSONRPC Methods (==) :: JSONRPCNotification -> JSONRPCNotification -> Bool # (/=) :: JSONRPCNotification -> JSONRPCNotification -> Bool # | |||||
| type Rep JSONRPCNotification Source # | |||||
Defined in JSONRPC type Rep JSONRPCNotification = D1 ('MetaData "JSONRPCNotification" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (C1 ('MetaCons "JSONRPCNotification" 'PrefixI 'True) (S1 ('MetaSel ('Just "jsonrpc") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: (S1 ('MetaSel ('Just "method") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "params") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Value)))) | |||||
data JSONRPCMessage Source #
Any valid JSON-RPC message that can be decoded off the wire.
Useful when reading from a transport where the message kind is unknown.
Constructors
| RequestMessage JSONRPCRequest | |
| ResponseMessage JSONRPCResponse | |
| ErrorMessage JSONRPCError | |
| NotificationMessage JSONRPCNotification |
Instances
| FromJSON JSONRPCMessage Source # | |||||
Defined in JSONRPC Methods parseJSON :: Value -> Parser JSONRPCMessage # parseJSONList :: Value -> Parser [JSONRPCMessage] # | |||||
| ToJSON JSONRPCMessage Source # | |||||
Defined in JSONRPC Methods toJSON :: JSONRPCMessage -> Value # toEncoding :: JSONRPCMessage -> Encoding # toJSONList :: [JSONRPCMessage] -> Value # toEncodingList :: [JSONRPCMessage] -> Encoding # omitField :: JSONRPCMessage -> Bool # | |||||
| Generic JSONRPCMessage Source # | |||||
Defined in JSONRPC Associated Types
Methods from :: JSONRPCMessage -> Rep JSONRPCMessage x # to :: Rep JSONRPCMessage x -> JSONRPCMessage # | |||||
| Show JSONRPCMessage Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> JSONRPCMessage -> ShowS # show :: JSONRPCMessage -> String # showList :: [JSONRPCMessage] -> ShowS # | |||||
| Eq JSONRPCMessage Source # | |||||
Defined in JSONRPC Methods (==) :: JSONRPCMessage -> JSONRPCMessage -> Bool # (/=) :: JSONRPCMessage -> JSONRPCMessage -> Bool # | |||||
| type Rep JSONRPCMessage Source # | |||||
Defined in JSONRPC type Rep JSONRPCMessage = D1 ('MetaData "JSONRPCMessage" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) ((C1 ('MetaCons "RequestMessage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 JSONRPCRequest)) :+: C1 ('MetaCons "ResponseMessage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 JSONRPCResponse))) :+: (C1 ('MetaCons "ErrorMessage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 JSONRPCError)) :+: C1 ('MetaCons "NotificationMessage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 JSONRPCNotification)))) | |||||
data JSONRPCErrorInfo Source #
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.
Constructors
| JSONRPCErrorInfo | |
Instances
| FromJSON JSONRPCErrorInfo Source # | Custom | ||||
Defined in JSONRPC Methods parseJSON :: Value -> Parser JSONRPCErrorInfo # parseJSONList :: Value -> Parser [JSONRPCErrorInfo] # | |||||
| ToJSON JSONRPCErrorInfo Source # | Custom | ||||
Defined in JSONRPC Methods toJSON :: JSONRPCErrorInfo -> Value # toEncoding :: JSONRPCErrorInfo -> Encoding # toJSONList :: [JSONRPCErrorInfo] -> Value # toEncodingList :: [JSONRPCErrorInfo] -> Encoding # omitField :: JSONRPCErrorInfo -> Bool # | |||||
| Generic JSONRPCErrorInfo Source # | |||||
Defined in JSONRPC Associated Types
Methods from :: JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x # to :: Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo # | |||||
| Show JSONRPCErrorInfo Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> JSONRPCErrorInfo -> ShowS # show :: JSONRPCErrorInfo -> String # showList :: [JSONRPCErrorInfo] -> ShowS # | |||||
| Eq JSONRPCErrorInfo Source # | |||||
Defined in JSONRPC Methods (==) :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool # (/=) :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool # | |||||
| type Rep JSONRPCErrorInfo Source # | |||||
Defined in JSONRPC type Rep JSONRPCErrorInfo = D1 ('MetaData "JSONRPCErrorInfo" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (C1 ('MetaCons "JSONRPCErrorInfo" 'PrefixI 'True) (S1 ('MetaSel ('Just "code") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: (S1 ('MetaSel ('Just "message") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "errorData") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Value))))) | |||||
Helpers
data EmptyParams Source #
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).
Instances
| FromJSON EmptyParams Source # | |||||
Defined in JSONRPC | |||||
| ToJSON EmptyParams Source # | |||||
Defined in JSONRPC Methods toJSON :: EmptyParams -> Value # toEncoding :: EmptyParams -> Encoding # toJSONList :: [EmptyParams] -> Value # toEncodingList :: [EmptyParams] -> Encoding # omitField :: EmptyParams -> Bool # | |||||
| Generic EmptyParams Source # | |||||
Defined in JSONRPC Associated Types
| |||||
| Show EmptyParams Source # | |||||
Defined in JSONRPC Methods showsPrec :: Int -> EmptyParams -> ShowS # show :: EmptyParams -> String # showList :: [EmptyParams] -> ShowS # | |||||
| Eq EmptyParams Source # | |||||
Defined in JSONRPC | |||||
| type Rep EmptyParams Source # | |||||
Standard JSON-RPC 2.0 error codes
pARSE_ERROR :: Int Source #
Parse error (-32700).
Invalid JSON was received by the server.
iNVALID_REQUEST :: Int Source #
Invalid request (-32600).
The JSON sent is not a valid Request object.
mETHOD_NOT_FOUND :: Int Source #
Method not found (-32601).
The method does not exist or is not available.
iNVALID_PARAMS :: Int Source #
Invalid params (-32602).
Invalid method parameter(s).
iNTERNAL_ERROR :: Int Source #
Internal error (-32603).
Internal JSON-RPC error.
Constants
rPC_VERSION :: Text Source #
JSON-RPC protocol version string ("2.0").
Every request, response, and notification includes a jsonrpc field
that MUST be exactly this value.