View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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,null,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, zone);
69          _fromJSON = fromJSON;
70          _format = new SimpleDateFormat(format, new DateFormatSymbols(locale));
71          _format.setTimeZone(zone);
72      }
73  
74      public Object fromJSON(Map map)
75      {
76          if (!_fromJSON)
77              throw new UnsupportedOperationException();
78          try
79          {
80              synchronized(_format)
81              {
82                  return _format.parseObject((String)map.get("value"));
83              }
84          }
85          catch(Exception e)
86          {
87              LOG.warn(e);
88          }
89          return null;
90      }
91  
92      public void toJSON(Object obj, Output out)
93      {
94          String date = _dateCache.format((Date)obj);
95          if (_fromJSON)
96          {
97              out.addClass(obj.getClass());
98              out.add("value",date);
99          }
100         else
101         {
102             out.add(date);
103         }
104     }
105 }