1.4 library(json_rpc_server): JSON RPC Server
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog JSON library
        • Supporting JSON
          • library(json_rpc_server): JSON RPC Server
            • json_method/1
            • json_rpc_dispatch/2
            • json_rpc_dispatch_request/4
            • json_rpc_error/2
            • json_rpc_error/3
Availability::- use_module(library(json_rpc_server)).(can be autoloaded)
Sourcejson_method(+Methods)
Methods is a comma-list of JSON RPC method declarations. Each declaration takes one of the forms below:
Callable:Reply
Here, Callable is a Prolog callable term whose name and number of argument match a predicate in this module. The arguments are JSON Schema types and Reply is a JSON Schema type.
Callable
Callable is as above, but there is no return value. This implements JSON RPC notifications, i.e., asynchronously processed messages for which we do not wait for a reply.

For example:

:- json_method
    subtract(#{type:number}, #{type:number}): #{type:number}.

subtract(A, B, R) :- R is A-B.

Methods with named arguments can be implemented using a single argument that is an object with specified properties. For example, the program below implements a depositing to a bank account. The method takes an account and amount parameter and returns the new balance. The json_rpc_error/2 throws a JSON RPC application error.

:- json_method
    deposit(#{ properties:
               #{ account: #{type:string},
                  amount:  #{type:number}
                }}): #{type:number},

deposit(Request, Reply),
    #{account: Account, amount: Amount} :< Request =>
    transaction((   retract(account(Account, Old))
                ->  New is Old+Amount,
                    asserta(account(Account, New))
                ;   json_rpc_error(2, "Account does not exist")
                )),
    Reply = New.