rust-cli / confy

🛋 Zero-boilerplate configuration management in Rust
Other
896 stars 59 forks source link

Thoughts on adding JSON as a serialization format #52

Open badtuple opened 3 years ago

badtuple commented 3 years ago

Are there any opinions on adding JSON as a serialization format? My project uses JSON because it requires interop between multiple components, and JSON's tooling is very strong and ubiquitous. There's also value in how restrictive the data model is. While I want to use confy, I can't justify adding another serialization format (and all the tooling requirements and conversion ambiguity that come with it) to my project.

I see that YAML is already a config option. Is there desire for adding one for JSON? If so, I would be happy to take it on and make a PR following the same general structure as the YAML feature. I understand there are arguments against JSON as a configuration format however (lack of comments for example) and don't want to jump in adding it if the maintainers have no desire for the feature.

matthiasbeyer commented 3 years ago

Isn't JSON a valid subset of YAML? That'd mean that you can just use the YAML backend for your JSON files...

badtuple commented 3 years ago

Yes YAML's inline form is a superset of JSON, but that doesn't make it trivial to write JSON in YAML. I don't want to trust that the input is well formed, I want to error out if anything is ambiguous or malformed.

Some examples of where this might be a problem:

Furthermore, there are many unofficial extensions to YAML that do things like import other files or allow running arbitrary code. Even if serde_yaml / confy doesn't do this, it means "valid" configs could work differently and have different meanings in different tools.

Rust's strong type system does provide decent resistance to many of these issues. But again, this project has a high degree of interop and it won't always be Rust reading it even if that's the only thing confy cares about.

Overall this probably seems very defensive. Technically if a person writes valid json and I parse it as YAML, it'll work in most scenarios. However, Hyrum's law and xkcd 1172 are not exaggerations. As soon as a config becomes valid (by people either figuring out that it's actually YAML and using it for convenience or preference, or by typo-ing their way in) then it will become very hard to change that behavior.

CKingX commented 2 years ago

Using JSON will also prevent ValueAfterTable TOML errors as well (and TOML Serde also states enums are not supported)