• <bdo id='nGslD'></bdo><ul id='nGslD'></ul>
      <tfoot id='nGslD'></tfoot>

      <small id='nGslD'></small><noframes id='nGslD'>

      <i id='nGslD'><tr id='nGslD'><dt id='nGslD'><q id='nGslD'><span id='nGslD'><b id='nGslD'><form id='nGslD'><ins id='nGslD'></ins><ul id='nGslD'></ul><sub id='nGslD'></sub></form><legend id='nGslD'></legend><bdo id='nGslD'><pre id='nGslD'><center id='nGslD'></center></pre></bdo></b><th id='nGslD'></th></span></q></dt></tr></i><div id='nGslD'><tfoot id='nGslD'></tfoot><dl id='nGslD'><fieldset id='nGslD'></fieldset></dl></div>

      <legend id='nGslD'><style id='nGslD'><dir id='nGslD'><q id='nGslD'></q></dir></style></legend>

      使用 SnakeYaml 转储带引号的值

      时间:2023-09-26
        <bdo id='EpJBN'></bdo><ul id='EpJBN'></ul>

        <i id='EpJBN'><tr id='EpJBN'><dt id='EpJBN'><q id='EpJBN'><span id='EpJBN'><b id='EpJBN'><form id='EpJBN'><ins id='EpJBN'></ins><ul id='EpJBN'></ul><sub id='EpJBN'></sub></form><legend id='EpJBN'></legend><bdo id='EpJBN'><pre id='EpJBN'><center id='EpJBN'></center></pre></bdo></b><th id='EpJBN'></th></span></q></dt></tr></i><div id='EpJBN'><tfoot id='EpJBN'></tfoot><dl id='EpJBN'><fieldset id='EpJBN'></fieldset></dl></div>

        • <tfoot id='EpJBN'></tfoot>

              <legend id='EpJBN'><style id='EpJBN'><dir id='EpJBN'><q id='EpJBN'></q></dir></style></legend>
            1. <small id='EpJBN'></small><noframes id='EpJBN'>

                <tbody id='EpJBN'></tbody>
                本文介绍了使用 SnakeYaml 转储带引号的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有一个简单的yml文件test.yml如下

                Having a simple yml file test.yml as follows

                color: 'red'
                

                我按如下方式加载和转储文件

                I load and dump the file as follows

                        final DumperOptions yamlOptions = new DumperOptions();
                        yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
                
                        Yaml yaml = new Yaml(yamlOptions);
                
                
                        Object result = yaml.load(new FileInputStream(new File("test.yml")));
                
                        System.out.println(yaml.dump(result));
                

                我希望得到

                color: 'red'
                

                但是,在转储期间,序列化程序会忽略引号并打印

                However, the during the dump, the serializer leaves out the quotes and prints

                color: red
                

                如何让序列化程序也打印原始引号?

                How can I make the serializer to print the original quotes too?

                推荐答案

                如何让序列化程序也打印原始引号?

                How can I make the serializer to print the original quotes too?

                不使用高级 API.引用规范:

                Not with the high-level API. Quoting the spec:

                标量样式是一种表示细节,不得用于传达内容信息,除非为了标签解析的目的而区分普通标量.

                The scalar style is a presentation detail and must not be used to convey content information, with the exception that plain scalars are distinguished for the purpose of tag resolution.

                高级 API 实现了整个 YAML 加载过程,按照规范的要求,只为您提供 YAML 文件的内容,而没有任何有关表示细节的信息.

                The high-level API implements the whole YAML loading process, giving you only the content of the YAML file, without any information about presentation details, as required by the spec.

                话虽如此,您可以使用保留演示详细信息的低级 API:

                That being said, you can use the low level API which preserves presentation details:

                final Yaml yaml = new Yaml();
                final Iterator<Event> events = yaml.parse(new StreamReader(new UnicodeReader(
                        new FileInputStream(new File("test.yml"))).iterator();
                
                final DumperOptions yamlOptions = new DumperOptions();
                final Emitter emitter = new Emitter(new PrintWriter(System.out), yamlOptions);
                while (events.hasNext()) emitter.emit(events.next());
                

                但是,请注意,即使这样也不会保留您输入的每个演示细节(例如,不会保留缩进和注释).SnakeYaml 不是往返的,因此无法保留准确的输入布局.

                However, be aware that even this will not preserve every presentation detail of your input (e.g. indentation and comments will not be preserved). SnakeYaml is not round-tripping and therefore unable to preserve the exact input layout.

                这篇关于使用 SnakeYaml 转储带引号的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Snakeyaml 似乎不必要地将简单值包装在列表中 下一篇:在 application.yml 中设置根日志级别

                相关文章

                最新文章

                  <bdo id='zGbau'></bdo><ul id='zGbau'></ul>

                  1. <legend id='zGbau'><style id='zGbau'><dir id='zGbau'><q id='zGbau'></q></dir></style></legend>

                    <small id='zGbau'></small><noframes id='zGbau'>

                    <i id='zGbau'><tr id='zGbau'><dt id='zGbau'><q id='zGbau'><span id='zGbau'><b id='zGbau'><form id='zGbau'><ins id='zGbau'></ins><ul id='zGbau'></ul><sub id='zGbau'></sub></form><legend id='zGbau'></legend><bdo id='zGbau'><pre id='zGbau'><center id='zGbau'></center></pre></bdo></b><th id='zGbau'></th></span></q></dt></tr></i><div id='zGbau'><tfoot id='zGbau'></tfoot><dl id='zGbau'><fieldset id='zGbau'></fieldset></dl></div>
                    <tfoot id='zGbau'></tfoot>