Unable to produce folded block for multi line string

Issue #304 wontfix
Vitalii Tamazian created an issue

I'm trying to use snakeYaml to save multiline strings (code snippets) in human-readable form. My scala code:

val code ="""// test
      |println("Hello world!")
      |exit(1)
    """.stripMargin

  val map: java.util.Map[String, String] = new java.util.HashMap[String, String]()
  map.put("Code", code)
  System.out.println(code)

  val options = new DumperOptions()
  options.setDefaultScalarStyle(ScalarStyle.FOLDED)
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)

  val yamal = new Yaml(options)

  System.out.println(yamal.dump(map))

produces yaml:

Code: "// test\r\nprintln(\"Hello world!\")\r\nexit(1)\r\n\r\n        "
but should produce something like below. Playing with different options didn't help so far.

Code: >
// test
println("Hello world!")
exit(1)

Comments (6)

  1. Andrey Somov
    • changed status to open

    This question requires a lot of explanations - specification (Block Indentation Indicator, Block Chomping Indicator), implementation, practical usage. I will try to do it as soon as I can. You can greatly help me if you can provide the following YAML document: try to create a YAML document where the root element is a list of 2 values. Each value is a map. Each map contains two String->String pairs. The first map has BLOCK multi-line scalars as values and the second map contains BLOCK multi-values as keys. (other values are simple plain scalars). This data structure will help me to explain the challenge.

    At the moment SnakeYAML is not using block scalars inside collections. Example where BLOCK scalar works: https://bitbucket.org/asomov/snakeyaml/src/tip/src/test/java/org/yaml/snakeyaml/Chapter2_3Test.java?at=default#cl-44

  2. Vitalii Tamazian reporter

    Sorry, not sure how to create these maps with different Dumper Options. Can you provide an example in java?

    Another point: We can create the valid Yaml below manually and read it back, so there should be way to do it through java api?

    Code: |
      // test
      println("Hello world!")
      exit(1)
    Snippet: |
      line1
      line2
    
  3. Andrey Somov
    1. The API at the moment is asymmetrical, we can parse any valid YAML document, but we cannot create any possible YAML document (for instance, comments are never produced)
    2. My question was not about Java, it was about a YAML document. The YAML document should be similar to what you already showed above. But leading and trailing spaces are important. That is why you have to use not only Block Style Indicator but also Block Indentation Indicator and Block Chomping Indicator (see http://yaml.org/spec/1.1/current.html#id926597).

    This is the template with plain scalars:

    --- 
    - 
      key1: value1
      key2: value2
    - 
      key3: value3
      key4: value4
    
  4. Vitalii Tamazian reporter

    Ok, looks like for our purposes it would be simpler to create the yaml manually, and read through the library. Thank for explanations!

  5. Andrey Somov

    Complexity - is one of the reasons why not everything is implemented.

    You can also use a template engine (Java and Scala have plenty of them).

  6. Log in to comment