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.codehaus.jackson.JsonGenerator;
025import org.codehaus.jackson.JsonParser;
026import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
027import org.nuxeo.ecm.automation.client.model.DateParser;
028
029/**
030 * Marshaller for the default ObjectCodec for the java Date class instances.
031 *
032 * @author ogrisel
033 * @since 5.7
034 */
035public class DateMarshaller implements JsonMarshaller<Date> {
036
037    @Override
038    public String getType() {
039        return "date";
040    }
041
042    @Override
043    public Class<Date> getJavaType() {
044        return Date.class;
045    }
046
047    @Override
048    public Date read(JsonParser jp) throws IOException {
049        jp.nextToken();
050        jp.nextToken();
051        return DateParser.parseW3CDateTime(jp.getText());
052    }
053
054    @Override
055    public void write(JsonGenerator jg, Object value) throws IOException {
056        jg.writeStartObject();
057        jg.writeStringField("entity-type", getType());
058        jg.writeStringField("value", DateParser.formatW3CDateTime((Date) value));
059        jg.writeEndObject();
060    }
061
062}