| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 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 |
* Convert a {@link Date} to JSON. |
| 35 |
* If fromJSON is true in the constructor, the JSON generated will |
| 36 |
* be of the form {class="java.util.Date",value="1/1/1970 12:00 GMT"} |
| 37 |
* If fromJSON is false, then only the string value of the date is generated. |
| 38 |
*/ |
| 39 |
public class JSONDateConvertor implements JSON.Convertor |
| 40 |
{ |
| 41 |
private static final Logger LOG = Log.getLogger(JSONDateConvertor.class); |
| 42 |
|
| 43 |
private final boolean _fromJSON; |
| 44 |
private final DateCache _dateCache; |
| 45 |
private final 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, null, zone); |
| 60 |
_fromJSON = fromJSON; |
| 61 |
_format = new SimpleDateFormat(format); |
| 62 |
_format.setTimeZone(zone); |
| 63 |
} |
| 64 |
|
| 65 |
public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale) |
| 66 |
{ |
| 67 |
_dateCache = new DateCache(format, locale, zone); |
| 68 |
_fromJSON = fromJSON; |
| 69 |
_format = new SimpleDateFormat(format, new DateFormatSymbols(locale)); |
| 70 |
_format.setTimeZone(zone); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 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 |
@Override |
| 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 |
} |