Use Jackson for JSON-processing. If you convert your results to a POJO simply make the POJO Jackson compatible (getters will be serialized automatically for instance or use @JsonProperty
.
Example for converting a pojo to JSON:
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(somePojo);
If you do not convert your results to a POJO the JsonNode
subclass called ObjectNode
can be used.
Example:
public String convert(ResultSet rs) {
ObjectNode node = new ObjectMapper().createObjectNode();
node.put("fieldName", rs.getString("columnName"));
return node.toString(); // this is proper JSON
}
However, the most common and clean approach is to return a POJO from your function (whether it is an EJB or a REST service or similar) and then let the framework convert it to JSON for you (typically the framework uses Jackson). This means that your method simply returns some kind of model object that is Jackson compatible.