jmacro-rpc-0.1: JSON-RPC clients and servers using JMacro.

Safe HaskellSafe-Infered



The jmacro-rpc library provides an implementation of the JSON-RPC 2.0 protocol in Haskell (, using typeclass based induction to automatically wrap arbitrary Haskell functions up as exposed RPCs, and to generate Haskell functions to call those RPCs on a remote server. Facilities are also included to expose RPCs in javascript on generated HTML pages, and to manage page-local (i.e. conversation) state on these pages. This package provides all core functionality and APIs. Additional backend-specific packages are provided for wiring these functions up to existing HTTP server packages.



type JRequest = [Value]Source

A JSON request is a list of values

type JResult = Either String ValueSource

A JSON result is either an error or a value.

data JsonRPC m s Source

A JsonRPC is a named function that takes a handle to some state, and yields a function from request to result in some monad. It is a representation of the server side of an RPC call.


JsonRPC String (s -> JRequest -> m JResult) 

retErr :: Monad m => a -> m (Either a b)Source

retErr = return . Left

class Monad m => ToJsonRPC a m | a -> m whereSource

This class should not be used directly.


toJsonRPC_ :: a -> [Value] -> m JResultSource


ToJSON b => ToJsonRPC (IO (Either String b)) IO 
(FromJSON a, ToJsonRPC b m) => ToJsonRPC (a -> b) m 

toJsonRPC :: ToJsonRPC a m => String -> a -> JsonRPC m ()Source

Takes a compatible function to a JsonRPC.

toJsonConvRPC :: ToJsonRPC a m => String -> (s -> a) -> JsonRPC m sSource

Takes a compatible function to a JsonRPC making use of state (i.e. a conversation).

class ToJsonRPCCall a b | a -> b whereSource

This class should not be used directly.


toJsonRPCCall_ :: [Value] -> a -> bSource


toJsonRPCCall :: ToJsonRPCCall a b => a -> bSource

Send a function on Jsonable values to a function of the same signature whose logic is invokable on a remote server. This function operates on the type of its argument. The body can remain undefined.

mkJsonRPCPair :: (ToJsonRPC a m, ToJsonRPCCall a t) => String -> (s -> a) -> (t, JsonRPC m s)Source

Takes a name and a function and yields both the server-side JsonRPC and the client-side stub.

invokeRPCLib :: JStatSource

JMacro Javascript stub to invoke json-rpc calls from web browsers.

asIO :: IO a -> IO aSource

id with a helpful type.

handleRpcs :: (Functor m, Monad m) => (Int -> m s) -> [JsonRPC m s] -> ByteString -> m ByteStringSource

Given a function for managing local state, and a list of JsonRPCs over that local state, produce a function from Bytestring to Bytestring mapping json-rpc requests to responses.

mkConversationPageGen :: (MonadIO m1, MonadIO m) => IO timestamp -> (IntMap (timestamp, s) -> IO (IntMap (timestamp, s))) -> ((Int -> m s) -> [JsonRPC m s] -> m1 resp) -> (JStat -> m1 resp) -> IO s -> [JsonRPC m s] -> IO (m1 resp, m1 resp)Source

Used for creating server backends. Yes, this type is confusing. But unless you are creating a server backend, you can ignore it. Takes a function to generate some time identifier, a function to cull page state, a rpc handler, a function for creating a single page, a function to initialize a page state, and a bunch of rpcs, and creates a pair of handler functions (one for POST and one for GET) such that individual clients first pull a page with an embedded ID, along with client-side rpc functions. Those functions in turn interact with state local to that invocation of a page when they make json-rpc requests to the server. The cull function is in IO so it has access to whatever notion of time it desires, including the opportunity to not run at all.