FlowStyle.BLOCK ignore for some multiline Strings

Issue #339 invalid
Former user created an issue

Hi all,

When dumping a java bean object to a file, we have the issue that some Strings snakeyaml ignore the FlowStyle.BLOCK options and dump then with the flow style. For example this wrong behaviour always happens with this string:

String value2 = "<display>\n  <title>About</title>\n  <instructions>Information About Your System</instructions>\n  <toolbar></toolbar>\n  <head />\n  <body>\n    <p>NuViewHR</p>\n    <set var=\"[varJarFile]\">\n      <file action=\"Timestamp\">\n        <filename>NuViewHR.jar</filename>\n        <path>[S_PathRoot]/SystemFiles/WebService/WEB-INF/Lib</path>\n      </file>\n    </set>\n    <set var=\"[varWFlwASP]\">\n      <file action=\"Timestamp\">\n        <filename>WrkFlw.asp</filename>\n        <path>[S_PathRoot]/WebPages</path>\n      </file>\n    </set>\n    <set var=\"[varWFlwASPX]\">\n      <file action=\"Timestamp\">\n        <filename>WrkFlw.aspx</filename>\n        <path>[S_PathRoot]/WebPages</path>\n      </file>\n    </set>\n    <set var=\"[varAjax]\">\n      <file action=\"Timestamp\">\n        <filename>XhtmlAjax.js</filename>\n        <path>[S_PathRoot]/WebPages/Common/Ajax</path>\n      </file>\n    </set>\n    <set var=\"[varAjaxCtls]\">\n      <file action=\"Timestamp\">\n        <filename>XhtmlAjaxControls.js</filename>\n        <path>[S_PathRoot]/WebPages/Common/Ajax</path>\n      </file>\n    </set>\n    <set var=\"[varAjaxDocs]\">\n      <file action=\"Timestamp\">\n        <filename>XhtmlAjaxDocuments.js</filename>\n        <path>[S_PathRoot]/WebPages/Common/Ajax</path>\n      </file>\n    </set>\n    <set var=\"[varScript]\">\n      <file action=\"Timestamp\">\n        <filename>XhtmlScripts.js</filename>\n        <path>[S_PathRoot]/WebPages/Profiles/Df/Forms</path>\n      </file>\n    </set>\n    <set var=\"[varXhtml]\">\n      <file action=\"Timestamp\">\n        <filename>Xhtml.xslt</filename>\n        <path>[S_PathRoot]/WebPages/Profiles/Df/Forms</path>\n      </file>\n    </set>\n    <set var=\"[varXhtmlObj]\">\n      <file action=\"Timestamp\">\n        <filename>XhtmlObjects.js</filename>\n        <path>[S_PathRoot]/WebPages/Profiles/Df/Forms</path>\n      </file>\n    </set>\n    <datasheet dataset=\"main\" headers=\"LEFT\" showlinecount=\"No\">\n      <row>\n        <col>[ctl_DbVersion]</col>\n        <col>[ctl_SrcVersion]</col>\n      </row>\n      <row>\n        <col>[ctl_DbRelease]</col>\n        <col>[ctl_SrcRelease]</col>\n      </row>\n      <row>\n        <col>[ctl_DbRevision]</col>\n        <col>[ctl_SrcRevision]</col>\n      </row>      \n<!--\n      <row>\n          <col>[ctl_SysCfgVal]</col>\n        </row>\n        <row>\n          <col>[ctl_SysVer]</col>\n        </row>      \n-->\n\n      <row style=\"1\">\n        <col>[ctl_SysHdr]</col>\n      </row>\n      <row>\n        <col>[ctl_SysJarFile]</col>\n      </row>\n      <row>\n        <col>&amp;nbsp;</col>\n      </row>\n      <row style=\"1\">\n        <col>[ctl_SysHdr2]</col>\n      </row>\n      <row>\n        <col>[ctl_SysWFlwASP]</col>\n      </row>\n      <row>\n        <col>[ctl_SysWFlwASPX]</col>\n      </row>\n      <row>\n        <col>&amp;nbsp;</col>\n      </row>\n      <row style=\"1\">\n        <col>[ctl_SysHdr3]</col>\n      </row>\n      <row>\n        <col>[ctl_SysAjax]</col>\n      </row>\n      <row>\n        <col>[ctl_SysAjaxCtls]</col>\n      </row>\n      <row>\n        <col>[ctl_SysAjaxDocs]</col>\n      </row>\n      <row>\n        <col>&amp;nbsp;</col>\n      </row>\n      <row style=\"1\">\n        <col>[ctl_SysHdr4]</col>\n      </row>\n      <row>\n        <col>[ctl_SysScript]</col>\n      </row>\n      <row>\n        <col>[ctl_SysXhtml]</col>\n      </row>\n      <row>\n        <col>[ctl_SysXhtmlObj]</col>\n      </row>\n      <row>\n        <col>&amp;nbsp;</col>\n      </row>\n      <row>\n        <col>&amp;nbsp;</col>\n      </row>\n      <row style=\"1\">\n        <col>[ctl_SysHdr5]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"RstHst\" headers=\"TOP\" showlinecount=\"No\">\n      <row>\n        <col>[ctl_restoredate]</col>\n        <col>[ctl_username]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"none\" headers=\"LEFT\" showlinecount=\"No\" title=\"Retrieve All Applicants\">\n      <row>\n        <col>[ctl_CSSCnsmAllDta]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"postingSite\" headers=\"LEFT\" showlinecount=\"No\" title=\"Number of Posting Sites\">\n      <row>\n        <col>[ctl_RcrPst]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"openReqs\" headers=\"LEFT\" showlinecount=\"No\" title=\"Number of Open requisitions\">\n      <row>\n        <col>[ctl_openreqscount]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"applicantsPerReq\" headers=\"TOP\" showlinecount=\"No\" title=\"Number of Applicants Applied per Open requisitions\">\n      <row>\n        <col>[ctl_Req]</col>\n        <col>[ctl_Applied]</col>\n      </row>\n    </datasheet>\n    <set var=\"[cssstatus]\"> \n    \n      <retrievecssstatus />\n    </set>\n    <datasheet dataset=\"none\" headers=\"LEFT\" showlinecount=\"No\" title=\"Number of Applicants not processed\">\n      <row>\n        <col>[ctl_CountUneviewed]</col>\n      </row>\n    </datasheet>\n    <datasheet dataset=\"none\" headers=\"LEFT\" showlinecount=\"No\" title=\"Number of Applicants not Completed\">\n      <row>\n        <col>[ctl_CountUncompleted]</col>\n      </row>\n    </datasheet>\n  </body>\n</display>";

