View Javadoc

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