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  
27  /* ------------------------------------------------------------ */
28  /**
29  * Convert a {@link Date} to JSON.
30  * If fromJSON is true in the constructor, the JSON generated will
31  * be of the form {class="java.util.Date",value="1/1/1970 12:00 GMT"}
32  * If fromJSON is false, then only the string value of the date is generated.
33  */
34  public class JSONDateConvertor implements JSON.Convertor
35  {
36      private boolean _fromJSON;
37      DateCache _dateCache;
38      SimpleDateFormat _format;
39  
40      public JSONDateConvertor()
41      {
42          this(false);
43      }
44  
45      public JSONDateConvertor(boolean fromJSON)
46      {
47          this(DateCache.DEFAULT_FORMAT,TimeZone.getTimeZone("GMT"),fromJSON);
48      }
49      
50      public JSONDateConvertor(String format,TimeZone zone,boolean fromJSON)
51      {
52          _dateCache=new DateCache(format);
53          _dateCache.setTimeZone(zone);
54          _fromJSON=fromJSON;
55          _format=new SimpleDateFormat(format);
56          _format.setTimeZone(zone);
57      }
58      
59      public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale)
60      {
61          _dateCache = new DateCache(format, locale);
62          _dateCache.setTimeZone(zone);
63          _fromJSON = fromJSON;
64          _format = new SimpleDateFormat(format, new DateFormatSymbols(locale));
65          _format.setTimeZone(zone);
66      }
67      
68      public Object fromJSON(Map map)
69      {
70          if (!_fromJSON)
71              throw new UnsupportedOperationException();
72          try
73          {
74              synchronized(_format)
75              {
76                  return _format.parseObject((String)map.get("value"));
77              }
78          }
79          catch(Exception e)
80          {
81              Log.warn(e);  
82          }
83          return null;
84      }
85  
86      public void toJSON(Object obj, Output out)
87      {
88          String date = _dateCache.format((Date)obj);
89          if (_fromJSON)
90          {
91              out.addClass(obj.getClass());
92              out.add("value",date);
93          }
94          else
95          {
96              out.add(date);
97          }
98      }
99  
100 }