Skip to content

Commit

Permalink
improved interceptor that accepts and maps JSON array
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Oct 13, 2014
1 parent 381e927 commit 61cffe6
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public String intercept(ActionInvocation invocation) throws Exception {
}

Object rootObject = null;
final ValueStack stack = invocation.getStack();
if (this.root != null) {
ValueStack stack = invocation.getStack();
rootObject = stack.findValue(this.root);

if (rootObject == null) {
Expand All @@ -100,6 +100,26 @@ public String intercept(ActionInvocation invocation) throws Exception {
// 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,59 @@ public void testRoot() throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"booleanField": true,
"stringField" : "test",
"intField" : 10,
"charField": "s",
"doubleField": 10.1,
"byteField": 3
}]

0 comments on commit 61cffe6

Please sign in to comment.