View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.ajax;
15  
16  import java.text.DateFormatSymbols;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  import java.util.Locale;
20  import java.util.Map;
21  import java.util.TimeZone;
22  
23  import org.eclipse.jetty.util.DateCache;
24  import org.eclipse.jetty.util.ajax.JSON.Output;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.log.Logger;
27  
28  /* ------------------------------------------------------------ */
29  /**
30  * Convert a {@link Date} to JSON.
31  * If fromJSON is true in the constructor, the JSON generated will
32  * be of the form {class="java.util.Date",value="1/1/1970 12:00 GMT"}
33  * If fromJSON is false, then only the string value of the date is generated.
34  */
35  public class JSONDateConvertor implements JSON.Convertor
36  {
37      private static final Logger LOG = Log.getLogger(JSONDateConvertor.class);
38      private boolean _fromJSON;
39      DateCache _dateCache;
40      SimpleDateFormat _format;
41  
42      public JSONDateConvertor()
43      {
44          this(false);
45      }
46  
47      public JSONDateConvertor(boolean fromJSON)
48      {
49          this(DateCache.DEFAULT_FORMAT,TimeZone.getTimeZone("GMT"),fromJSON);
50      }
51      
52      public JSONDateConvertor(String format,TimeZone zone,boolean fromJSON)
53      {
54          _dateCache=new DateCache(format);
55          _dateCache.setTimeZone(zone);
56          _fromJSON=fromJSON;
57          _format=new SimpleDateFormat(format);
58          _format.setTimeZone(zone);
59      }
60      
61      public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale)
62      {
63          _dateCache = new DateCache(format, locale);
64          _dateCache.setTimeZone(zone);
65          _fromJSON = fromJSON;
66          _format = new SimpleDateFormat(format, new DateFormatSymbols(locale));
67          _format.setTimeZone(zone);
68      }
69      
70      public Object fromJSON(Map map)
71      {
72          if (!_fromJSON)
73              throw new UnsupportedOperationException();
74          try
75          {
76              synchronized(_format)
77              {
78                  return _format.parseObject((String)map.get("value"));
79              }
80          }
81          catch(Exception e)
82          {
83              LOG.warn(e);  
84          }
85          return null;
86      }
87  
88      public void toJSON(Object obj, Output out)
89      {
90          String date = _dateCache.format((Date)obj);
91          if (_fromJSON)
92          {
93              out.addClass(obj.getClass());
94              out.add("value",date);
95          }
96          else
97          {
98              out.add(date);
99          }
100     }
101 }