dlang-community / D-YAML

YAML parser and emitter for the D programming language
https://dlang-community.github.io/D-YAML/
Boost Software License 1.0
119 stars 37 forks source link

Feature Request: JSON-compatible dumper settings #284

Closed tom-tan closed 2 years ago

tom-tan commented 2 years ago

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 with Dumper#defaultCollectionStyle and Dumper#defaultScalarStyle.

However, we cannot dump a YAML object to an output range in JSON-compatible format due to the following reasons:

Herringway commented 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).

Herringway commented 2 years ago

Also, JSON scalars use both plain and double quoted styles, depending on their types

tom-tan commented 2 years ago

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 and Node.scalarStyle.

Unfortunately they are not available for user code because they have package visibility. Can we make them public?

Herringway commented 2 years ago

Unfortunately they are not available for user code because they have package visibility. Can we make them public?

Whoops! I meant Node.setStyle.

tom-tan commented 2 years ago

I meant Node.setStyle.

Oh, I missed it. I can use it to make a custom dumper for JSON-compatible output.

Thanks!