com.ibm.pdq.runtime.data.handlers
Class JSONResultHandler
java.lang.Object
com.ibm.pdq.runtime.data.handlers.JSONResultHandler
- All Implemented Interfaces:
- ResultHandler<String>
-
public class JSONResultHandler
- extends Object
- implements ResultHandler<String>
An implementation of ResultHandler
that can be specified to cause pureQuery to return content of the results of an SQL query as a String
in the JSON format, which is
described at http://www.json.org. JSONResultHandler
is provided to be an example of how a ResultHandler
may be written to process the contents of a
ResultSet
. See ResultHandler
for a
description of how an implementation of ResultHandler
can be specified to a pureQuery method.
The rows are returned by the JSONResultHandler
in the same order in which they are returned from the data source. An instance of JSONResultHandler
maintains no state
information. Therefore, a single instance of JSONResultHandler
can be used for more than one pureQuery method.
The returned String
, which is described at http://www.json.org, has the following format:
- There are no spaces, line breaks, or other white space.
- A "[" indicates the start of the
ResultSet
, and a "]" indicates the end of the ResultSet
.
- Inside the "[" and the "]", the rows are listed in the order that they are listed in the
ResultSet
. The start of each row is indicated by "{"
and the end of each row is indicated by "}". Adjacent rows are separated by ",".
- For each row, the columns are listed within the "{" and the "}" in the order that they are returned from the data source. Adjacent columns are separated by
",".
- Each column is returned as columnLabel:columnStringValue, where columnLabel is the column label that is returned from the data source, and
columnStringValue is a
String
representation of the contents of that column.
For example, suppose a data source returned the contents of three columns:
Suppose data was returned for four rows as follows:
column1 |
column2 |
column3 |
row1Column1Data |
row1Column2Data |
row1Column3Data |
row2Column1Data |
row2Column2Data |
row2Column3Data |
row3Column1Data |
row3Column2Data |
row3Column3Data |
row4Column1Data |
row4Column2Data |
row4Column3Data |
With this data, the String
returned by handle(ResultSet)
would be as follows (except that there would be no
spaces, line breaks, or other white space in the actual returned String
):
[
{"column1":"row1Column1Data","column2":"row1Column2Data","column3":"row1Column3Data"},
{"column1":"row2Column1Data","column2":"row2Column2Data","column3":"row2Column3Data"},
{"column1":"row3Column1Data","column2":"row3Column2Data","column3":"row3Column3Data"},
{"column1":"row4Column1Data","column2":"row4Column2Data","column3":"row4Column3Data"}
]
Constructor Summary
Method Summary
Method Summary Return Data Type | Method Name and Description |
String |
handle(ResultSet rs)
Returns the results of an SQL statement as a String in the JSON format. |
JSONResultHandler
public JSONResultHandler()
handle
public String handle(ResultSet rs)
-
Returns the results of an SQL statement as a
String
in the JSON format.
See JSONResultHandler
for a
description of the JSON format and for an example of the JSON format representation of a ResultSet
.
-
- Specified by:
handle
in interface ResultHandler<String>
-
- Parameters:
rs
- a ResultSet
that represents the
results from an SQL statement
- Returns:
- a
String
that contains the entire contents of resultSet
in the JSON format