Closed tom-tan closed 2 years ago
Currently, the Loader retains the styles of loaded YAML, and the default setting is only used when the nodes do not have styles. It is currently possible to get most of the way there by manually setting the styles on the nodes yourself - see Node.collectionStyle
and Node.scalarStyle
.
YAML 1.2 support should make this a lot easier though, via schemas (the json schema in particular, naturally).
Also, JSON scalars use both plain and double quoted styles, depending on their types
Thank you for your suggestion!
It is currently possible to get most of the way there by manually setting the styles on the nodes yourself - see
Node.collectionStyle
andNode.scalarStyle
.
Unfortunately they are not available for user code because they have package
visibility. Can we make them public?
Unfortunately they are not available for user code because they have
package
visibility. Can we make them public?
Whoops! I meant Node.setStyle
.
I meant
Node.setStyle
.
Oh, I missed it. I can use it to make a custom dumper for JSON-compatible output.
Thanks!
YAML 1.1 is almost a superset of JSON and therefore D-YAML can read most of existing JSON files via
Loader
.D-YAML also provides
Dumper
to dump a YAML object to an output range (of course it dumps in YAML format) and we can customize its output format withDumper#defaultCollectionStyle
andDumper#defaultScalarStyle
.However, we cannot dump a YAML object to an output range in JSON-compatible format due to the following reasons:
defaultCollectionStyle
anddefaultScalarStyle
seems to be used only if nodes are not generated viaLoader.fromString
norLoader.fromFile
to keep the original collection and scalar styles and I did not find a way to overwrite the original styles. It would be nice if we have an option to overwrite styles.enum str = q"EOS
auto root = Loader.fromString(str).load; auto dumper = dumper(); dumper.YAMLVersion = null; dumper.defaultCollectionStyle = CollectionStyle.flow;
dumper.dump(stdout.lockingTextWriter, root);
CollectionStyle
(type ofdefaultCollectionStyle
) hasflow
member for flow (i.e., JSON-compatible) style output for sequences and mappings but there are no corresponding members inScalarStyle
to dump an object in JSON-compatible format for scalar types. It would be nice ifScalaStyle
has a member for JSON-compatible style for scalar types.