從snakeyaml project WIKI服用。例子是here。
如何分析JodaTime
由於JodaTime是沒有的JavaBean(因爲它沒有一個空的構造函數),它需要一些額外的處理分析時:
private class ConstructJodaTimestamp extends ConstructYamlTimestamp {
public Object construct(Node node) {
Date date = (Date) super.construct(node);
return new DateTime(date, DateTimeZone.UTC);
}
}
當JodaTime實例是JavaBean的您可以使用以下屬性:
Yaml y = new Yaml(new JodaPropertyConstructor());
class JodaPropertyConstructor extends Constructor {
public JodaPropertyConstructor() {
yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
}
class TimeStampConstruct extends Constructor.ConstructScalar {
@Override
public Object construct(Node nnode) {
if (nnode.getTag().equals("tag:yaml.org,2002:timestamp")) {
Construct dateConstructor = yamlConstructors.get(Tag.TIMESTAMP);
Date date = (Date) dateConstructor.construct(nnode);
return new DateTime(date, DateTimeZone.UTC);
} else {
return super.construct(nnode);
}
}
}
}
您是否找到解決方案? – Maxence