Search code examples
yamlsnakeyaml

SnakeYaml: escaping new line characters in yaml


I've just tried the following code snippet in a groovy console

import org.yaml.snakeyaml.Yaml
o = new Yaml().load("a: a\\nb")
o.a

and it returns

a\nb

instead of

a
b

I've followed this guide

I was wondering if it's a bug, or I'm doing something wrong...


Solution

  • I've found it out

    To be escaped, the value has to be between double quotes, like this:

    import org.yaml.snakeyaml.Yaml
    o = new Yaml().load('a: "a\\nb"')
    o.a
    

    output:

    groovy> import org.yaml.snakeyaml.Yaml 
    groovy> o = new Yaml().load('a: "a\\nb"') 
    groovy> o.a 
    
    Result: a
    b