twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

Support JSON with Comments in `fromJson` function #2825

Closed felipecrs closed 1 year ago

felipecrs commented 1 year ago

Is your feature request related to a problem? Please describe.

I'm trying to use modify templates to handle both my VS Code settings.json and also my Windows Terminal settings.json. However, their formats are JSON with Comments, which differs from regular JSONs a little.

Currently, if there are comments in the JSON, the fromJson will silently load no value. Effectively, at the end of the modify template, it will basically clean everything up that was in the file previously.

$ printf "%s\n" "{" "  // This is a comment" '  "a": "whatever",' "}" | 
  chezmoi execute-template --with-stdin '{{ fromJson .chezmoi.stdin }}'
<no value>

Describe the solution you'd like

I believe it would be nice to make fromJson accept JSON with Comments as well (which is a superset of JSON), or, a new function like fromJsonc.

Describe alternatives you've considered

I have a .chezmoiscript that backs up my configuration before actually changing it so that if something wrong happens, I can still recover.

Additional context

I found this library for Go which seems to be actively maintained:

halostatue commented 1 year ago

I think that a new function (fromJsonc) would be better than adding JSONC support to fromJson, because you can reasonably do fromJson | toJson and get the input back out (possibly sorted, possibly pathologically unsorted because Go), but you can’t do that with JWCC. I don’t believe we would want to support a toJsonc.

felipecrs commented 1 year ago

I also think so, there is no reason to add a function toJsonc, especially considering that comments can't be retained anyway.

FranklinYu commented 1 year ago

I see that JWCC above links to a blog. Is that the formal specification? Is there any test suite to verify implementations?

twpayne commented 1 year ago

@FranklinYu your questions are answered in the blog post :)