Working with JSON objects

The runtime JSON API that you use to consume or produce JSON data is based on a PolicyCenter JsonObject object. The JsonObject object is basically a Map<String, Object>, the values for which can be one of the following:
  • Scalar POJO objects
  • Other JsonObject objects
  • Lists of scalars or JsonObject objects

These scalar values are the actual deserializer value, meaning BigDecimal objects or TypeKey objects, rather than just JavaScript primitive types. Thus, after the REST framework receives a request, the framework deserialization and validation of input happens in a single step. If the input does not validate against the schema or it is impossible to deserialize the input, the framework returns a 400 "bad input" error and does not invoke the API handler class.

It is possible to manipulate JSON input by doing the following:
  • Use standard map operations such as get, put, and containsKey
  • Use a generated schema wrapper class

The following code examples illustrate these concepts.

Example JSON manipulation with JsonObject

The following example code illustrates how to create a Gosu function to manipulate a JSON object.

// Function with body of type JsonObject
function startBatchProcess(body: JsonObject): Long {
  var processId: ProcessID
  var processName = body.get("processType") as String
  var args = body.get("args") as List<String>
  ...
}
 
// Function returns a JsonObject
function getWorkQueue(processType: String): JsonObject {
  try {
    var wQueueStatus = getDelegate().getWQueueStatus(processType)
    var jsonObject = new JsonObject()
    if (wQueueStatus != null) {
        jsonObject.put("processType", wQueueStatus.getQueueName())
        jsonObject.put("numActiveExecutors", wQueueStatus.getNumActiveExecutors())
        jsonObject.put("numOfActiveWorkItems", wQueueStatus.getNumActiveWorkItems())
      }
      return jsonObject  
  } catch (illegalArgumentException: IllegalArgumentException) {
    throw new NotFoundException("Invalid process type " + processType)
  }
Notice the following:
  • The startBatchProcess method takes a single JsonObject object as input.
  • The startBatchProcess method performs standard map get operations on the passed-in JSON object.
  • The getWorkQueue method returns a JsonObject object.
  • The getWorkQueue method performs standard map put operations to create the JSON object returned by the method.

Example JSON manipulation with JSONWrapper class

The following example code illustrates a Gosu function that takes a generated JSonWrapper class as a parameter.

uses jsonschema.gw.pl.system.maintenance_tools.v10_0.Workqueue
 
...
//Function with body of type ProcessStartParams class, which is one of the generated JsonWrapper classes
function startBatchProcess(body: ProcessStartParams): Long {
  var processId: ProcessID
  var processName = body.getprocessType()
  var args = body.getargs()
  ...
}
 
//Function returning JsonWrapper object
function getWorkQueue(processType: String): Workqueue {
  try {
    var wQueueStatus = getDelegate().getWQueueStatus(processType)
    var workqueue = new Workqueue ()
    if (wQueueStatus != null) {
        workqueue.setprocessType(wQueueStatus.getQueueName())
        workqueue.setnumActiveExecutors(wQueueStatus.getNumActiveExecutors())
        workqueue.setnumOfActiveWorkItems(wQueueStatus.getNumActiveWorkItems())
      }
      return workqueue
  } catch (illegalArgumentException: IllegalArgumentException) {
    throw new NotFoundException("Invalid process type " + processType)
  }
}
Notice the following:
  • The startBatchProcess method takes a single JsonWrapper object as input.
  • The startBatchProcess method uses standard methods on ProcessStartParams to perform business logic.