this is a big issue because it is very complex to manually modify a long string not using the BLOCK flow. Here you can find a small reproducer of the issue: https://github.com/ufoscout/snakeyaml-issue

Comments (4)

  1. Andrey Somov

    Thank you for the test. It makes the issue clear.

    DumperOptions.FlowStyle.BLOCK is about how to emit sequences. And it works perfect in your case.

    The issue is with a scalar. SnakeYAML has a dedicated ScalarStyle. Unfortunately, it does not help you. There is no configuration now to break scalars into separate lines. There is a zillion of ways to serialize a string into a readable scalar.

    Feel free to make a proposal how to improve it.

  2. Francesco Cina

    Hi Andrey,

    I am a little bit confused about block and scalar styles, However, it would be good to have a way to force the use of the multi line block whenever there is an end line symbol (i.e. "\n", "\r\n", "\r") in the source string. For example:

    // I want to be sure that this string:
    String myStringWithEOL: this is my very very very\nlong string\n
    
    // is serialized with the block notation:
    myStringWithEOL: |
      this is my very very very 
      long string
    

    Would it be possible?

  3. Andrey Somov
    1. It should not be in the context of this issue because it is confusing (FlowStyle.BLOCK does not ignore anything)
    2. Your question is about the scalar style(s). YAML specification supports many ways to represent scalar - http://yaml.org/spec/1.1/current.html#id903915. Double Quoted, Single Quoted, Plain, Block Style Indicator, Block Indentation Indicator, Block Chomping Indicator, Literal, Folded - all this may be taken into account. At the moment SnakeYAML supports very simple format. (via DumperOptions and in the Emtter)
    3. If you have a consistent proposal how to improve the scalar output - feel free to provide a patch.
  4. Log in to comment