Author: mrdon Date: Thu Nov 8 01:17:34 2007 New Revision: 593088 URL: http://svn.apache.org/viewvc?rev=593088&view=rev Log: Added handling of json arrays and lists
Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java Modified: struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java?rev=593088&r1=593087&r2=593088&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java (original) +++ struts/sandbox/trunk/struts2-rest-plugin/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java Thu Nov 8 01:17:34 2007 @@ -21,9 +21,11 @@ package org.apache.struts2.rest.handler; import java.io.*; +import java.util.Collection; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; +import net.sf.json.JSONArray; /** * Handles JSON content using json-lib @@ -37,18 +39,37 @@ while ((len = in.read(buffer)) > 0) { sb.append(buffer, 0, len); } - JSONObject jsonObject = JSONObject.fromObject(sb.toString()); - JSONObject.toBean(jsonObject, target, new JsonConfig()); + if (target != null && sb.length() > 0 && sb.charAt(0) == '[') { + JSONArray jsonArray = JSONArray.fromObject(sb.toString()); + if (target.getClass().isArray()) { + JSONArray.toArray(jsonArray, target, new JsonConfig()); + } else { + JSONArray.toList(jsonArray, target, new JsonConfig()); + } + + } else { + JSONObject jsonObject = JSONObject.fromObject(sb.toString()); + JSONObject.toBean(jsonObject, target, new JsonConfig()); + } } public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { if (obj != null) { - JSONObject jsonObject = JSONObject.fromObject(obj); - stream.write(jsonObject.toString()); + if (isArray(obj)) { + JSONArray jsonArray = JSONArray.fromObject(obj); + stream.write(jsonArray.toString()); + } else { + JSONObject jsonObject = JSONObject.fromObject(obj); + stream.write(jsonObject.toString()); + } } return null; + } + + private boolean isArray(Object obj) { + return obj instanceof Collection || obj.getClass().isArray(); } public String getContentType() { Modified: struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java?rev=593088&r1=593087&r2=593088&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java (original) +++ struts/sandbox/trunk/struts2-rest-plugin/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java Thu Nov 8 01:17:34 2007 @@ -25,6 +25,7 @@ import java.io.StringWriter; import java.io.IOException; import java.io.StringReader; +import java.util.Arrays; public class JsonLibHandlerTest extends TestCase { @@ -36,6 +37,16 @@ handler.fromObject(contact, "success", writer); assertEquals("{\"age\":44,\"important\":true,\"name\":\"bob\"}", writer.toString()); + } + + public void testFromObjectArray() throws IOException { + Contact contact = new Contact("bob", true, 44); + + StringWriter writer = new StringWriter(); + JsonLibHandler handler = new JsonLibHandler(); + handler.fromObject(Arrays.asList(contact), "success", writer); + + assertEquals("[{\"age\":44,\"important\":true,\"name\":\"bob\"}]", writer.toString()); } public void testToObject() throws IOException {