diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java new file mode 100644 index 000000000..2f46769e4 --- /dev/null +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java @@ -0,0 +1,29 @@ +/* + * Copyright 2017 The Apache Software Foundation. + * + * Licensed 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; + +/** + *

Class consisting of various constant values being used controlling + * JSON plugin behaviour

+ * + *

+ * These values can be overridden using struts.xml file by providing custom values. + *

+ */ +public class JSONConstants { + + public static final String RESULT_EXCLUDE_PROXY_PROPERTIES = "struts.json.result.excludeProxyProperties"; +} diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java index f44719f1a..c650e0626 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java @@ -41,6 +41,7 @@ import java.util.zip.GZIPOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.opensymphony.xwork2.inject.Inject; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -58,7 +59,14 @@ public class JSONUtil { public static final boolean CACHE_BEAN_INFO_DEFAULT = true; private static final Logger LOG = LogManager.getLogger(JSONUtil.class); - + + private static JSONWriter writer = new JSONWriter(); + + @Inject + public static void setWriter(JSONWriter writer) { + JSONUtil.writer = writer; + } + /** * Serializes an object into JSON. * @@ -70,7 +78,6 @@ public class JSONUtil { * @throws JSONException in case of error during serialize */ public static String serialize(Object object, boolean cacheBeanInfo) throws JSONException { - JSONWriter writer = new JSONWriter(); writer.setCacheBeanInfo(cacheBeanInfo); return writer.write(object); } @@ -124,7 +131,6 @@ public class JSONUtil { Collection includeProperties, boolean ignoreHierarchy, boolean excludeNullProperties, boolean cacheBeanInfo) throws JSONException { - JSONWriter writer = new JSONWriter(); writer.setIgnoreHierarchy(ignoreHierarchy); writer.setCacheBeanInfo(cacheBeanInfo); return writer.write(object, excludeProperties, includeProperties, excludeNullProperties); @@ -186,7 +192,6 @@ public class JSONUtil { public static String serialize(Object object, Collection excludeProperties, Collection includeProperties, boolean ignoreHierarchy, boolean enumAsBean, boolean excludeNullProperties, String defaultDateFormat, boolean cacheBeanInfo) throws JSONException { - JSONWriter writer = new JSONWriter(); writer.setIgnoreHierarchy(ignoreHierarchy); writer.setEnumAsBean(enumAsBean); writer.setDateFormatter(defaultDateFormat); diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java index 0e10ccf85..00396c5c1 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java @@ -20,6 +20,7 @@ */ package org.apache.struts2.json; +import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.ProxyUtil; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -76,6 +77,12 @@ public class JSONWriter { private boolean enumAsBean = ENUM_AS_BEAN_DEFAULT; private boolean excludeNullProperties; private boolean cacheBeanInfo = true; + private boolean excludeProxyProperties = false; + + @Inject(value = JSONConstants.RESULT_EXCLUDE_PROXY_PROPERTIES, required = false) + public void setExcludeProxyProperties(String excludeProxyProperties) { + this.excludeProxyProperties = Boolean.parseBoolean(excludeProxyProperties); + } /** * @param object Object to be serialized into JSON @@ -102,6 +109,7 @@ public class JSONWriter { Collection includeProperties, boolean excludeNullProperties) throws JSONException { this.excludeNullProperties = excludeNullProperties; this.buf.setLength(0); + this.stack.clear(); this.root = object; this.exprStack = ""; this.buildExpr = ((excludeProperties != null) && !excludeProperties.isEmpty()) @@ -211,7 +219,7 @@ public class JSONWriter { BeanInfo info; try { - Class clazz = ProxyUtil.ultimateTargetClass(object); + Class clazz = excludeProxyProperties ? ProxyUtil.ultimateTargetClass(object) : object.getClass(); info = ((object == this.root) && this.ignoreHierarchy) ? getBeanInfoIgnoreHierarchy(clazz) diff --git a/plugins/json/src/main/resources/struts-plugin.xml b/plugins/json/src/main/resources/struts-plugin.xml index e6aa1de31..01fc1bd30 100644 --- a/plugins/json/src/main/resources/struts-plugin.xml +++ b/plugins/json/src/main/resources/struts-plugin.xml @@ -5,6 +5,9 @@ "http://struts.apache.org/dtds/struts-2.5.dtd"> + + + diff --git a/plugins/spring/src/main/resources/struts-plugin.xml b/plugins/spring/src/main/resources/struts-plugin.xml index cc13bca45..4198ecc1e 100644 --- a/plugins/spring/src/main/resources/struts-plugin.xml +++ b/plugins/spring/src/main/resources/struts-plugin.xml @@ -36,6 +36,7 @@ +