All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.fasterxml.jackson.databind.ser.std.StdKeySerializers Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.databind.ser.std;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import com.fitbur.fasterxml.jackson.core.*;

import com.fitbur.fasterxml.jackson.databind.JavaType;
import com.fitbur.fasterxml.jackson.databind.JsonSerializer;
import com.fitbur.fasterxml.jackson.databind.SerializerProvider;

public class StdKeySerializers
{
    protected final static JsonSerializer DEFAULT_KEY_SERIALIZER = new StdKeySerializer();

    @SuppressWarnings("unchecked")
    protected final static JsonSerializer DEFAULT_STRING_SERIALIZER
        = (JsonSerializer)(JsonSerializer) new StringKeySerializer();
    
    private StdKeySerializers() { }

    @SuppressWarnings("unchecked")
    public static JsonSerializer getStdKeySerializer(JavaType keyType)
    {
        if (keyType == null) {
            return DEFAULT_KEY_SERIALIZER;
        }
        Class cls = keyType.getRawClass();
        if (cls == String.class) {
            return DEFAULT_STRING_SERIALIZER;
        }
        if (cls == Object.class) {
            return DEFAULT_KEY_SERIALIZER;
        }
        // [JACKSON-606] special handling for dates...
        if (Date.class.isAssignableFrom(cls)) {
            return (JsonSerializer) DateKeySerializer.instance;
        }
        if (Calendar.class.isAssignableFrom(cls)) {
            return (JsonSerializer) CalendarKeySerializer.instance;
        }
        // If no match, just use com.fitburfault one:
        return DEFAULT_KEY_SERIALIZER;
    }

    /*
    /**********************************************************
    /* Standard implementations
    /**********************************************************
     */

    public static class StringKeySerializer
        extends StdSerializer
    {
        public StringKeySerializer() { super(String.class); }
        
        @Override
        public void serialize(String value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonGenerationException
        {
            jgen.writeFieldName(value);
        }
    }

    public static class DateKeySerializer
        extends StdSerializer
    {
        protected final static JsonSerializer instance = new DateKeySerializer();

        public DateKeySerializer() { super(Date.class); }
        
        @Override
        public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonGenerationException
        {
            provider.com.fitburfaultSerializeDateKey(value, jgen);
        }
    }

    public static class CalendarKeySerializer
        extends StdSerializer
    {
        protected final static JsonSerializer instance = new CalendarKeySerializer();

        public CalendarKeySerializer() { super(Calendar.class); }
        
        @Override
        public void serialize(Calendar value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonGenerationException
        {
            provider.com.fitburfaultSerializeDateKey(value.getTimeInMillis(), jgen);
        }
    }
}