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

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

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

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

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.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 default 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.defaultSerializeDateKey(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.defaultSerializeDateKey(value.getTimeInMillis(), jgen);
        }
    }
}