001/*
002 * Copyright (c) 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015import java.util.Date;
016
017import org.codehaus.jackson.JsonGenerator;
018import org.codehaus.jackson.JsonParser;
019import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
020import org.nuxeo.ecm.automation.client.model.DateParser;
021
022/**
023 * Marshaller for the default ObjectCodec for the java Date class instances.
024 *
025 * @author ogrisel
026 * @since 5.7
027 */
028public class DateMarshaller implements JsonMarshaller<Date> {
029
030    @Override
031    public String getType() {
032        return "date";
033    }
034
035    @Override
036    public Class<Date> getJavaType() {
037        return Date.class;
038    }
039
040    @Override
041    public Date read(JsonParser jp) throws IOException {
042        jp.nextToken();
043        jp.nextToken();
044        return DateParser.parseW3CDateTime(jp.getText());
045    }
046
047    @Override
048    public void write(JsonGenerator jg, Object value) throws IOException {
049        jg.writeStartObject();
050        jg.writeStringField("entity-type", getType());
051        jg.writeStringField("value", DateParser.formatW3CDateTime((Date) value));
052        jg.writeEndObject();
053    }
054
055}