【发布时间】:2022-01-22 14:43:14
【问题描述】:
{
"name": "test",
"columns": [
{
"name": "a",
"type": "TEXT"
},
{
"name": "b",
"type": "TEXT"
}
],
"rules": [
{
"production": {
"a": "[b]"
},
"filters": {
"a": [
"",
"ALL",
false
]
},
"refcolumns": [
"b"
]
}
]
}
JSON 文档有一个属性columns,其中包含一组Column 对象(也可以是使用属性名称作为键的映射)。
这是列对象在 JSON 中完全序列化的唯一地方。在其他任何地方,列都是使用唯一的 name 属性引用的
引用可用于映射键和值
我想反序列化这个文档并且:
- 在
columns属性中解析对其对应对象的引用 - 使用相同的java对象instance(列类是不可变的)并且不要每次都创建一个新的
Column。 (我想减少对象的数量)
JsonIdentityInfo 不适用于地图键。所以我使用自定义序列化程序
这里的 Rule 类是如何序列化的,
JsonColumnKeySerializer 只返回 Column 的 "name" 属性
class Rule {
@JsonSerialize(keyUsing = Column.JsonColumnKeySerializer.class)
private HashMap<Column, RuleFormula> productions = new HashMap<>();
@JsonSerialize(keyUsing = Column.JsonColumnKeySerializer.class)
private Map<Column, RuleFilter> filters = new LinkedHashMap<>();
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
@JsonIdentityReference(alwaysAsId=true) // for testing purposes...
private Set<Column> refcolumns = new HashSet<>();
}
【问题讨论】: