Working with JSON objects
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
JsonObjectobjects - Lists of scalars or
JsonObjectobjects
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.
- Use standard map operations such as
get, put, andcontainsKey - 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)
}
- The startBatchProcess method takes a single
JsonObjectobject as input. - The startBatchProcess method performs standard map
getoperations on the passed-in JSON object. - The getWorkQueue method returns a
JsonObjectobject. - The getWorkQueue method performs standard map
putoperations 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)
}
}
- The startBatchProcess method takes a single
JsonWrapperobject as input. - The startBatchProcess method uses standard methods on
ProcessStartParamsto perform business logic.
