我正在尝试反序列化一些代表进程的JSON,其中进程可以有多个子进程。正在创建父进程并正确填充。但是,子进程列表具有按预期创建的对象,但不填充这些对象。
这是我的json:
{ "name":"net.sample.parent",
"id":19,
"subProcesses":[{
"name":"net.sample.child",
"id":21,
"subProcesses":[],
"processVariables":{},
"openTasks":null,
"parentProcessId":null}],
"processVariables":{
"id":1,
"startDate":"Aug 28, 2010 12:00:00 AM",
"budget":10000.0,
"userName":"mary",
"endDate":"Aug 28, 2011 12:00:00 AM"},
"openTasks":null,
"parentProcessId":null}
这是我创建GsonBuilder的代码:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ProcessInstanceSummary.class, new ProcessInstanceCreator());
gsonBuilder.registerTypeAdapter(HumanTask.class, new TaskInstanceCreator());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
public class TaskInstanceCreator implements InstanceCreator<HumanTask> {
public HumanTask createInstance(Type type) {
return new SimpleHumanTask();
}
}
public class ProcessInstanceCreator implements InstanceCreator<ProcessInstanceSummary> {
public ProcessInstanceSummary createInstance(Type type) {
return new ProcessInstanceSummaryImpl();
}
}
private class DateDeserializer implements JsonDeserializer<Date> {
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonPrimitive asJsonPrimitive = json.getAsJsonPrimitive();
return new Date(asJsonPrimitive.getAsLong());
}
}
界面
public interface ProcessInstanceSummary {
String getName();
Long getId();
Map<String, Object> getProcessVariables();
List<HumanTask> getOpenTasks();
List<ProcessInstanceSummary> getSubProcesses();
Long getParentProcessId();
}
实施:
public class ProcessInstanceSummaryImpl implements ProcessInstanceSummary {
private String name;
private Long id;
private Map<String, Object> processVariables;
private List<HumanTask> openTasks;
private List<ProcessInstanceSummary> subProcesses;
private Long parentProcess;
/*
No arg constructor here
Getters and setters here.
*/
}
好的,所以我在调用toObject时得到的结果如下所示:
dto ProcessInstanceSummaryImpl (id=39)
id Long (id=71)
name "net.sample.parent" (id=74)
openTasks null
parentProcess null
processVariables LinkedHashMap<K,V> (id=75)
subProcesses ArrayList<E> (id=57)
elementData Object[10] (id=66)
[0] ProcessInstanceSummaryImpl (id=69)
id null
name null
openTasks null
parentProcess null
processVariables null
subProcesses null
[1] null
[2] null
[3] null
[4] null
[5] null
[6] null
[7] null
[8] null
[9] null
modCount 1
size 1
如您所见,创建了一个正确类型的对象并将其添加到subProcesses列表中,但没有填充任何属性。
如果我只是采用子进程JSON并通过解析器运行它,它按预期工作。
我缺少一些步骤吗?
任何帮助都将不胜感激。
答案 0 :(得分:0)
尝试将setter添加到ProcessInstanceSummary接口,以便它可以设置属性。
答案 1 :(得分:0)
行。我修好了它。事实证明,您需要确保将列表通用设置为实现。见下文:
public class ProcessInstanceSummaryImpl implements ProcessInstanceSummary {
private String name;
private Long id;
private Map<String, Object> processVariables;
private List<HumanTask> openTasks;
private List<ProcessInstanceSummaryImpl> subProcesses;
private Long parentProcessId;
/*
No arg constructor here
Getters and setters here.
*/
}