de.chaosfisch.util.DateTimeTypeConverter.java Source code

Java tutorial

Introduction

Here is the source code for de.chaosfisch.util.DateTimeTypeConverter.java

Source

/*
 * Copyright (c) 2014 Dennis Fischer.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0+
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 *
 * Contributors: Dennis Fischer
 */

package de.chaosfisch.util;

import com.google.gson.*;
import org.joda.time.DateTime;

import java.lang.reflect.Type;
import java.util.Date;

public class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
    @Override
    public JsonElement serialize(final DateTime src, final Type typeOfSrc, final JsonSerializationContext context) {
        return new JsonPrimitive(src.toString());
    }

    @Override
    public DateTime deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {
        try {
            return new DateTime(json.getAsString());
        } catch (final IllegalArgumentException e) {
            // May be it came in formatted as a java.util.Date, so try that
            final Date date = context.deserialize(json, Date.class);
            return new DateTime(date);
        }
    }
}