Python Gotcha's?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Apr 5 14:22:22 EDT 2012
On Thu, 05 Apr 2012 11:06:11 +0000, Duncan Booth wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>
>> JSON expects double-quote marks, not single:
>> v = json.loads("{'test':'test'}") fails v =
>> json.loads('{"test":"test"}') succeeds
>>
>>
> You mean JSON expects a string with valid JSON? Quelle surprise.
Actually, on further thought, and on reading the JSON RFC, I have decided
that this is a design bug and not merely a gotcha.
The relevant section of the RFC is this:
4. Parsers
A JSON parser transforms a JSON text into another representation. A
JSON parser MUST accept all texts that conform to the JSON grammar.
A JSON parser MAY accept non-JSON forms or extensions.
http://www.ietf.org/rfc/rfc4627.txt
So a valid parser is permitted to accept data which is not strictly JSON.
Given that both Javascript and Python (and I would argue, any sensible
modern language) allows both single and double quotation marks as
delimiters, the JSON parser should do the same. Failure to take advantage
of that is a design flaw.
Of course, the RFC goes on to say that a JSON generator MUST only
generate text which conforms to the JSON grammar. So a conforming
implementation would be perfectly entitled to accept, but not emit,
single-quote delimited strings.
--
Steven
More information about the Python-list
mailing list