federomero / pretty-json

Atom plugin. Format JSON documents.
MIT License
94 stars 23 forks source link

Try to convert JS objects to valid JSON #5

Closed brandom closed 10 years ago

brandom commented 10 years ago

This is a best effort approach using regular expressions. There may be a mature lib or better way to do this, but this will work in most cases.

federomero commented 10 years ago

Hi @brandom,

I'm afraid I can't merge this because it brakes quite easily (e.g. 'a: "b: c"' breaks).

Thanks for submitting your PR anyways

brandom commented 10 years ago

@federomero Like I said in the comments, it makes a best effort to convert invalid JSON into valid JSON before JSON.parse(). I wouldn't say it breaks because the string above would not have been parsed either way. The regex is only applied to invalid JSON and the original string is returned if the result cannot be parsed, just like the existing code.

My regex skills are lacking so I am sure this can be improved, but it doesn't break the existing functionality of prettifying valid JSON.