java 自定义反序列化_java-使用jackson反序列化为自定义对象的HashMap

我有以下课程:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import org.codehaus.jackson.annotate.JsonProperty;

import java.io.Serializable;

import java.util.HashMap;

@JsonIgnoreProperties(ignoreUnknown = true)

public class Theme implements Serializable {

@JsonProperty

private String themeName;

@JsonProperty

private boolean customized;

@JsonProperty

private HashMap descriptor;

//...getters and setters for the above properties

}

当我执行以下代码时:

HashMap test = new HashMap();

Theme t1 = new Theme();

t1.setCustomized(false);

t1.setThemeName("theme1");

test.put("theme1", t1);

Theme t2 = new Theme();

t2.setCustomized(true);

t2.setThemeName("theme2");

t2.setDescriptor(new HashMap());

t2.getDescriptor().put("foo", "one");

t2.getDescriptor().put("bar", "two");

test.put("theme2", t2);

String json = "";

ObjectMapper mapper = objectMapperFactory.createObjectMapper();

try {

json = mapper.writeValueAsString(test);

} catch (IOException e) {

e.printStackTrace();

}

产生的json字符串如下所示:

{

"theme2": {

"themeName": "theme2",

"customized": true,

"descriptor": {

"foo": "one",

"bar": "two"

}

},

"theme1": {

"themeName": "theme1",

"customized": false,

"descriptor": null

}

}

我的问题是让上面的json字符串反序列化回

HashMap

目的。

我的反序列化代码如下所示:

HashMap themes =

objectMapperFactory.createObjectMapper().readValue(json, HashMap.class);

它使用正确的键反序列化为HashMap,但不会为这些值创建Theme对象。 我不知道在readValue()方法中要指定什么而不是“ HashMap.class”。

任何帮助,将不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值