com.sandata.lab.common.utils.data.adapter.CustomJodaTimeAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.sandata.lab.common.utils.data.adapter.CustomJodaTimeAdapter.java

Source

/*
 * Copyright (c) 2015. Sandata Technologies, LLC
 * 26 Harbor Park Drive, Port Washington, NY 11050, 800-544-7263
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Sandata Technologies, LLC
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered into with
 * Sandata.
 */

package com.sandata.lab.common.utils.data.adapter;

import com.google.gson.*;
import com.sandata.lab.common.utils.date.DateUtil;
import org.joda.time.LocalTime;

import java.lang.reflect.Type;

/**
 * Date: 9/5/13
 * Time: 5:28 PM
 */

public class CustomJodaTimeAdapter implements JsonDeserializer<LocalTime>, JsonSerializer<LocalTime> {

    @Override
    public LocalTime deserialize(JsonElement jsonElement, Type type,
            JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        try {
            return new LocalTime(jsonElement.getAsString());

        } catch (IllegalArgumentException iae) {
            // Fall back to parse with "hh:mm aa" format
            return DateUtil.parseShortTime(jsonElement.getAsString());
        }

    }

    @Override
    public JsonElement serialize(LocalTime localTime, Type type,
            JsonSerializationContext jsonSerializationContext) {

        return new JsonPrimitive(DateUtil.format(localTime));
    }
}