Snakeyaml 转储空字段而不是 null



我有一个LinkedHashMap,其中某些值可能是空的,我希望它们被转储为空字段而不是空字段。甚至有可能还是我白白地破坏了我的神经?另外,如果打印了整数,如何摆脱单引号?

我的代码:

Map<String, Object> map = org.createOrgYamlMap();
DumperOptions options= new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yaml = new Yaml(options);
PrintWriter writer = new PrintWriter(new File(orgDirectory.toString() + "/organization.yml"));
yaml.dump(map, writer);

给我 yaml 文件:

uuid: '123'
name: test1
document_vault: null

我想要什么:

uuid: 123
name: test1
document_vault:

Override representMapping Method of Representer class。

Map<String, Object> map = org.createOrgYamlMap();
Representer representer = new Representer() {
@Override
protected Node representMapping(Tag tag, Map<?, ?> mapping, Boolean flowStyle) {
List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size());
MappingNode node = new MappingNode(tag, value, flowStyle);
representedObjects.put(objectToRepresent, node);
boolean bestStyle = true;
for (Map.Entry<?, ?> entry : mapping.entrySet()) {
Node nodeKey = representData(entry.getKey());
//If value is null then treat it as empty string.
Node nodeValue = entry.getValue() == null ?
representData("") : representData(entry.getValue());
if (!(nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null)) {
bestStyle = false;
}
if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) {
bestStyle = false;
}
value.add(new NodeTuple(nodeKey, nodeValue));
}
if (flowStyle == null) {
if (defaultFlowStyle != FlowStyle.AUTO) {
node.setFlowStyle(defaultFlowStyle.getStyleBoolean());
} else {
node.setFlowStyle(bestStyle);
}
}
return node;
}
};

DumperOptions options= new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yaml = new Yaml(representer, options);
PrintWriter writer = new PrintWriter(new File(orgDirectory.toString() + "/organization.yml"));
yaml.dump(map, writer);

测试代码:

public static void main(String[] args) throws FileNotFoundException {
Map<String, Object> lhm = new LinkedHashMap<>();
lhm.put("c", null);
DumperOptions options = new DumperOptions();
Representer representer = new Representer() {
@Override
protected Node representMapping(Tag tag, Map<?, ?> mapping, Boolean flowStyle) {
List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size());
MappingNode node = new MappingNode(tag, value, flowStyle);
representedObjects.put(objectToRepresent, node);
boolean bestStyle = true;
for (Map.Entry<?, ?> entry : mapping.entrySet()) {
Node nodeKey = representData(entry.getKey());
Node nodeValue = entry.getValue() == null ?
representData("") : representData(entry.getValue());
if (!(nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null)) {
bestStyle = false;
}
if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) {
bestStyle = false;
}
value.add(new NodeTuple(nodeKey, nodeValue));
}
if (flowStyle == null) {
if (defaultFlowStyle != FlowStyle.AUTO) {
node.setFlowStyle(defaultFlowStyle.getStyleBoolean());
} else {
node.setFlowStyle(bestStyle);
}
}
return node;
}
};
options.setDefaultFlowStyle(FlowStyle.BLOCK);
Yaml yaml = new Yaml(representer, options);
String s = yaml.dump(lhm);
System.out.println(s);
}

输出:

c: ''

刚刚用同样的问题与自己辩论,我希望将 null 打印为~而不是null.

根据他们的测试,您可以通过以下方式轻松实现

public void test() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(FlowStyle.BLOCK);
Yaml yaml = new Yaml(new NullRepresenter(), options);
Map<String, String> map = new HashMap<String, String>();
map.put("uuid", "123");
map.put("document_vault", null);
String output = yaml.dump(map);
assertEquals("uuid: 123ndocument_vault: ~n", output);
}
private class NullRepresenter extends Representer {
public NullRepresenter() {
super();
// null representer is exceptional and it is stored as an instance variable.
this.nullRepresenter = new RepresentNull();
}
private class RepresentNull implements Represent {
public Node representData(Object data) {
return representScalar(Tag.NULL, "~");
}
}
}

由于''不是允许值的一部分,因此将在 null 标记前面附加。

document_vault: !!null ''

相关内容

  • 没有找到相关文章

最新更新