1 Supporting JSON
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog JSON library
        • Supporting JSON
          • library(json): Reading and writing JSON serialization
          • library(json_schema): JSON Schema reader and validator
          • library(json_rpc_client): JSON RPC client
            • json_call/4
            • json_notify/3
            • json_batch/5
            • json_full_duplex/2
          • library(json_rpc_server): JSON RPC Server
          • library(json_convert): Convert between JSON terms and Prolog application terms
          • library(http/http_json): HTTP JSON Plugin module

1.3 library(json_rpc_client): JSON RPC client

This module implements a JSON RPC compliant client. The three predicates require a stream pair (see stream_pair/2) that connects us to a JSON RPC server.

[det]json_call(+Stream, +Goal, -Result, +Options)
Run Goal on a JSON RPC service identified by Stream and wait for Result. This predicate may be called from multiple threads. As replies come in in arbitrary order, this predicate starts a thread the reads the replies from Stream and informs the calling thread using a Prolog message queue.

If Stream is closed this library terminates the thread and related message queue.

Goal is a callable term. The functor name is the method. If there is a single argument that is a dict, we invoke a JSON-RPC method using named arguments. If there is a single argument that is a list, use the elements of the list as positional arguments. If there are zero or more than one arguments use these as positional arguments. Examples:
TermMethodTypeJSON (params)


f(#{a:1,b:2} fnamed{"a":1, "b":2}
f(["a", 42]) fpositional["a", 42]
f([#{"a":1}])fpositional[{"a":1}]
f() fpositional[]
f("a", 42) fpositional["a", 42]

Options processed:

[det]json_notify(+Stream, +Goal, +Options)
Run Goal on a JSON RPC service identified by Stream without waiting for the result.
[det]json_batch(+Stream, +Notifications:list, +Calls:list, -Results:list, +Options)
Run a batch of notifications and normal calls on the JSON server at the other end of Stream. On success, Result is unified to a list with the same length as Calls. Each element either contains a value, similar to json_call/4 or a term error(Dict), where Dict holds the code, message and optional data field. Note that error(Dict) is not a valid JSON type and this is thus unambiguous. While the JSON RPC standard allows the server to process the messages in any order and allows for concurrent processing, all results are sent in one message and this client ensures the elements of the Results list are in the same order as the Calls list. If the Calls list is empty this predicate does not wait for a reply.
[det]json_full_duplex(+Stream, :Options)
Start the thread for incomming data and on requests, dispatch them using library(jso_rpc_server) in the module derived from the Options list.