From 61cffe68feda8a4c527d085647e9567b2f689fc7 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Mon, 13 Oct 2014 20:07:07 +0300 Subject: [PATCH] improved interceptor that accepts and maps JSON array --- .../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(-) create mode 100644 plugins/json/src/test/java/org/apache/struts2/json/AnotherBean.java create mode 100644 plugins/json/src/test/java/org/apache/struts2/json/TestAction5.java create mode 100644 plugins/json/src/test/resources/org/apache/struts2/json/json-12.txt 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 69158bc01..f802af9eb 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; 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 000000000..81ba27d10 --- /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 beans; + + private AnotherBean yetAnotherBean; + + public List getBeans() { + if (this.beans == null) { + this.beans = new ArrayList(); + } + return this.beans; + } + + public void setBeans(List 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; + } +} 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 7836f849a..7bf53d328 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 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 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(); 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 000000000..9c3321a2d --- /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 beans; + + private AnotherBean anotherBean; + + public List getBeans() { + if (this.beans == null) { + this.beans = new ArrayList(); + } + return this.beans; + } + + public void setBeans(List 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; + } +} 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 000000000..abd0c7180 --- /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 +}]