yeison / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

Dumping a list of objects doesn't indent correctly #200

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If you have a structure like:

{
  "version": "1.0.0",
  "results": [
    {"foo": "bar"}
    …
  ]
}

You would expect that to output as

version: 1.0.0
results:
  - foo: "bar"

But instead, it outputs with the array with the same indentation as the hash 
keys:

version: 1.0.0
results:
- foo: "bar"

See attached reduction.

Run with mvn test

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running JsonOutputTest
Configuring TestNG with: 
org.apache.maven.surefire.testng.conf.TestNG652Configurator@5eae264f
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.612 sec <<< 
FAILURE!
testJsonOutput(JsonOutputTest)  Time elapsed: 0.088 sec  <<< FAILURE!
java.lang.AssertionError: expected:<responses:
 - {}
version: 1.0.0
> but was:<responses:
- {}
version: 1.0.0
>
    at org.testng.Assert.fail(Assert.java:89)
    at org.testng.Assert.failNotEquals(Assert.java:489)
    at org.testng.Assert.assertEquals(Assert.java:118)
    at org.testng.Assert.assertEquals(Assert.java:171)
    at org.testng.Assert.assertEquals(Assert.java:181)
    at JsonOutputTest.testJsonOutput(JsonOutputTest.java:30)

Original issue reported on code.google.com by s...@yahoo-inc.com on 23 Sep 2014 at 8:22

Attachments:

GoogleCodeExporter commented 9 years ago
Do you think it is a bug ? 
Have you checked all the possibilities in DumperOptions ?

Original comment by py4fun@gmail.com on 24 Sep 2014 at 8:25

GoogleCodeExporter commented 9 years ago
Yes, I believe it's a bug - why would outputting incorrect YAML be a default 
option?

If the hierarchy of your object is conceptually indented then its 
representation should be as well.

If your value is an array of strings it would be indented as such, but when it 
is an array of objects it is not.

Original comment by s...@yahoo-inc.com on 25 Sep 2014 at 12:32

GoogleCodeExporter commented 9 years ago
Well, if SnakeYAML outputs incorrect YAML - it is  bug. But the generated YAML 
is perfectly correct. (If "correct" means that it follows the specification)
Do you may be mean: "the generated YAML does not match my personal preferences" 
?

Then it can be divided into 2 parts:
- how can I generate the output with my format
- why my format is better and why it should be accepted as the default format

Original comment by py4fun@gmail.com on 25 Sep 2014 at 11:58

GoogleCodeExporter commented 9 years ago
Ok, let's consider the actual use case then:

How can I get snakeyaml to output in a format that perl YAML can understand?

http://search.cpan.org/~ingy/YAML-1.12/lib/YAML.pod

Let's say you output the above into a file called foo.yaml.  How can you get 
the following command to work?
    perl -MYAML -e 'YAML::LoadFile("foo.yaml")'

Original comment by s...@yahoo-inc.com on 25 Sep 2014 at 2:15

GoogleCodeExporter commented 9 years ago
Now it is clear. If search our issues you will find a number of these cases. 
Perl parser cannot parse YAML and users try to compensate it with changes in 
SnakeYAML.
I can recommend the following:
1) file a bug for Perl - they have to improve the parser
2) try to use different output options (flow style, for instance)
        DumperOptions options = new DumperOptions();
        options.setIndent(4);
        options.setPrettyFlow(true);
        options.setDefaultFlowStyle(FlowStyle.FLOW);
        Yaml yaml = new Yaml(options);
3) Have a look at org.yaml.snakeyaml.emitter.Emitter (ExpectBlockSequenceItem, 
writeIndent())
Study how it works and make a proposal how to improve it. This is Open Source !

I hope it helps.

Original comment by py4fun@gmail.com on 25 Sep 2014 at 4:29

GoogleCodeExporter commented 9 years ago
Since there is no feedback the issue is closed.

Original comment by py4fun@gmail.com on 4 Feb 2015 at 10:03