jsonrpc-0.2.0.0: JSON-RPC 2.0 types and type classes for Haskell
Copyright(c) 2026 DPella AB
LicenseMPL-2.0
Maintainermatti@dpella.io, lobo@dpella.io
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

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:

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

Request identifiers

newtype RequestId Source #

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.

Constructors

RequestId Value 

Instances

Instances details
FromJSON RequestId Source # 
Instance details

Defined in JSONRPC

ToJSON RequestId Source # 
Instance details

Defined in JSONRPC

Show RequestId Source # 
Instance details

Defined in JSONRPC

Eq RequestId Source # 
Instance details

Defined in JSONRPC

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

requestMethod

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.

default fromJSONRPCRequest :: forall (c0 :: Meta) (i0 :: Meta) (s0 :: Meta) i1 (s1 :: Meta) i30. (Generic a, Rep a ~ D1 c0 (C1 i0 (S1 s0 (K1 i1 RequestId :: Type -> Type) :*: S1 s1 (K1 i30 (RequestParams a) :: Type -> Type)))) => JSONRPCRequest -> Either String a Source #

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

notificationsMethod

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.

default fromJSONRPCNotification :: forall (c0 :: Meta) (i0 :: Meta) (s1 :: Meta) i30. (Generic a, Rep a ~ D1 c0 (C1 i0 (S1 s1 (K1 i30 (NotificationParams a) :: Type -> Type)))) => JSONRPCNotification -> Either String a Source #

Deriving-via wrappers

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 

Fields

Instances

Instances details
FromJSON JSONRPCRequest Source #

Custom FromJSON instance that treats a missing params key as Null. JSON-RPC 2.0 allows the params field to be omitted.

Instance details

Defined in JSONRPC

ToJSON JSONRPCRequest Source # 
Instance details

Defined in JSONRPC

Generic JSONRPCRequest Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCRequest 
Instance details

Defined in JSONRPC

Show JSONRPCRequest Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCRequest Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCRequest Source # 
Instance details

Defined in JSONRPC

data JSONRPCResponse Source #

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

{ "jsonrpc": "2.0", "id": 1, "result": { ... } }

Constructors

JSONRPCResponse 

Fields

Instances

Instances details
FromJSON JSONRPCResponse Source # 
Instance details

Defined in JSONRPC

ToJSON JSONRPCResponse Source # 
Instance details

Defined in JSONRPC

Generic JSONRPCResponse Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCResponse 
Instance details

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))))
Show JSONRPCResponse Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCResponse Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCResponse Source # 
Instance details

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 

Fields

Instances

Instances details
FromJSON JSONRPCError Source # 
Instance details

Defined in JSONRPC

ToJSON JSONRPCError Source # 
Instance details

Defined in JSONRPC

Generic JSONRPCError Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCError 
Instance details

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))))
Show JSONRPCError Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCError Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCError Source # 
Instance details

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 

Fields

Instances

Instances details
FromJSON JSONRPCNotification Source #

Custom FromJSON instance that treats a missing params key as Null. JSON-RPC 2.0 allows notifications to omit the params field entirely.

Instance details

Defined in JSONRPC

ToJSON JSONRPCNotification Source #

Custom ToJSON instance that omits params when it is Null.

Instance details

Defined in JSONRPC

Generic JSONRPCNotification Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCNotification 
Instance details

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))))
Show JSONRPCNotification Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCNotification Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCNotification Source # 
Instance details

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.

Instances

Instances details
FromJSON JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

ToJSON JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

Generic JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCMessage 
Instance details

Defined in JSONRPC

Show JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCMessage Source # 
Instance details

Defined in JSONRPC

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 

Fields

  • code :: Int

    A number indicating the error type that occurred.

  • message :: Text

    A short description of the error.

  • errorData :: Maybe Value

    Additional information about the error (serialised as "data" in JSON).

Instances

Instances details
FromJSON JSONRPCErrorInfo Source #

Custom FromJSON that reads the JSON key "data" into the Haskell field errorData.

Instance details

Defined in JSONRPC

ToJSON JSONRPCErrorInfo Source #

Custom ToJSON that maps the Haskell field errorData to the JSON key "data" as required by the JSON-RPC 2.0 specification.

Instance details

Defined in JSONRPC

Generic JSONRPCErrorInfo Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep JSONRPCErrorInfo 
Instance details

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)))))
Show JSONRPCErrorInfo Source # 
Instance details

Defined in JSONRPC

Eq JSONRPCErrorInfo Source # 
Instance details

Defined in JSONRPC

type Rep JSONRPCErrorInfo Source # 
Instance details

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

Instances details
FromJSON EmptyParams Source # 
Instance details

Defined in JSONRPC

ToJSON EmptyParams Source # 
Instance details

Defined in JSONRPC

Generic EmptyParams Source # 
Instance details

Defined in JSONRPC

Associated Types

type Rep EmptyParams 
Instance details

Defined in JSONRPC

type Rep EmptyParams = D1 ('MetaData "EmptyParams" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (V1 :: Type -> Type)
Show EmptyParams Source # 
Instance details

Defined in JSONRPC

Eq EmptyParams Source # 
Instance details

Defined in JSONRPC

type Rep EmptyParams Source # 
Instance details

Defined in JSONRPC

type Rep EmptyParams = D1 ('MetaData "EmptyParams" "JSONRPC" "jsonrpc-0.2.0.0-CiW9EhnIi6YA7T1lJn7cYt" 'False) (V1 :: Type -> Type)

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.