View Javadoc
1   /*
2    * Copyright 2002-2019 the original author or authors.
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    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.security.oauth2.common.util;
18  
19  import java.io.IOException;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  import com.fasterxml.jackson.core.JsonParseException;
25  import com.fasterxml.jackson.core.JsonProcessingException;
26  import com.fasterxml.jackson.databind.DeserializationContext;
27  import com.fasterxml.jackson.databind.JsonDeserializer;
28  
29  /**
30   * JSON deserializer for Jackson to handle regular date instances as timestamps in ISO format.
31   * 
32   * @author Dave Syer
33   *
34   */
35  public class JsonDateDeserializer extends JsonDeserializer<Date> {
36  	 
37      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
38   
39  	@Override
40  	public Date deserialize(com.fasterxml.jackson.core.JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
41  		try {
42  			synchronized (dateFormat) {				
43  				return dateFormat.parse(parser.getText());
44  			}
45  		}
46  		catch (ParseException e) {
47  			throw new JsonParseException("Could not parse date", parser.getCurrentLocation(), e);
48  		}
49  	}
50  }