UcasRichard / snakeyaml

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

Dumping maps with null values to match an input possibility is very hard #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Input this to snakeyaml
onekey:
2. Notice that the result is a Map with one key value pair, "onekey" and 
null
3. Attempt to make the output look exactly like the input.

        DumperOptions myopt = new DumperOptions ();
        myopt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml myyaml = new Yaml (myopt);
        Map map = new HashMap ();
        map.put("onekey",null);
        System.out.print(myyaml.dump(map));

onekey: null

        map.put("onekey","");
        System.out.print(myyaml.dump(map));

onekey: ''

4. Notice that hacking snakeyaml is required and one unsafe solution is 
possible.

What is the expected output? What do you see instead?

onekey : 

DumperOptions.calculateScalarStyle is the only way to force _nothing_ to be 
emitted under this circumstance.  In general, there is not enough 
information to safely use this implementation of calculateScalarStyle:

        public ScalarStyle calculateScalarStyle(ScalarAnalysis analysis, 
ScalarStyle style) {
            if (analysis.scalar.equals(""))
                return ScalarStyle.PLAIN;
            return style;
        }

There does not seem to be an option in the Representer or a flag on a Node 
to force this to happen when the programmer knows this is safe and 
desirable.

My aim is to have yaml safely roundtrip from input to output and remain 
tagless and noiseless for humans to edit.

What version of the product are you using? On what operating system?

1.7-snapshot, 1.6, 1.5

Original issue reported on code.google.com by shr...@gmail.com on 12 Apr 2010 at 8:15

GoogleCodeExporter commented 9 years ago
Unfortunately, YAML spec does not give recommended values for standard tags.
!!null has 5 values (http://yaml.org/type/null.html) and !!bool has 18 (!) 
possible
values (http://yaml.org/type/bool.html)
For some (non-java) developers '~' is the natural way to represent 'null'.
I tried to configure the "default' values but it leads to very ugly code I have
remove it.

I have added a couple of examples to control values of standard tags.
Please have a look.
For null:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/types/NullTagTest.java
For bool:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/types/BoolTagTest.java

I think it it more flexible then the solution with ScalarAnalysis.

-
Andrey

Original comment by aso...@gmail.com on 13 Apr 2010 at 8:39

GoogleCodeExporter commented 9 years ago
Your advised solution is better.

However, the problem that I faced was that I demand that my output look a 
certain 
way.  And I believe BaseRepresenter needs some more functions.

When I pre-processed my objects, I even made special objects so that those 
alone 
could be turned into blanks.  Using representScalar(Tag.NULL,"") via 
BaseRepresenter.representer has a different output than assigning a new 
BaseRepresenter.nullRepresenter.  (This triggers aliases to be made of the 
blank.)

That is fine, now I understand, but.

But this does not help others with context dependent choices about how very 
special 
scalars get represented.  I believe that BaseRepresenter needs some new 
functions 
like representNullAs(String x) when one is inside a custom represent object 
 and decides to make a late decision about outputting null (in 1 of 18 ways).  This 
might extend to booleans with representTrueAs and representFalseAs.

Original comment by shr...@gmail.com on 20 Apr 2010 at 12:24

GoogleCodeExporter commented 9 years ago
I do not see how your proposal may be implemented. Null and boolean have just a 
few
possible values and we may create a method for each value. But what do we do 
for Date
and int (integers may get underscores to separate thousands)? Can you may be 
submit a
patch with the changes in the API ?
May be a better way is to make the implementations of Represent interface inside
SafeRepresenter public to simplify extension.

Original comment by aso...@gmail.com on 20 Apr 2010 at 7:32

GoogleCodeExporter commented 9 years ago
I will have to get back to about patches when I can peek into the code a bit.

Another representer hurdle:
[&id001]

How can I force that to happen with Representer?

Original comment by shr...@gmail.com on 26 Apr 2010 at 1:17

GoogleCodeExporter commented 9 years ago
Anchors ans aliases are automatically generated. You cannot (and should not) 
try to
create them yourself. It may very easily lead to an inconsistent document.

N.B. I would prefer to have one topic per issue. It significantly simplifies
understanding and resolving issues.

Original comment by aso...@gmail.com on 26 Apr 2010 at 7:22

GoogleCodeExporter commented 9 years ago
The ticket will be closed because the reporter does not contribute anything. 

Original comment by aso...@gmail.com on 1 Oct 2010 at 8:00