Documentation‎ > ‎

JSON-RPC

Once you have created methods in Persevere, you can invoke the methods using JSON-RPC, which supports a wide range of arguments. JSON-RPC requests can be issued by sending a POST request with the body being a JSON-RPC request. The URI for the request is used to resolve the target object for the method call. For example, in order to call the getFullName method of /Persion/4 with no arguments, you could make the following HTTP request:
POST /Person/4 HTTP/1.1
Content-Length: 30

{method:"getFullName", id:"1", params:[]}
The value returned by the target function will be in response object in the result property:
{"result":"John Doe", "error":null, id:"1"}
If the function throws an uncaught exception before returning the exception will be included in response object error property:
{"result":null, "error":"Failed to create the full name", id:"1"}
Note that the JSON-RPC specification requires an id property to be included to correlate the call message with the return message. At the JSON-RPC level, the id property does not indicate the identity of an object as with Persevere persisted objects. The id of "1" used in this example does indicate an object with an id of "1", but rather simply means the response will have an id of "1". However, within the params and response property values you can reference and include objects with ids.

To setup methods to be executed via JSON-RPC, you must define the methods in the table/class configuration. For example, the getFullName method as executed above, you might have a configuration file /WEB-INF/jslib/person.js:
Class({
            "id":"Person",
            "prototype":{
                "getFullName": function(a,b){
                   return this.firstName + this.lastName;
                }
            },
             "createInstance": function(firstName, lastName){
                return new Person({firstName, lastName});
             }
            "properties":{                
            }   
});