diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/edit/MenuManagementEdit.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/edit/MenuManagementEdit.java index 978f35f3c..f4a18ee63 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/edit/MenuManagementEdit.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/edit/MenuManagementEdit.java @@ -12,9 +12,10 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +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.JSONObject; import org.apache.jena.ontology.OntModel; import org.apache.jena.rdf.model.Model; @@ -139,7 +140,7 @@ public class MenuManagementEdit extends VitroHttpServlet { private void sendReorderResponse(String errorMessage, HttpServletResponse resp) { try{ - JSONObject rObj = new JSONObject(); + ObjectNode rObj = JsonNodeFactory.instance.objectNode(); resp.setCharacterEncoding("UTF-8"); resp.setContentType("application/json;charset=UTF-8"); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individual/IndividualResponseBuilder.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individual/IndividualResponseBuilder.java index 77304b2d6..6cc9096fc 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individual/IndividualResponseBuilder.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individual/IndividualResponseBuilder.java @@ -11,7 +11,6 @@ import org.apache.jena.rdf.model.RDFNode; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONException; import org.apache.jena.query.QuerySolution; import org.apache.jena.query.ResultSet; @@ -133,8 +132,6 @@ class IndividualResponseBuilder { if (openSocialManager.isVisible()) { body.put("bodyOnload", "my.init();"); } - } catch (JSONException e) { - log.error("JSONException in doTemplate()", e); } catch (IOException e) { log.error("IOException in doTemplate()", e); } catch (SQLException e) { diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java index e109e577c..314506cb3 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java @@ -5,9 +5,10 @@ package edu.cornell.mannlib.vitro.webapp.controller.individuallist; import java.util.Collection; import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; - +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; @@ -26,19 +27,22 @@ public class IndividualJsonWrapper { addJSONFields = add; } - static JSONObject packageIndividualAsJson(VitroRequest vreq, Individual ind) - throws JSONException { + static ObjectNode packageIndividualAsJson(VitroRequest vreq, Individual ind) { // need an unfiltered dao to get firstnames and lastnames WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory(); - JSONObject jo = new JSONObject(); + ObjectNode jo = JsonNodeFactory.instance.objectNode(); jo.put("URI", ind.getURI()); jo.put("label", ind.getRdfsLabel()); jo.put("name", ind.getName()); jo.put("thumbUrl", ind.getThumbUrl()); jo.put("imageUrl", ind.getImageUrl()); jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); - jo.put("mostSpecificTypes", getMostSpecificTypes(ind, fullWdf)); + ArrayNode ao = JsonNodeFactory.instance.arrayNode(); + for (String type : getMostSpecificTypes(ind, fullWdf)) { + ao.add(type); + } + jo.put("mostSpecificTypes", ao); if (addJSONFields != null) { addJSONFields.add(jo, vreq, ind); } @@ -55,6 +59,6 @@ public class IndividualJsonWrapper { } public interface AddJSONFields { - public void add(JSONObject jo, VitroRequest vreq, Individual ind) throws JSONException; + public void add(ObjectNode jo, VitroRequest vreq, Individual ind); } } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java index 78ac15163..b25e95e66 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java @@ -8,12 +8,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.ObjectMapper; +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.ArrayUtils; 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; @@ -34,9 +35,10 @@ public class IndividualListResultsUtils { * Process results related to VClass or vclasses. Handles both single and * multiple vclasses being sent. */ - public static JSONObject wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq, - boolean multipleVclasses) { - JSONObject rObj = new JSONObject(); + public static ObjectNode wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq, + boolean multipleVclasses) { + + ObjectNode rObj = JsonNodeFactory.instance.objectNode(); if (log.isDebugEnabled()) { dumpParametersFromRequest(vreq); @@ -91,47 +93,44 @@ public class IndividualListResultsUtils { return list; } - private static JSONObject packageVClassAsJson(VClass vclass) - throws JSONException { - JSONObject jvclass = new JSONObject(); + private static ObjectNode packageVClassAsJson(VClass vclass) { + ObjectNode jvclass = JsonNodeFactory.instance.objectNode(); jvclass.put("URI", vclass.getURI()); jvclass.put("name", vclass.getName()); return jvclass; } - private static JSONArray packageLettersAsJson() throws JSONException, - UnsupportedEncodingException { + private static ArrayNode packageLettersAsJson() throws UnsupportedEncodingException { List letters = Controllers.getLetters(); - JSONArray jletters = new JSONArray(); + ArrayNode jletters = JsonNodeFactory.instance.arrayNode(); for (String s : letters) { - JSONObject jo = new JSONObject(); + ObjectNode jo = JsonNodeFactory.instance.objectNode(); jo.put("text", s); jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); - jletters.put(jo); + jletters.add(jo); } return jletters; } - private static JSONArray packagePageRecordsAsJson(List pages) - throws JSONException { - JSONArray wpages = new JSONArray(); + private static ArrayNode packagePageRecordsAsJson(List pages) { + ArrayNode wpages = JsonNodeFactory.instance.arrayNode(); for (PageRecord pr : pages) { - JSONObject p = new JSONObject(); + ObjectNode p = JsonNodeFactory.instance.objectNode(); p.put("text", pr.text); p.put("param", pr.param); p.put("index", pr.index); - wpages.put(p); + wpages.add(p); } return wpages; } - private static JSONArray packageIndividualsAsJson(VitroRequest vreq, - List inds) throws JSONException { + private static ArrayNode packageIndividualsAsJson(VitroRequest vreq, + List inds) { log.debug("Number of individuals returned from request: " + inds.size()); - JSONArray jInds = new JSONArray(); + ArrayNode jInds = JsonNodeFactory.instance.arrayNode(); for (Individual ind : inds) { - jInds.put(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind)); + jInds.add(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind)); } return jInds; } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetAllVClasses.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetAllVClasses.java index 3d72a2ac9..02ea45bd6 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetAllVClasses.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetAllVClasses.java @@ -6,9 +6,12 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import com.fasterxml.jackson.databind.ObjectMapper; +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.JSONObject; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; @@ -28,8 +31,8 @@ public class GetAllVClasses extends JsonObjectProducer { } @Override - protected JSONObject process() throws Exception { - JSONObject map = new JSONObject(); + protected ObjectNode process() throws Exception { + ObjectNode map = JsonNodeFactory.instance.objectNode(); //Get all VClassGroups List vclasses = new ArrayList(); VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); @@ -43,10 +46,10 @@ public class GetAllVClasses extends JsonObjectProducer { //Sort vclass by name Collections.sort(vclasses); - ArrayList classes = new ArrayList(vclasses.size()); + ArrayNode classes = JsonNodeFactory.instance.arrayNode(); for(VClass vc: vclasses) { - JSONObject vcObj = new JSONObject(); + ObjectNode vcObj = JsonNodeFactory.instance.objectNode(); vcObj.put("name", vc.getName()); vcObj.put("URI", vc.getURI()); classes.add(vcObj); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRandomSearchIndividualsByVClass.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRandomSearchIndividualsByVClass.java index 98e6c9d53..c21fb3a88 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRandomSearchIndividualsByVClass.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRandomSearchIndividualsByVClass.java @@ -6,12 +6,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; 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; @@ -37,8 +36,8 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC * Search for individuals by VClass. */ @Override - protected JSONObject process() throws Exception { - JSONObject rObj = null; + protected ObjectNode process() throws Exception { + ObjectNode rObj = null; //This gets the first vclass value and sets that as display type. List vclassIds = super.getVclassIds(vreq); @@ -57,13 +56,13 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC * Look through the return object. For each individual, render the short * view and insert the resulting HTML into the object. */ - private void addShortViewRenderings(JSONObject rObj) throws JSONException { - JSONArray individuals = rObj.getJSONArray("individuals"); - String vclassName = rObj.getJSONObject("vclass").getString("name"); - for (int i = 0; i < individuals.length(); i++) { - JSONObject individual = individuals.getJSONObject(i); + private void addShortViewRenderings(ObjectNode rObj) { + ArrayNode individuals = (ArrayNode) rObj.get("individuals"); + String vclassName = rObj.get("vclass").get("name").asText(); + for (int i = 0; i < individuals.size(); i++) { + ObjectNode individual = (ObjectNode) individuals.get(i); individual.put("shortViewHtml", - renderShortView(individual.getString("URI"), vclassName)); + renderShortView(individual.get("URI").asText(), vclassName)); } } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRenderedSearchIndividualsByVClass.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRenderedSearchIndividualsByVClass.java index 7c606c1ed..1befb3448 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRenderedSearchIndividualsByVClass.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetRenderedSearchIndividualsByVClass.java @@ -6,12 +6,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; 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; @@ -38,8 +37,8 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy * information are in the request parameters. */ @Override - protected JSONObject process() throws Exception { - JSONObject rObj = null; + protected ObjectNode process() throws Exception { + ObjectNode rObj = null; //This gets the first vclass value and sets that as display type List vclassIds = super.getVclassIds(vreq); @@ -65,13 +64,13 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy * Look through the return object. For each individual, render the short * view and insert the resulting HTML into the object. */ - private void addShortViewRenderings(JSONObject rObj) throws JSONException { - JSONArray individuals = rObj.getJSONArray("individuals"); - String vclassName = rObj.getJSONObject("vclass").getString("name"); - for (int i = 0; i < individuals.length(); i++) { - JSONObject individual = individuals.getJSONObject(i); + private void addShortViewRenderings(ObjectNode rObj) { + ArrayNode individuals = (ArrayNode) rObj.get("individuals"); + String vclassName = rObj.get("vclass").get("name").asText(); + for (int i = 0; i < individuals.size(); i++) { + ObjectNode individual = (ObjectNode) individuals.get(i); individual.put("shortViewHtml", - renderShortView(individual.getString("URI"), vclassName)); + renderShortView(individual.get("URI").asText(), vclassName)); } } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClass.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClass.java index a861d0106..8dd55b20f 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClass.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClass.java @@ -2,9 +2,10 @@ package edu.cornell.mannlib.vitro.webapp.controller.json; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; @@ -21,8 +22,8 @@ public class GetSearchIndividualsByVClass extends JsonObjectProducer { } @Override - protected JSONObject process() throws Exception { - VClass vclass=null; + protected ObjectNode process() throws Exception { + VClass vclass = null; String queryType = (String) vreq.getAttribute("queryType"); String vitroClassIdStr = vreq.getParameter("vclassId"); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClasses.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClasses.java index 77da4a50a..f973d12f0 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClasses.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetSearchIndividualsByVClasses.java @@ -5,9 +5,9 @@ package edu.cornell.mannlib.vitro.webapp.controller.json; import java.util.Arrays; import java.util.List; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; @@ -25,7 +25,7 @@ public class GetSearchIndividualsByVClasses extends JsonObjectProducer { } @Override - protected JSONObject process() throws Exception { + protected ObjectNode process() throws Exception { log.debug("Executing retrieval of individuals by vclasses"); VClass vclass=null; log.debug("Retrieving search individuals by vclasses"); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetVClassesForVClassGroup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetVClassesForVClassGroup.java index 4f1dbdfdf..1af8cfb4f 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetVClassesForVClassGroup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/GetVClassesForVClassGroup.java @@ -4,9 +4,11 @@ package edu.cornell.mannlib.vitro.webapp.controller.json; import java.util.ArrayList; +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.JSONObject; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; @@ -26,8 +28,8 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer { } @Override - protected JSONObject process() throws Exception { - JSONObject map = new JSONObject(); + protected ObjectNode process() throws Exception { + ObjectNode map = JsonNodeFactory.instance.objectNode(); String vcgUri = vreq.getParameter("classgroupUri"); if( vcgUri == null ){ throw new Exception("no URI passed for classgroupUri"); @@ -38,10 +40,10 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer { if( vcg == null ){ throw new Exception("Could not find vclassgroup: " + vcgUri); } - - ArrayList classes = new ArrayList(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()); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonObjectProducer.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonObjectProducer.java index 43a5dba94..bece7fad8 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonObjectProducer.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonObjectProducer.java @@ -8,10 +8,12 @@ import java.io.Writer; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +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.JSONException; -import org.json.JSONObject; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; @@ -42,10 +44,10 @@ public abstract class JsonObjectProducer extends JsonProducer { * Sub-classes implement this method. Given the request, produce a JSON * object as the result. */ - protected abstract JSONObject process() throws Exception; + protected abstract ObjectNode process() throws Exception; public final void process(HttpServletResponse resp) throws IOException { - JSONObject jsonObject = null; + ObjectNode jsonObject = null; String errorMessage = ""; try { @@ -57,20 +59,16 @@ public abstract class JsonObjectProducer extends JsonProducer { } if (jsonObject == null) { - jsonObject = new JSONObject(); + jsonObject = JsonNodeFactory.instance.objectNode(); } log.debug("Response to JSON request: " + jsonObject.toString()); - try { - resp.setCharacterEncoding("UTF-8"); - resp.setContentType("application/json;charset=UTF-8"); - Writer writer = resp.getWriter(); + resp.setCharacterEncoding("UTF-8"); + resp.setContentType("application/json;charset=UTF-8"); + Writer writer = resp.getWriter(); - jsonObject.put("errorMessage", errorMessage); - writer.write(jsonObject.toString()); - } catch (JSONException e) { - log.error(e, e); - } + jsonObject.put("errorMessage", errorMessage); + writer.write(jsonObject.toString()); } } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java index 5e67f4d56..9f7c46afd 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java @@ -11,9 +11,9 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.Individual; @@ -82,7 +82,7 @@ public class JsonServlet extends VitroHttpServlet { } - public static JSONObject getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { + public static ObjectNode getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { List vclassURIs = Collections.singletonList(vclassURI); VitroRequest vreq = new VitroRequest(req); @@ -91,7 +91,7 @@ public class JsonServlet extends VitroHttpServlet { return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false); } - public static JSONObject getSearchIndividualsByVClasses(List vclassURIs, HttpServletRequest req) throws Exception { + public static ObjectNode getSearchIndividualsByVClasses(List vclassURIs, HttpServletRequest req) throws Exception { VitroRequest vreq = new VitroRequest(req); log.debug("Retrieve search results for vclasses" + vclassURIs.toString()); IndividualListResults vcResults = getSearchVClassIntersectionResults(vclassURIs, vreq); @@ -126,7 +126,7 @@ public class JsonServlet extends VitroHttpServlet { return value; } - public static JSONObject getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { + public static ObjectNode getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { VitroRequest vreq = new VitroRequest(req); IndividualListResults vcResults = getRandomSearchVClassResults(vclassURI, vreq); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/JsonToFmModel.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/JsonToFmModel.java index d7da6488a..caccca279 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/JsonToFmModel.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/JsonToFmModel.java @@ -28,10 +28,9 @@ import java.util.SortedMap; import java.util.TreeMap; //import java.util.regex.Pattern; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; //import org.springframework.extensions.surf.util.ISO8601DateFormat; /** @@ -49,44 +48,27 @@ public final class JsonToFmModel // // public static boolean autoConvertISO8601 = true; - /** - * Convert JSON Object string to Freemarker-compatible data model - * - * @param jsonString JSON string - * @return model - * @throws JSONException - */ - public static Map convertJSONObjectToMap(String jsonString) throws JSONException - { - JSONObject jo = new JSONObject(new JSONTokener(jsonString)); - return convertJSONObjectToMap(jo); - } - /** * JSONObject is an unordered collection of name/value pairs -> convert to Map (equivalent to Freemarker "hash") */ @SuppressWarnings("unchecked") - public static Map convertJSONObjectToMap(JSONObject jo) throws JSONException + public static Map convertJSONObjectToMap(ObjectNode jo) { Map model = new HashMap(); - - Iterator itr = (Iterator)jo.keys(); + + Iterator itr = (Iterator)jo.fieldNames(); while (itr.hasNext()) { String key = (String)itr.next(); Object o = jo.get(key); - if (o instanceof JSONObject) + if (o instanceof ObjectNode) { - model.put(key, convertJSONObjectToMap((JSONObject)o)); + model.put(key, convertJSONObjectToMap((ObjectNode)o)); } - else if (o instanceof JSONArray) + else if (o instanceof ArrayNode) { - model.put(key, convertJSONArrayToList((JSONArray)o)); - } - else if (o == JSONObject.NULL) - { - model.put(key, null); // note: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing + model.put(key, convertJSONArrayToList((ArrayNode)o)); } else { @@ -102,43 +84,24 @@ public final class JsonToFmModel return model; } - /** - * Convert JSON Array string to Freemarker-compatible data model - * - * @param jsonString JSON String - * @return model - * @throws JSONException - */ - public static Map convertJSONArrayToMap(String jsonString) throws JSONException - { - Map model = new HashMap(); - JSONArray ja = new JSONArray(new JSONTokener(jsonString)); - model.put(ROOT_ARRAY, convertJSONArrayToList(ja)); - return model; - } - /** * JSONArray is an ordered sequence of values -> convert to List (equivalent to Freemarker "sequence") */ - public static List convertJSONArrayToList(JSONArray ja) throws JSONException + public static List convertJSONArrayToList(ArrayNode ja) { List model = new ArrayList(); - - for (int i = 0; i < ja.length(); i++) + + for (int i = 0; i < ja.size(); i++) { - Object o = ja.get(i); - - if (o instanceof JSONArray) + JsonNode o = ja.get(i); + + if (o instanceof ArrayNode) { - model.add(convertJSONArrayToList((JSONArray)o)); + model.add(convertJSONArrayToList((ArrayNode) o)); } - else if (o instanceof JSONObject) + else if (o instanceof ObjectNode) { - model.add(convertJSONObjectToMap((JSONObject)o)); - } - else if (o == JSONObject.NULL) - { - model.add(null); + model.add(convertJSONObjectToMap((ObjectNode) o)); } else { diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/BrowseDataGetter.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/BrowseDataGetter.java index d9df76d71..7e4e83bcb 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/BrowseDataGetter.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/BrowseDataGetter.java @@ -11,11 +11,11 @@ import java.util.Map; import javax.servlet.ServletContext; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONArray; -import org.json.JSONObject; import org.apache.jena.rdf.model.Model; @@ -109,12 +109,12 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter { VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri); map.put("class", new VClassTemplateModel(vclass)); - JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); - map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") )); - map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") )); - map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") )); - map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") )); - map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") )); + ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); + map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") )); + map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") )); + map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") )); + map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") )); + map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") )); return map; } @@ -253,10 +253,7 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter { /** * For processig of JSONObject */ - public JSONObject convertToJSON(Map dataMap, VitroRequest vreq) { - JSONObject rObj = null; - return rObj; + public ObjectNode convertToJSON(Map dataMap, VitroRequest vreq) { + return null; } - - } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/web/widgets/BrowseWidget.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/web/widgets/BrowseWidget.java index f324059a0..4a0ccf77b 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/web/widgets/BrowseWidget.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/web/widgets/BrowseWidget.java @@ -11,11 +11,11 @@ import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder; 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.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.VClass; @@ -84,12 +84,12 @@ public class BrowseWidget extends Widget { VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri); map.put("class", new VClassTemplateModel(vclass)); - JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); - map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") )); - map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") )); - map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") )); - map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") )); - map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") )); + ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); + map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") )); + map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") )); + map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") )); + map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") )); + map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") )); return map; } diff --git a/api/src/main/java/edu/ucsf/vitro/opensocial/OpenSocialManager.java b/api/src/main/java/edu/ucsf/vitro/opensocial/OpenSocialManager.java index a7968ce7e..a31393758 100644 --- a/api/src/main/java/edu/ucsf/vitro/opensocial/OpenSocialManager.java +++ b/api/src/main/java/edu/ucsf/vitro/opensocial/OpenSocialManager.java @@ -15,11 +15,12 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONException; -import org.json.JSONObject; import edu.cornell.mannlib.vedit.beans.LoginStatusBean; import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers; @@ -189,20 +190,26 @@ public class OpenSocialManager { // JSON Helper Functions public static String buildJSONPersonIds(List personIds, - String message) throws JSONException { - JSONObject json = new JSONObject(); + String message) { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode json = mapper.createObjectNode(); json.put("message", message); - json.put("personIds", personIds); + + ArrayNode persons = mapper.createArrayNode(); + for (String personId : personIds) { + persons.add(personId); + } + json.put("personIds", persons); return json.toString(); } - public static String buildJSONPersonIds(String personId, String message) throws JSONException { + public static String buildJSONPersonIds(String personId, String message) { List personIds = new ArrayList(); personIds.add(personId); return buildJSONPersonIds(personIds, message); } - public static String buildJSONPersonIds(Individual ind, String message) throws JSONException { + public static String buildJSONPersonIds(Individual ind, String message) { List personIds = new ArrayList(); personIds.add(ind.getURI()); return buildJSONPersonIds(personIds, message);