Repository: struts
Updated Branches:
  refs/heads/develop 1f333997f -> da4a22078


improved interceptor that accepts and maps JSON array


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/61cffe68
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/61cffe68
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/61cffe68

Branch: refs/heads/develop
Commit: 61cffe68feda8a4c527d085647e9567b2f689fc7
Parents: 381e927
Author: Aleksandr Mashchenko <[email protected]>
Authored: Mon Oct 13 20:07:07 2014 +0300
Committer: Aleksandr Mashchenko <[email protected]>
Committed: Mon Oct 13 20:07:07 2014 +0300

----------------------------------------------------------------------
 .../apache/struts2/json/JSONInterceptor.java    | 22 ++++++++-
 .../org/apache/struts2/json/AnotherBean.java    | 32 ++++++++++++
 .../struts2/json/JSONInterceptorTest.java       | 52 ++++++++++++++++++++
 .../org/apache/struts2/json/TestAction5.java    | 52 ++++++++++++++++++++
 .../org/apache/struts2/json/json-12.txt         |  8 +++
 5 files changed, 165 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/61cffe68/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java 
b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
index 69158bc..f802af9 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
@@ -87,8 +87,8 @@ public class JSONInterceptor extends AbstractInterceptor {
         }
 
         Object rootObject = null;
