Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace comments with spaces instead of stripping #119

Open
bcowgill opened this issue Apr 20, 2016 · 5 comments
Open

Replace comments with spaces instead of stripping #119

bcowgill opened this issue Apr 20, 2016 · 5 comments
Assignees

Comments

@bcowgill
Copy link

So that error messages from other tools which parse the json will match up as close as possible to the source json5

@jordanbtucker
Copy link
Member

I'm not sure I follow. If your JSON5 file is invalid, then you'll get errors when you try to compile it to JSON. A JSON file generated from a JSON5 source file would not have any errors.

If you're talking about application errors not related to JSON syntax (e.g. the JSON consuming application expected a number instead of a string) can you provide an example where replacing comments with a space instead of stripping them would match up more closely with the source file?

@bcowgill
Copy link
Author

I mean what you said in your second paragraph. I don't have a real world example yet, as far as JSON is concerned. As in, this is not stopping me. I have felt this pain before using other source code translators though. As you say, if a consuming app reported an error as line 5 column 32
then the removal of commented lines would throw the line number off and any inline comments
would throw the column count off.

While looking for a commented JSON solution I saw this one:

https://github.com/sindresorhus/strip-json-comments

Which mentions that feature as an option and thought I'd post it here as well.

BTW json5 worked just perfectly for my project yesterday so thanks for that!

@jordanbtucker
Copy link
Member

Okay, it makes more sense after reading the README for strip-json-comments.

Comments are replaced with whitespace, meaning spaces and new lines where appropriate, to keep the lines and columns matching. Here's some examples:

{
  // happy
  "earth": "day"
}

{

  "earth": "day"
}
{
  "earth-day-2016": /* 4/22/2016 in ms */ 1461308400000
}

{
  "earth-day-2016":                       1461308400000
}
{
  "first-earth-day":
  /* Earth Day was founded by
     Senator Gaylord Nelson on
     April 22, 1970 */
  "1970-04-22T00:00:00.000Z"
}

{
  "first-earth-day":



  "1970-04-22T00:00:00.000Z"
}

@jordanbtucker
Copy link
Member

I don't think this makes sense for this project since JSON5 to JSON is not a one-to-one compilation even without comments. JSON5 documents are normalized to JSON as if you used the JSON.stringify function.

Implementing sourcemaps might be a better general option.

@bcowgill
Copy link
Author

I realised it also wouldn't help in the case where you split lines using \ as the following line numbers would also be out of alignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants