mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-4413 Improves JSON interceptor to handle JSON arrays
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
[{
|
||||
"booleanField": true,
|
||||
"stringField" : "test",
|
||||
"intField" : 10,
|
||||
"charField": "s",
|
||||
"doubleField": 10.1,
|
||||
"byteField": 3
|
||||
}]
|
||||
Reference in New Issue
Block a user