+        final ValueStack stack = invocation.getStack();
         if (this.root != null) {
-            ValueStack stack = invocation.getStack();
             rootObject = stack.findValue(this.root);
 
             if (rootObject == null) {
@@ -100,6 +100,26 @@ public class JSONInterceptor extends AbstractInterceptor {
             // load JSON object
             Object obj = JSONUtil.deserialize(request.getReader());
 
+            // JSON array (this.root cannot be null in this case)
+            if(obj instanceof List && this.root != null) {
+                String mapKey = this.root;
+                rootObject = null;
+
+                if(this.root.indexOf('.') != -1) {
+                    mapKey = this.root.substring(this.root.lastIndexOf('.') + 
1);
+
+                    rootObject = stack.findValue(this.root.substring(0, 
this.root.lastIndexOf('.')));
+                    if (rootObject == null) {
+                        throw new RuntimeException("JSON array: Invalid root 
expression: '" + this.root + "'.");
+                    }
+                }
+
+                // create a map with a list inside
+                Map m = new HashMap();
+                m.put(mapKey, new ArrayList((List) obj));
+                obj = m;
+            }
+
             if (obj instanceof Map) {
                 Map json = (Map) obj;
 

http://git-wip-us.apache.org/repos/asf/struts/blob/61cffe68/plugins/json/src/test/java/org/apache/struts2/json/AnotherBean.java
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/test/java/org/apache/struts2/json/AnotherBean.java 
b/plugins/json/src/test/java/org/apache/struts2/json/AnotherBean.java
new file mode 100644
index 0000000..81ba27d
--- /dev/null
+++ b/plugins/json/src/test/java/org/apache/struts2/json/AnotherBean.java
@@ -0,0 +1,32 @@
+package org.apache.struts2.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AnotherBean {
+    private List<Bean> beans;
+
+    private AnotherBean yetAnotherBean;
+
+    public List<Bean> getBeans() {
+        if (this.beans == null) {
+            this.beans = new ArrayList<Bean>();
+        }
+        return this.beans;
+    }
+
+    public void setBeans(List<Bean> beans) {
+        this.beans = beans;
+    }
+
+    public AnotherBean getYetAnotherBean() {
+        if(this.yetAnotherBean == null) {
+            this.yetAnotherBean = new AnotherBean();
+        }
+        return yetAnotherBean;
+    }
+
+    public void setYetAnotherBean(AnotherBean yetAnotherBean) {
+        this.yetAnotherBean = yetAnotherBean;
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/61cffe68/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java 
b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
index 7836f84..7bf53d3 100644
--- 
a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
+++ 
b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
@@ -459,7 +459,59 @@ public class JSONInterceptorTest extends StrutsTestCase {
         assertEquals(bean2.getDoubleField(), 10.1);
         assertEquals(bean2.getByteField(), 3);
     }
+    
+    public void testJSONArray() throws Exception {
+        setRequestContent("json-12.txt");
+        this.request.addHeader("content-type", "application/json");
+
+        // interceptor
+        JSONInterceptor interceptor = new JSONInterceptor();
+        interceptor.setRoot("beans");
+        TestAction5 action = new TestAction5();
+
+        this.invocation.setAction(action);
+        this.invocation.getStack().push(action);
+
+        interceptor.intercept(this.invocation);
+
+        List<Bean> beans = action.getBeans();
+
+        assertNotNull(beans);
+        assertEquals(1, beans.size());
+        assertTrue(beans.get(0).isBooleanField());
+        assertEquals(beans.get(0).getStringField(), "test");
+        assertEquals(beans.get(0).getIntField(), 10);
+        assertEquals(beans.get(0).getCharField(), 's');
+        assertEquals(beans.get(0).getDoubleField(), 10.1);
+        assertEquals(beans.get(0).getByteField(), 3);
+    }
 
+    public void testJSONArray2() throws Exception {
+        setRequestContent("json-12.txt");
+        this.request.addHeader("content-type", "application/json");
+
+        // interceptor
+        JSONInterceptor interceptor = new JSONInterceptor();
+        interceptor.setRoot("anotherBean.yetAnotherBean.beans");
+        TestAction5 action = new TestAction5();
+
+        this.invocation.setAction(action);
+        this.invocation.getStack().push(action);
+
+        interceptor.intercept(this.invocation);
+
+        List<Bean> beans = 
action.getAnotherBean().getYetAnotherBean().getBeans();
+
+        assertNotNull(beans);
+        assertEquals(1, beans.size());
+        assertTrue(beans.get(0).isBooleanField());
+        assertEquals(beans.get(0).getStringField(), "test");
+        assertEquals(beans.get(0).getIntField(), 10);
+        assertEquals(beans.get(0).getCharField(), 's');
+        assertEquals(beans.get(0).getDoubleField(), 10.1);
+        assertEquals(beans.get(0).getByteField(), 3);
+    }
+    
     @Override
     protected void setUp() throws Exception {
         super.setUp();

http://git-wip-us.apache.org/repos/asf/struts/blob/61cffe68/plugins/json/src/test/java/org/apache/struts2/json/TestAction5.java
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/test/java/org/apache/struts2/json/TestAction5.java 
b/plugins/json/src/test/java/org/apache/struts2/json/TestAction5.java
new file mode 100644
index 0000000..9c3321a
--- /dev/null
+++ b/plugins/json/src/test/java/org/apache/struts2/json/TestAction5.java
@@ -0,0 +1,52 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestAction5 {
+    private List<Bean> beans;
+
+    private AnotherBean anotherBean;
+
+    public List<Bean> getBeans() {
+        if (this.beans == null) {
+            this.beans = new ArrayList<Bean>();
+        }
+        return this.beans;
+    }
+
+    public void setBeans(List<Bean> beans) {
+        this.beans = beans;
+    }
+
+    public AnotherBean getAnotherBean() {
+        if(this.anotherBean == null) {
+            this.anotherBean = new AnotherBean();
+        }
+        return anotherBean;
+    }
+
+    public void setAnotherBean(AnotherBean anotherBean) {
+        this.anotherBean = anotherBean;
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/61cffe68/plugins/json/src/test/resources/org/apache/struts2/json/json-12.txt
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/test/resources/org/apache/struts2/json/json-12.txt 
b/plugins/json/src/test/resources/org/apache/struts2/json/json-12.txt
new file mode 100644
index 0000000..abd0c71
--- /dev/null
+++ b/plugins/json/src/test/resources/org/apache/struts2/json/json-12.txt
@@ -0,0 +1,8 @@
+[{
+    "booleanField": true,
+    "stringField" : "test",
+    "intField" : 10,
+    "charField": "s",
+    "doubleField": 10.1,
+    "byteField": 3
+}]

Reply via email to