ar.com.wolox.base.json.DateTimeJsonSerializationTest.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.wolox.base.json.DateTimeJsonSerializationTest.java

Source

/**
 * Copyright (c) 2011 Wolox <http://www.wolox.com.ar/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.wolox.base.json;

import static org.junit.Assert.assertEquals;

import org.joda.time.DateTime;
import org.junit.Test;

import ar.com.wolox.commons.base.json.DateTimeJsonDeserializer;
import ar.com.wolox.commons.base.json.DateTimeJsonSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
 * Tests the serialization process using the {@link DateTimeJsonSerializer} and {@link DateTimeJsonDeserializer}
 * to convert to JSON a {@link DateTime} using the {@link ObjectMapper}
 *
 * @author Guido Marucci Blas
 * @since Sep 7, 2011
 */
public final class DateTimeJsonSerializationTest {

    /** Foo class used to test the serialization */
    @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
    private static final class Foo {

        @JsonDeserialize(using = DateTimeJsonDeserializer.class)
        private final DateTime date;

        /**
         * Creates the Foo.
         *
         * @param date
         */
        @JsonCreator
        public Foo(@JsonProperty("date") final DateTime date) {
            this.date = date;
        }

        /**
         * Returns the date.
         *
         * @return <code>DateTime</code> with the date.
         */
        public DateTime getDate() {
            return date;
        }
    }

    /** Tests the serialization/deserialization process */
    @Test
    public void testSerializationProcess() throws Exception {
        final DateTime date = new DateTime(1988, 11, 29, 12, 0);
        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        final SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null))
                .addDeserializer(DateTime.class, new DateTimeJsonDeserializer())
                .addSerializer(DateTime.class, new DateTimeJsonSerializer());
        mapper.registerModule(testModule);

        final String json = mapper.writeValueAsString(new Foo(date));
        final Foo deserialized = mapper.readValue(json, Foo.class);
        assertEquals(date, deserialized.getDate());
    }
}