gpt4 book ai didi

redis - Jackson2HashMapper 不使用 Flatten=True 序列化 Date

转载 作者:可可西里 更新时间:2023-11-01 11:14:46 28 4
gpt4 key购买 nike

将 Jackson Hash Mapper 与 Flatten=true 结合使用,我的日期字段将被丢弃。这是正确的行为还是错误?有没有办法用 Flatten=true 序列化 Date?

我使用了以下测试 Pojo:

import java.util.Date;
public class FooClass{
private Boolean foolean;
private Integer barteger;
private String simpleString;
private Date myDate;
public void setFoolean(Boolean value){ foolean = value; }
public Boolean getFoolean(){ return foolean; }
public void setBarteger(Integer value){ barteger = value; }
public Integer getBarteger(){ return barteger; }
public void setSimpleString(String value) { simpleString = value; }
public String getSimpleString(){ return simpleString; }
public void setMyDate(Date value) { myDate = value; }
public Date getMyDate(){ return myDate; }
}

public class Main {
public static void main(String[] args) throws ParseException,
JsonParseException, JsonMappingException, IOException {
Jackson2HashMapper hashMapper = new Jackson2HashMapper(true);

FooClass fooObject = new FooClass();
fooObject.setFoolean(true);
fooObject.setBarteger(10);
fooObject.setSimpleString("Foobar");
fooObject.setMyDate(new Date());

Map<String, Object> hash = hashMapper.toHash(fooObject);
for (String key: hash.keySet())
{
System.out.println("hash contains: " + key + "=" +
hash.get(key.toString()));
}

FooClass newFoo = (FooClass)(hashMapper.fromHash(hash));
System.out.println("FromHash: " + newFoo);
}
}

在这种情况下,我得到以下输出:

hash contains: @class=FooClass
hash contains: foolean=true
hash contains: barteger=10
hash contains: simpleString=Foobar
FromHash: FooClass@117159c0

如果我改变 new Jackson2HashMapper(false);然后我得到:

hash contains: @class=FooClass
hash contains: foolean=true
hash contains: barteger=10
hash contains: simpleString=Foobar
hash contains: myDate=[java.util.Date, 1547033077869]
FromHash: FooClass@7ed7259e

我希望在这两种情况下都能序列化 Date 字段 - 可能还有一个描述日期类型的附加字段(扁平化)。

我将其原因追溯到 HashMapper 代码中的以下行:typingMapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);配置映射器的位置。

最佳答案

它似乎在 Jackson2HashMapper 中发出。深入Jackson2HashMapper的源码后,好像问题在Jackson2HashMapper。为此创建了一个问题,DATAREDIS-1001Jackson2HashMapper does not serialize Date/Calender fields when flatten = true

关于redis - Jackson2HashMapper 不使用 Flatten=True 序列化 Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54109445/

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