[VIVO-1320] Convert some org.json usages to Jackson

This commit is contained in:
Graham Triggs 2017-09-15 18:30:53 +01:00
parent 7c1390d745
commit 916640ef43
5 changed files with 33 additions and 28 deletions

View file

@ -16,11 +16,12 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
@ -148,10 +149,10 @@ public class AutocompleteController extends VitroAjaxController {
Collections.sort(results);
JSONArray jsonArray = new JSONArray();
ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
for (SearchResult result : results) {
//jsonArray.put(result.toMap());
jsonArray.put(result.toJSONObject());
jsonArray.add(result.toJSONObject());
}
response.getWriter().write(jsonArray.toString());
@ -356,8 +357,8 @@ public class AutocompleteController extends VitroAjaxController {
//Simply passing in the array in the map converts it to a string and not to an array
//which is what we want so need to convert to an object instad
JSONObject toJSONObject() {
JSONObject jsonObj = new JSONObject();
ObjectNode toJSONObject() {
ObjectNode jsonObj = JsonNodeFactory.instance.objectNode();
try {
jsonObj.put("label", label);
jsonObj.put("uri", uri);
@ -365,7 +366,10 @@ public class AutocompleteController extends VitroAjaxController {
//But this should really be changed so that the entire array is all that should be returned
jsonObj.put("msType", msType);
//map.put("allMsTypes", allMsTypes);
JSONArray allMsTypesArray = new JSONArray(allMsTypes);
ArrayNode allMsTypesArray = JsonNodeFactory.instance.arrayNode();
for (String msType : allMsTypes) {
allMsTypesArray.add(msType);
}
jsonObj.put("allMsTypes", allMsTypesArray);
} catch(Exception ex) {
log.error("Error occurred in converting values to JSON object", ex);

View file

@ -12,9 +12,10 @@ import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
@ -107,7 +108,10 @@ public class DataAutocompleteController extends VitroAjaxController {
String dataValue = dataLiteral.getString();
outputResults.add(dataValue);
}
JSONArray jsonArray = new JSONArray(outputResults);
ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
for (String res : outputResults) {
jsonArray.add(res);
}
try {
response.getWriter().write(jsonArray.toString());
} catch (Throwable e) {

View file

@ -8,9 +8,9 @@ import java.util.Map;
import javax.servlet.ServletContext;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.apache.jena.rdf.model.Model;
@ -180,9 +180,8 @@ public class ClassGroupPageData extends DataGetterBase implements DataGetter{
* For processing of JSONObject
*/
//Currently empty, TODO: Review requirements
public JSONObject convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
JSONObject rObj = null;
return rObj;
public JsonNode convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
return null;
}
protected static void setAllClassCountsToZero(VClassGroup vcg){
for(VClass vc : vcg){

View file

@ -10,10 +10,12 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -313,17 +315,14 @@ public class DataGetterUtils {
*
* Convert data to JSON for page uri based on type and related datagetters
* TODO: How to handle different data getters? Will this replace json fields or add to them?
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static JSONObject covertDataToJSONForPage(VitroRequest vreq, String pageUri, Model displayModel) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
public static ObjectNode covertDataToJSONForPage(VitroRequest vreq, String pageUri, Model displayModel) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
//Get PageDataGetter types associated with pageUri
JSONObject rObj = null;
ObjectNode rObj = null;
try{
List<DataGetter> dataGetters = getDataGettersForPage(vreq, displayModel, pageUri);
for(DataGetter getter: dataGetters) {
JSONObject typeObj = null;
ObjectNode typeObj = null;
try{
//Assumes the data getter itself will have a convert to json method
/*
@ -394,12 +393,12 @@ public class DataGetterUtils {
/*
* Copied from JSONServlet as expect this to be related to VitroClassGroup
*/
public static JSONObject processVClassGroupJSON(VClassGroup vcg) {
JSONObject map = new JSONObject();
public static ObjectNode processVClassGroupJSON(VClassGroup vcg) {
ObjectNode map = JsonNodeFactory.instance.objectNode();
try {
ArrayList<JSONObject> classes = new ArrayList<JSONObject>(vcg.size());
ArrayNode classes = JsonNodeFactory.instance.arrayNode();
for( VClass vc : vcg){
JSONObject vcObj = new JSONObject();
ObjectNode vcObj = JsonNodeFactory.instance.objectNode();
vcObj.put("name", vc.getName());
vcObj.put("URI", vc.getURI());
vcObj.put("entityCount", vc.getEntityCount());

View file

@ -11,10 +11,10 @@ import java.util.Map;
import javax.servlet.ServletContext;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
@ -359,9 +359,8 @@ public class IndividualsForClassesDataGetter extends DataGetterBase implements D
/**
* For processig of JSONObject
*/
public JSONObject convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
JSONObject rObj = null;
return rObj;
public JsonNode convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
return null;
}
protected static void setAllClassCountsToZero(VClassGroup vcg){