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

This commit is contained in:
Graham Triggs 2017-09-15 16:28:24 +01:00
parent 826fb9c570
commit 5175ef9c50
4 changed files with 47 additions and 58 deletions

View file

@ -11,11 +11,11 @@ import java.util.ListIterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
@ -50,7 +50,7 @@ public class GetEntitiesByVClass extends JsonArrayProducer {
}
@Override
protected JSONArray process() throws ServletException {
protected ArrayNode process() throws ServletException {
log.debug("in getEntitiesByVClass()");
String vclassURI = vreq.getParameter("vclassURI");
WebappDaoFactory daos = vreq.getUnfilteredWebappDaoFactory();
@ -96,26 +96,22 @@ public class GetEntitiesByVClass extends JsonArrayProducer {
//put all the entities on the JSON array
JSONArray ja = individualsToJson( entsToReturn );
ArrayNode ja = individualsToJson( entsToReturn );
//put the responseGroup number on the end of the JSON array
if( more ){
try{
JSONObject obj = new JSONObject();
obj.put("resultGroup", "true");
obj.put("size", count);
obj.put("total", numberOfEntsInVClass);
ObjectNode obj = JsonNodeFactory.instance.objectNode();
obj.put("resultGroup", "true");
obj.put("size", count);
obj.put("total", numberOfEntsInVClass);
StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( requestHash );
obj.put("nextUrl", nextUrlStr.toString());
StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( requestHash );
obj.put("nextUrl", nextUrlStr.toString());
ja.put(obj);
}catch(JSONException je ){
throw new ServletException("unable to create continuation as JSON: " + je.getMessage());
}
ja.add(obj);
}
log.debug("done with getEntitiesByVClass()");

View file

@ -11,11 +11,11 @@ import java.util.ListIterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -32,7 +32,7 @@ public class GetEntitiesByVClassContinuation extends JsonArrayProducer {
}
@Override
protected JSONArray process() throws ServletException {
protected ArrayNode process() throws ServletException {
log.debug("in getEntitiesByVClassContinuation()");
String resKey = vreq.getParameter("resultKey");
if( resKey == null )
@ -68,26 +68,22 @@ public class GetEntitiesByVClassContinuation extends JsonArrayProducer {
}
//put all the entities on the JSON array
JSONArray ja = individualsToJson( entsToReturn );
ArrayNode ja = individualsToJson( entsToReturn );
//put the responseGroup number on the end of the JSON array
if( more ){
try{
JSONObject obj = new JSONObject();
obj.put("resultGroup", "true");
obj.put("size", count);
ObjectNode obj = JsonNodeFactory.instance.objectNode();
obj.put("resultGroup", "true");
obj.put("size", count);
StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( resKey );
obj.put("nextUrl", nextUrlStr.toString());
StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( resKey );
obj.put("nextUrl", nextUrlStr.toString());
ja.put(obj);
}catch(JSONException je ){
throw new ServletException(je.getMessage());
}
}
ja.add(obj);
}
log.debug("done with getEntitiesByVClassContinuation()");
return ja;
}

View file

@ -8,9 +8,10 @@ import java.io.Writer;
import javax.servlet.ServletContext;
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 edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -35,10 +36,10 @@ public abstract class JsonArrayProducer extends JsonProducer {
* Sub-classes implement this method. Given the request, produce a JSON
* object as the result.
*/
protected abstract JSONArray process() throws Exception;
protected abstract ArrayNode process() throws Exception;
public final void process(HttpServletResponse resp) throws IOException {
JSONArray jsonArray = null;
ArrayNode jsonArray = null;
try {
jsonArray = process();
} catch (Exception e) {
@ -47,7 +48,7 @@ public abstract class JsonArrayProducer extends JsonProducer {
}
if (jsonArray == null) {
jsonArray = new JSONArray();
jsonArray = JsonNodeFactory.instance.arrayNode();
}
log.debug("Response to JSON request: " + jsonArray.toString());

View file

@ -7,12 +7,12 @@ import java.util.List;
import javax.servlet.ServletException;
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.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
@ -27,19 +27,15 @@ public abstract class JsonProducer {
/**
* Process a list of Individuals into a JSON array that holds the Names and URIs.
*/
protected JSONArray individualsToJson(List<Individual> individuals) throws ServletException {
try{
JSONArray ja = new JSONArray();
for (Individual ent: individuals) {
JSONObject entJ = new JSONObject();
entJ.put("name", ent.getName());
entJ.put("URI", ent.getURI());
ja.put( entJ );
}
return ja;
}catch(JSONException ex){
throw new ServletException("could not convert list of Individuals into JSON: " + ex);
}
protected ArrayNode individualsToJson(List<Individual> individuals) throws ServletException {
ArrayNode ja = JsonNodeFactory.instance.arrayNode();
for (Individual ent: individuals) {
ObjectNode entJ = JsonNodeFactory.instance.objectNode();
entJ.put("name", ent.getName());
entJ.put("URI", ent.getURI());
ja.add( entJ );
}
return ja;
}
/**