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  
44      private final boolean _fromJSON;
45      private final DateCache _dateCache;
46      private final SimpleDateFormat _format;
47  
48      public JSONDateConvertor()
49      {
50          this(false);
51      }
52  
53      public JSONDateConvertor(boolean fromJSON)
54      {
55          this(DateCache.DEFAULT_FORMAT,TimeZone.getTimeZone("GMT"),fromJSON);
56      }
57  
58      public JSONDateConvertor(String format,TimeZone zone,boolean fromJSON)
59      {
60          _dateCache=new DateCache(format);
61          _dateCache.setTimeZone(zone);
62          _fromJSON=fromJSON;
63          _format=new SimpleDateFormat(format);
64          _format.setTimeZone(zone);
65      }
66  
67      public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale)
68      {
69          _dateCache = new DateCache(format, locale);
70          _dateCache.setTimeZone(zone);
71          _fromJSON = fromJSON;
72          _format = new SimpleDateFormat(format, new DateFormatSymbols(locale));
73          _format.setTimeZone(zone);
74      }
75  
76      public Object fromJSON(Map map)
77      {
78          if (!_fromJSON)
79              throw new UnsupportedOperationException();
80          try
81          {
82              synchronized(_format)
83              {
84                  return _format.parseObject((String)map.get("value"));
85              }
86          }
87          catch(Exception e)
88          {
89              LOG.warn(e);
90          }
91          return null;
92      }
93  
94      public void toJSON(Object obj, Output out)
95      {
96          String date = _dateCache.format((Date)obj);
97          if (_fromJSON)
98          {
99              out.addClass(obj.getClass());
100             out.add("value",date);
101         }
102         else
103         {
104             out.add(date);
105         }
106     }
107 }