SublimePrettyJson icon indicating copy to clipboard operation
SublimePrettyJson copied to clipboard

support for c style comments

Open bastianh opened this issue 12 years ago • 1 comments

I know it's not something that is officially on www.json.org but it's a fairly common extension and many parsers allow comments and it would be nice if pretty json would allow them, too :)

maybe it could simply ignore stuff between /* */ ?

bastianh avatar May 24 '13 09:05 bastianh

Hi @bastianhoyer

what can i do is to strip off comments to avoid getting exception on parsing json with comments, but in this case you will loose comments after prettyfing, so i really dont know if its worth implementing

import re
import json

comment_rx = re.compile(r"""
/\* .*? \*/ |
// [^\n\r]*
""",re.S | re.X)


s = """
{
    /**dasfsd*/
    "use_entire_file_if_no_selection" : true,
    // test
    // tedfsf
    "indent" : 2,
    "sort_keys" : false, // test
    /*
     * sdfadsf
     */
    "ensure_ascii" : false
}
"""

s = comment_rx.sub('',s)
print(s)

print(json.dumps(json.loads(s)))

strips off all comments

dzhibas avatar May 24 '13 11:05 dzhibas