gpt4 book ai didi

generics - 使用 Jackson 序列化 Map

转载 作者:行者123 更新时间:2023-12-02 03:17:46 27 4
gpt4 key购买 nike

我想与 Jackson 连载一张 map 。日期应该序列化为时间戳,就像我的所有其他日期一样。

以下代码以“Tue Mar 11 00:00:00 CET 1952”(即 Date.toString())形式呈现键,而不是时间戳。

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

我认为这是因为类型删除,并且 jackson 在运行时不知道该键是日期。但我没有找到将 TypeReference 传递给任何 writeValue 方法的方法。

有没有一种简单的方法来实现我想要的行为,或者所有的键总是由 jackson 呈现为字符串?

感谢您的任何提示。

最佳答案

默认的 map 键序列化器是StdKeySerializer,它只是执行此操作。

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

您可以使用the SimpleModule feature ,并使用 addKeySerializer 方法指定自定义 key 序列化程序。

<小时/>

这就是如何做到这一点。

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()));
}
}
<小时/>

最新 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()));
}
}

关于generics - 使用 Jackson 序列化 Map<Date, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56949886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com