mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
WW-4185 makes default json date format configurable
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1556522 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -84,6 +84,7 @@ public class JSONResult implements Result {
|
||||
private boolean enumAsBean = JSONWriter.ENUM_AS_BEAN_DEFAULT;
|
||||
private boolean noCache = false;
|
||||
private boolean excludeNullProperties = false;
|
||||
private String defaultDateFormat = null;
|
||||
private int statusCode;
|
||||
private int errorCode;
|
||||
private String callbackParameter;
|
||||
@@ -199,7 +200,8 @@ public class JSONResult implements Result {
|
||||
}
|
||||
|
||||
protected String createJSONString(HttpServletRequest request, Object rootObject) throws JSONException {
|
||||
String json = JSONUtil.serialize(rootObject, excludeProperties, includeProperties, ignoreHierarchy, enumAsBean, excludeNullProperties);
|
||||
String json = JSONUtil.serialize(rootObject, excludeProperties, includeProperties, ignoreHierarchy,
|
||||
enumAsBean, excludeNullProperties, defaultDateFormat);
|
||||
json = addCallbackIfApplicable(request, json);
|
||||
return json;
|
||||
}
|
||||
@@ -443,4 +445,13 @@ public class JSONResult implements Result {
|
||||
public void setEncoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
public String getDefaultDateFormat() {
|
||||
return defaultDateFormat;
|
||||
}
|
||||
|
||||
@Inject(required=false,value="struts.json.dateformat")
|
||||
public void setDefaultDateFormat(String defaultDateFormat) {
|
||||
this.defaultDateFormat = defaultDateFormat;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,21 +29,9 @@ import org.apache.struts2.json.annotations.SMDMethod;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
@@ -104,15 +92,18 @@ public class JSONUtil {
|
||||
* root object
|
||||
* @param enumAsBean
|
||||
* whether to serialized enums a Bean or name=value pair
|
||||
* @param defaultDateFormat
|
||||
* date format used to serialize dates
|
||||
* @return JSON string
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static String serialize(Object object, Collection<Pattern> excludeProperties,
|
||||
Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean enumAsBean,
|
||||
boolean excludeNullProperties) throws JSONException {
|
||||
Collection<Pattern> includeProperties, boolean ignoreHierarchy, boolean enumAsBean,
|
||||
boolean excludeNullProperties, String defaultDateFormat) throws JSONException {
|
||||
JSONWriter writer = new JSONWriter();
|
||||
writer.setIgnoreHierarchy(ignoreHierarchy);
|
||||
writer.setEnumAsBean(enumAsBean);
|
||||
writer.setDateFormatter(defaultDateFormat);
|
||||
return writer.write(object, excludeProperties, includeProperties, excludeNullProperties);
|
||||
}
|
||||
|
||||
|
||||
@@ -608,6 +608,12 @@ public class JSONWriter {
|
||||
this.enumAsBean = enumAsBean;
|
||||
}
|
||||
|
||||
public void setDateFormatter(String defaultDateFormat) {
|
||||
if (defaultDateFormat != null) {
|
||||
this.formatter = new SimpleDateFormat(defaultDateFormat);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class JSONAnnotationFinder {
|
||||
private boolean serialize = true;
|
||||
private Method accessor;
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.mock.web.MockServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -170,6 +171,23 @@ public class JSONResultTest extends StrutsTestCase {
|
||||
assertEquals(normalizedExpected, normalizedActual);
|
||||
}
|
||||
|
||||
public void testCustomDateFormat() throws Exception {
|
||||
JSONResult result = new JSONResult();
|
||||
result.setDefaultDateFormat("MM-dd-yyyy");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||
|
||||
SingleDateBean dateBean = new SingleDateBean();
|
||||
dateBean.setDate(sdf.parse("2012-12-23 10:10:10 GMT"));
|
||||
|
||||
stack.push(dateBean);
|
||||
|
||||
this.invocation.setAction(dateBean);
|
||||
result.execute(this.invocation);
|
||||
|
||||
String out = response.getContentAsString();
|
||||
assertEquals("{\"date\":\"12-23-2012\"}", out);
|
||||
}
|
||||
|
||||
public void testPrefixAndSuffix() throws Exception {
|
||||
JSONResult result = new JSONResult();
|
||||
result.setWrapPrefix("_prefix_");
|
||||
|
||||
@@ -5,9 +5,11 @@ import org.apache.struts2.json.annotations.JSONFieldBridge;
|
||||
import org.apache.struts2.json.bridge.StringBridge;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class JSONWriterTest extends StrutsTestCase{
|
||||
@Test
|
||||
@@ -100,4 +102,34 @@ public class JSONWriterTest extends StrutsTestCase{
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCanSerializeADate() throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||
|
||||
SingleDateBean dateBean = new SingleDateBean();
|
||||
dateBean.setDate(sdf.parse("2012-12-23 10:10:10 GMT"));
|
||||
|
||||
JSONWriter jsonWriter = new JSONWriter();
|
||||
jsonWriter.setEnumAsBean(false);
|
||||
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
|
||||
String json = jsonWriter.write(dateBean);
|
||||
assertEquals("{\"date\":\"2012-12-23T10:10:10\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCanSetDefaultDateFormat() throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||
|
||||
SingleDateBean dateBean = new SingleDateBean();
|
||||
dateBean.setDate(sdf.parse("2012-12-23 10:10:10 GMT"));
|
||||
|
||||
JSONWriter jsonWriter = new JSONWriter();
|
||||
jsonWriter.setEnumAsBean(false);
|
||||
jsonWriter.setDateFormatter("MM-dd-yyyy");
|
||||
String json = jsonWriter.write(dateBean);
|
||||
assertEquals("{\"date\":\"12-23-2012\"}", json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.apache.struts2.json;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class SingleDateBean {
|
||||
|
||||
private Date date;
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user