Code

Opened 7 years ago

Closed 7 years ago

Last modified 14 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 ... )

Attachments (0)

Change History (6)

comment:1 Changed 7 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 7 years ago by anonymous

  • Has patch set
  • Patch needs improvement set

comment:3 Changed 7 years ago by mboersma

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 7 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from new to 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 Changed 7 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 14 months ago by hobsonlane@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.