View Javadoc

    1   //========================================================================
    2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.util.ajax;
    16
    17  import java.text.DateFormatSymbols;
    18  import java.text.SimpleDateFormat;
    19  import java.util.Date;
    20  import java.util.Locale;
    21  import java.util.Map;
    22  import java.util.TimeZone;
    23
    24  import org.mortbay.log.Log;
    25  import org.mortbay.util.DateCache;
    26  import org.mortbay.util.ajax.JSON.Output;
    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 boolean _fromJSON;
    38      DateCache _dateCache;
    39      SimpleDateFormat _format;
    40
    41      public JSONDateConvertor()
    42      {
    43          this(false);
    44      }
    45
    46      public JSONDateConvertor(boolean fromJSON)
    47      {
    48          this(DateCache.DEFAULT_FORMAT,TimeZone.getTimeZone("GMT"),fromJSON);
    49      }
    50
    51      public JSONDateConvertor(String format,TimeZone zone,boolean fromJSON)
    52      {
    53          _dateCache=new DateCache(format);
    54          _dateCache.setTimeZone(zone);
    55          _fromJSON=fromJSON;
    56          _format=new SimpleDateFormat(format);
    57          _format.setTimeZone(zone);
    58      }
    59
    60      public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale)
    61      {
    62          _dateCache = new DateCache(format, locale);
    63          _dateCache.setTimeZone(zone);
    64          _fromJSON = fromJSON;
    65          _format = new SimpleDateFormat(format, new DateFormatSymbols(locale));
    66          _format.setTimeZone(zone);
    67      }
    68
    69      public Object fromJSON(Map map)
    70      {
    71          if (!_fromJSON)
    72              throw new UnsupportedOperationException();
    73          try
    74          {
    75              synchronized(_format)
    76              {
    77                  return _format.parseObject((String)map.get("value"));
    78              }
    79          }
    80          catch(Exception e)
    81          {
    82              Log.warn(e);
    83          }
    84          return null;
    85      }
    86
    87      public void toJSON(Object obj, Output out)
    88      {
    89          String date = _dateCache.format((Date)obj);
    90          if (_fromJSON)
    91          {
    92              out.addClass(obj.getClass());
    93              out.add("value",date);
    94          }
    95          else
    96          {
    97              out.add(date);
    98          }
    99      }
    100
    101 }