Opened 17 years ago

Closed 17 years ago

Last modified 9 years ago

#5586 closed Uncategorized (wontfix)

Comments are not allowed in json fixtures

Reported by: jwickers@… Owned by: nobody
Component: Core (Serialization) Version: dev
Severity: Normal Keywords: json fixture comment
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When there are javascript comments in a json fixture Django cannot install them, it complains about:

Problem installing fixture '.../fixtures/initial_data.json': Expecting object: line 2 column 2 (char 3)

line 2 column 2 (char3) is the beginning of the comment (either /* ... */ or ... )

Change History (7)

comment:1 by jwickers@…, 17 years ago

Ok there is a quick workaround, in django/utils/simplejson/decoder.py
In the decode method i filter the javascript comments before treating them:

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

This is just a workaround for now, since it doesn't address comments in strings, and it will mess the error message line/column indications.

comment:2 by anonymous, 17 years ago

Has patch: set
Patch needs improvement: set

comment:3 by Matt Boersma, 17 years ago

JSON does not define any comment notation (see http://www.json.org/), so what you're describing may be legal JavaScript, but not JSON. I tried parsing a simple JSON file with and without a comment, and both simplejson and turbojson refused when the comment was present.

I think this should be "wontfix."

comment:4 by James Bennett, 17 years ago

Resolution: wontfix
Status: newclosed

Agreed. JSON is deliberately a subset of JavaScript notation, not the entirety of JavaScript notation, and comments are not part of it.

comment:5 by jwickers@…, 17 years ago

Fair enough, although comments are useful in a big JSON file.
So perhaps i should switch to another fixture format, but meanwhile i will keep my hack since it does the job anyway.

comment:6 by hobsonlane@…, 12 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

You could implement an ignore of any "comment" object attribute names (at the level of "pk") to make Django fixtures comment-friendly without violating the "strict subset of json" rule.

comment:7 by Fraser Harris, 9 years ago

Douglas Crockford has commented on cases like this:

"Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser."

Seems like something Django could do natively.

Note: See TracTickets for help on using tickets.
Back to Top