| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| package com.epam.dlab.util.mongo; |
| |
| import com.fasterxml.jackson.core.JsonParser; |
| import com.fasterxml.jackson.core.ObjectCodec; |
| import com.fasterxml.jackson.databind.DeserializationContext; |
| import com.fasterxml.jackson.databind.JsonDeserializer; |
| import com.fasterxml.jackson.databind.JsonNode; |
| |
| import java.io.IOException; |
| import java.text.DateFormat; |
| import java.text.ParseException; |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| |
| /** |
| * Deserializes {@link java.util.Date} from JSON |
| */ |
| public class IsoDateDeSerializer extends JsonDeserializer<Date> { |
| static final String DATE_NODE = "$date"; |
| static final String ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; |
| |
| @Override |
| public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
| ObjectCodec oc = jsonParser.getCodec(); |
| JsonNode node = oc.readTree(jsonParser); |
| |
| Date date; |
| if (node.get(DATE_NODE) != null) { |
| String dateValue = node.get(DATE_NODE).asText(); |
| DateFormat df = new SimpleDateFormat(ISO_DATE_FORMAT); |
| try { |
| date = df.parse(dateValue); |
| } catch (ParseException e) { |
| date = new Date(Long.valueOf(dateValue)); |
| } |
| } else { |
| date = new Date(node.asLong()); |
| } |
| return date; |
| } |
| |
| } |