Modified:
/trunk/jjson-struts2/src/main/java/de/grobmeier/json/plugins/struts2/JsonResult.java
/trunk/jjson-struts2/src/test/java/de/grobmeier/json/plugins/struts2/JsonResultTest.java
/trunk/jjson-struts2/src/test/resources/struts.xml
=======================================
---
/trunk/jjson-struts2/src/main/java/de/grobmeier/json/plugins/struts2/JsonResult.java
Fri Jul 1 03:33:32 2011
+++
/trunk/jjson-struts2/src/main/java/de/grobmeier/json/plugins/struts2/JsonResult.java
Tue Jul 12 02:54:21 2011
@@ -42,6 +42,8 @@
private String charSet = "UTF-8";
+ private boolean commentOutput = true;
+
/** Default constructor */
public JsonResult() {
}
@@ -108,7 +110,14 @@
JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
String result = encoder.encode(obj);
log.debug("Encoded JSON: " + result);
- writer.write(result);
+ if(this.commentOutput) {
+ log.debug("JSON will be served with comments - change with param:
outputComment = false ");
+ writer.write("/* ");
+ writer.write(result);
+ writer.write(" */");
+ } else {
+ writer.write(result);
+ }
} finally {
if (writer != null) {
writer.flush();
@@ -135,4 +144,13 @@
public void setCharSet(String charSet) {
this.charSet = charSet;
}
-}
+
+ /**
+ * Set true, if the result json should be in comments \/* JSON *\/
+ * false, other wise
+ * @param commentOutput true, if the output should be commented
+ */
+ public void setCommentOutput(boolean commentOutput) {
+ this.commentOutput = commentOutput;
+ }
+}
=======================================
---
/trunk/jjson-struts2/src/test/java/de/grobmeier/json/plugins/struts2/JsonResultTest.java
Tue Jul 5 02:16:54 2011
+++
/trunk/jjson-struts2/src/test/java/de/grobmeier/json/plugins/struts2/JsonResultTest.java
Tue Jul 12 02:54:21 2011
@@ -33,4 +33,10 @@
String executeAction = this.executeAction("/test");
Assert.assertEquals("{\"test\":\"mytest\"}", executeAction);
}
-}
+
+ @Test
+ public void testCommenteJson() throws Exception {
+ String executeAction = this.executeAction("/test2");
+ Assert.assertEquals("/* {\"test\":\"mytest\"} */", executeAction);
+ }
+}
=======================================
--- /trunk/jjson-struts2/src/test/resources/struts.xml Tue Jul 5 02:16:54
2011
+++ /trunk/jjson-struts2/src/test/resources/struts.xml Tue Jul 12 02:54:21
2011
@@ -23,11 +23,23 @@
<struts>
<package name="jjson" extends="struts-default">
<result-types>
- <result-type name="json"
class="de.grobmeier.json.plugins.struts2.JsonResult"/>
+ <result-type name="json"
class="de.grobmeier.json.plugins.struts2.JsonResult">
+ <param name="commentOutput">false</param>
+ </result-type>
</result-types>
<action name="test" class="de.grobmeier.json.plugins.struts2.TestAction">
<result type="json" />
</action>
</package>
+
+ <package name="commentJjson" extends="struts-default">
+ <result-types>
+ <result-type name="json"
class="de.grobmeier.json.plugins.struts2.JsonResult" />
+ </result-types>
+
+ <action name="test2"
class="de.grobmeier.json.plugins.struts2.TestAction">
+ <result type="json" />
+ </action>
+ </package>
</struts>