#5586 closed Uncategorized (wontfix)
Comments are not allowed in json fixtures
Reported by: | 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 , 17 years ago
comment:2 by , 17 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
comment:3 by , 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 , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
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 , 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 , 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 , 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.
Ok there is a quick workaround, in django/utils/simplejson/decoder.py
In the decode method i filter the javascript comments before treating them:
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.