001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
020
021import java.io.IOException;
022import java.util.Date;
023
024import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
025import org.nuxeo.ecm.automation.client.model.DateParser;
026
027import com.fasterxml.jackson.core.JsonGenerator;
028import com.fasterxml.jackson.core.JsonParser;
029
030/**
031 * Marshaller for the default ObjectCodec for the java Date class instances.
032 *
033 * @author ogrisel
034 * @since 5.7
035 */
036public class DateMarshaller implements JsonMarshaller<Date> {
037
038    @Override
039    public String getType() {
040        return "date";
041    }
042
043    @Override
044    public Class<Date> getJavaType() {
045        return Date.class;
046    }
047
048    @Override
049    public Date read(JsonParser jp) throws IOException {
050        jp.nextToken();
051        jp.nextToken();
052        return DateParser.parseW3CDateTime(jp.getText());
053    }
054
055    @Override
056    public void write(JsonGenerator jg, Object value) throws IOException {
057        jg.writeStartObject();
058        jg.writeStringField("entity-type", getType());
059        jg.writeStringField("value", DateParser.formatW3CDateTime((Date) value));
060        jg.writeEndObject();
061    }
062
063}