struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1556522 - in /struts/struts2/trunk/plugins/json/src: main/java/org/apache/struts2/json/ test/java/org/apache/struts2/json/
Date Wed, 08 Jan 2014 13:03:00 GMT
Author: lukaszlenart
Date: Wed Jan  8 13:02:59 2014
New Revision: 1556522

URL: http://svn.apache.org/r1556522
Log:
WW-4185 makes default json date format configurable

Added:
    struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/SingleDateBean.java
Modified:
    struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
    struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
    struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
    struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
    struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java

Modified: struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java?rev=1556522&r1=1556521&r2=1556522&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
(original)
+++ struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
Wed Jan  8 13:02:59 2014
@@ -84,6 +84,7 @@ public class JSONResult implements Resul
     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 Resul
     }
 
     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 Resul
     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;
+    }
 }

Modified: struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java?rev=1556522&r1=1556521&r2=1556522&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
(original)
+++ struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
Wed Jan  8 13:02:59 2014
@@ -29,21 +29,9 @@ import org.apache.struts2.json.annotatio
 
 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);
     }
 

Modified: struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java?rev=1556522&r1=1556521&r2=1556522&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
(original)
+++ struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
Wed Jan  8 13:02:59 2014
@@ -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;

Modified: struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java?rev=1556522&r1=1556521&r2=1556522&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
(original)
+++ struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java
Wed Jan  8 13:02:59 2014
@@ -32,6 +32,7 @@ import org.springframework.mock.web.Mock
 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 Stru
         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_");

Modified: struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java?rev=1556522&r1=1556521&r2=1556522&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
(original)
+++ struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
Wed Jan  8 13:02:59 2014
@@ -5,9 +5,11 @@ import org.apache.struts2.json.annotatio
 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 Stru
             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);
+    }
+
 }

Added: struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/SingleDateBean.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/SingleDateBean.java?rev=1556522&view=auto
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/SingleDateBean.java
(added)
+++ struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/SingleDateBean.java
Wed Jan  8 13:02:59 2014
@@ -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;
+    }
+}



Mime
View raw message