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

support for c style comments #25

Open
bastianh opened this issue May 24, 2013 · 1 comment
Open

support for c style comments #25

bastianh opened this issue May 24, 2013 · 1 comment

Comments

@bastianh
Copy link

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 /* */ ?

@dzhibas
Copy link
Owner

dzhibas commented May 24, 2013

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

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

3 participants