empty value how to?

454 views
Skip to first unread message

Adam Jenkins

unread,
Jan 23, 2022, 5:50:54 PM1/23/22
to SnakeYAML
Hi Everyone,

I'm trying to output something like

mykey: 

but if I use an empty string I get

mykey: ' '

(note, the space between the ' was added by me for clarity in this post, it is an empty char in the output)

if I use null I get

mykey: null

Is there anyway to configure it for the first output example?

Cheers
Adam

Uday Bhaskar Sarma Seetamraju

unread,
Jan 23, 2022, 9:55:12 PM1/23/22
to snakeya...@googlegroups.com
Hi Adam,

That is the default YAML behavior (to use quotes).
Strongly recommend you try to stick to it.

But, If you MUST AVOID any quotes ..
.. when generating YAML output, use:
org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN
FYI: the other alternatives are:-
public static enum ScalarStyle {
    DOUBLE_QUOTED('"'),
    SINGLE_QUOTED('\''),
    LITERAL('|'),
    FOLDED('>'),
    PLAIN((Character)null);
To use this ENUM, you need to ue an EMITTER .. ..  like shown below:
final org.yaml.snakeyaml.DumperOptions dopt = new DumperOptions(); // https://bitbucket.org/asomov/snakeyaml/src/default/src/main/java/org/yaml/snakeyaml/DumperOptions.java
dopt.setDefaultScalarStyle( org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN );
final org.yaml.snakeyaml.emitter.Emitter snakeEmitter = new Emitter( this.javaWriter, _dumperoptions );
final org.yaml.snakeyaml.resolver.Resolver resolver = new Resolver(); // we cannot pass NULL as 2nd parameter for BELOW-constructor for Serializer.
final org.yaml.snakeyaml.serializer.Serializer serializer = new Serializer( snakeEmitter, resolver, _dumperoptions, null );
try {
serializer.open();
serializer.serialize( _outputNode ); serializer.close();
In the above SAMPLE CODE, I'm giving you ALL the intermediate classes .. .. in case, you'll need additional "flexibility" in what you output as YAML.

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeyaml-cor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/snakeyaml-core/90bb7e56-22fb-403f-a4fe-78c34b78a31an%40googlegroups.com.

Adam Jenkins

unread,
Jan 23, 2022, 9:57:31 PM1/23/22
to SnakeYAML
thanks heaps Uday :)
Reply all
Reply to author
Forward
0 new messages