snakeyaml转储空字段,而不是null

时间:2018-07-04 14:19:12

标签: null yaml snakeyaml

我有一个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:

2 个答案:

答案 0 :(得分:2)

重写Representer类的describeMapping方法。

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: ''

答案 1 :(得分:1)

只是在同一个问题上辩论了一下,我希望将null打印为~而不是null

根据他们的tests,您可以轻松达到

    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: 123\ndocument_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, "~");
            }
        }
    }

由于''不是allowed values的一部分,因此它将在null标签之前。

document_vault: !!null ''