Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:32:13+00:00 2026-05-23T14:32:13+00:00

I want to serialize a Map with Jackson. The Date should be serialized as

  • 0

I want to serialize a Map with Jackson.
The Date should be serialized as a timestamp, like all my other dates.

The following code renders the keys in the form “Tue Mar 11 00:00:00 CET 1952” (which is Date.toString()) instead of the timestamp.

Map<Date, String> myMap = new HashMap<Date, String>();
...
ObjectMapper.writeValue(myMap)

I assume this is because of type erasure and jackson doesn’t know at runtime that the key is a Date. But I didn’t find a way to pass a TypeReference to any writeValue method.

Is there a simple way to achieve my desired behaviour or are all keys always rendered as Strings by jackson?

Thanks for any hint.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:32:13+00:00Added an answer on May 23, 2026 at 2:32 pm

    The default map key serializer is StdKeySerializer, and it simply does this.

    String keyStr = (value.getClass() == String.class) ? ((String) value) : value.toString();
    jgen.writeFieldName(keyStr);
    

    You could make use of the SimpleModule feature, and specify a custom key serializer, using the addKeySerializer method.


    And here’s how that could be done.

    import java.io.IOException;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.codehaus.jackson.JsonGenerator;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.Version;
    import org.codehaus.jackson.map.JsonSerializer;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.ObjectWriter;
    import org.codehaus.jackson.map.SerializerProvider;
    import org.codehaus.jackson.map.module.SimpleModule;
    import org.codehaus.jackson.map.type.MapType;
    import org.codehaus.jackson.map.type.TypeFactory;
    
    public class CustomKeySerializerDemo
    {
      public static void main(String[] args) throws Exception
      {
        Map<Date, String> myMap = new HashMap<Date, String>();
        myMap.put(new Date(), "now");
        Thread.sleep(100);
        myMap.put(new Date(), "later");
    
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(myMap));
        // {"Mon Jul 04 11:38:36 MST 2011":"now","Mon Jul 04 11:38:36 MST 2011":"later"}
    
        SimpleModule module =  
          new SimpleModule("MyMapKeySerializerModule",  
              new Version(1, 0, 0, null));
        module.addKeySerializer(Date.class, new DateAsTimestampSerializer());
    
        MapType myMapType = TypeFactory.defaultInstance().constructMapType(HashMap.class, Date.class, String.class);
    
        ObjectWriter writer = new ObjectMapper().withModule(module).typedWriter(myMapType);
        System.out.println(writer.writeValueAsString(myMap));
        // {"1309806289240":"later","1309806289140":"now"}
      }
    }
    
    class DateAsTimestampSerializer extends JsonSerializer<Date>
    {
      @Override
      public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) 
          throws IOException, JsonProcessingException
      {
        jgen.writeFieldName(String.valueOf(value.getTime()));
      }
    }
    

    Update for the latest Jackson (2.0.4):

    import java.io.IOException;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.ObjectWriter;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import com.fasterxml.jackson.databind.module.SimpleModule;
    import com.fasterxml.jackson.databind.type.MapType;
    import com.fasterxml.jackson.databind.type.TypeFactory;
    
    public class CustomKeySerializerDemo
    {
      public static void main(String[] args) throws Exception
      {
        Map<Date, String> myMap = new HashMap<Date, String>();
        myMap.put(new Date(), "now");
        Thread.sleep(100);
        myMap.put(new Date(), "later");
    
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(myMap));
        // {"2012-07-13T21:14:09.499+0000":"now","2012-07-13T21:14:09.599+0000":"later"}
    
        SimpleModule module = new SimpleModule();
        module.addKeySerializer(Date.class, new DateAsTimestampSerializer());
    
        MapType myMapType = TypeFactory.defaultInstance().constructMapType(HashMap.class, Date.class, String.class);
    
        ObjectWriter writer = new ObjectMapper().registerModule(module).writerWithType(myMapType);
        System.out.println(writer.writeValueAsString(myMap));
        // {"1342214049499":"now","1342214049599":"later"}
      }
    }
    
    class DateAsTimestampSerializer extends JsonSerializer<Date>
    {
      @Override
      public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) 
          throws IOException, JsonProcessingException
      {
        jgen.writeFieldName(String.valueOf(value.getTime()));
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to Serialize the Hash-Map of Default Lazy-list code and store it in
I'm serializing some java.util.Dates within a Map. The dates are serialized into Longs (Jackson
I have a class with a map, and I want to serialize the class
I want to serialize a hash map to a file and de-serialize it later
In the following Jackson/Java code that serializes objects into JSON, I am getting this:
I want to serialize my enum-value as an int, but i only get the
I want to Serialize a 60mb file into XML but it gives me System
I want to serialize a nullable bool simply by converting it to a string
I want to serialize this class: public class CarDisplay { public string Name {
I want to serialize a class to xml and store that in a field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.