prantlf / jsonlint

JSON/CJSON/JSON5 parser, syntax & schema validator and pretty-printer with a command-line client, written in pure JavaScript.
http://prantlf.github.io/jsonlint/
MIT License
38 stars 9 forks source link

False difference detection when using "comments"? #26

Closed mgzenitech closed 3 months ago

mgzenitech commented 6 months ago

Test case:

jsonlint.json:

{
  "check": true,
  "comments": true,
  "continue": true,
  "enforce-double-quotes": true,
  "no-duplicate-keys": true,
  "sort-keys": true,
  "trailing-newline": true,
  "trim-trailing-commas": true
}

test.json

{
  "a": "b" //aaaa
}

test run:

jsonlint -f jsonlint.json test.json
File: test.json
1 hunk differs
===================================================================
--- test.json.orig
+++ test.json
@@ -1,3 +1,3 @@
 {
-  "a": "b" //aaaa
+  "a": "b"
 }

I'm a missing something in config?

prantlf commented 3 months ago

Yes, you are, but this one is tricky :-) TL;DR: Add this to jsonlint.json:

  "pretty-print": true

Options comments and trailing-commas make the parser ignore those two and they won't be available when the output is printed by default. If you want to retain comments or trailing commas in the output, you have to add the option pretty-print. It will try to retain also these two, which otherwise have no value in the parsed JSON.

Also, depending on your preference, you should add trailing-newline or no-trailing-newline to enforce the standard and avoid false difference reports.