Opened 9 years ago

Closed 9 years ago

Last modified 9 months ago

#5586 closed Uncategorized (wontfix)

Comments are not allowed in json fixtures

Reported by: jwickers@… Owned by: nobody
Component: Core (Serialization) Version: master
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 Changed 9 years ago by jwickers@…

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

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 Changed 9 years ago by anonymous

Has patch: set
Patch needs improvement: set

comment:3 Changed 9 years ago by Matt Boersma

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 Changed 9 years ago by James Bennett

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 Changed 9 years ago by jwickers@…

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 Changed 4 years ago by hobsonlane@…

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 Changed 9 months ago by Fraser Harris

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