gpt4 book ai didi

null - Snakeyaml 转储空字段而不是 null

转载 作者:行者123 更新时间:2023-12-02 03:33:18 24 4
gpt4 key购买 nike

我有一个 LinkedHashMap,其中某些值可能为 null,我希望将它们转储为空字段而不是 null。这是否有可能,还是我徒劳地伤了我的神经?另外,如果打印的是整数,如何去掉单引号?

我的代码:

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:

最佳答案

重写Representer类的representMapping方法。

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 - Snakeyaml 转储空字段而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51175846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com