simplejson 1.1
simplejson is a simple, fast, complete, correct and extensible JSON encoder/decoder for Python 2.3+. It is pure Python code with no dependencies. It's now the recommended module for Python use by JSON.org (replacing json-py).
simplejson was previously named simple_json, but was renamed to comply with PEP 8 module naming guidelines.
simplejson 1.1 has a much bigger test suite, a full set of documentation and a couple new features.
- The encoder and decoder have been extended to understand NaN, Infinity, and -Infinity (but this can be turned off via allow_nan=False for strict JSON compliance)
- The decoder's scanner has been fixed so that it no longer accepts invalid JSON documents
- The decoder now reports line and column information as well as character numbers for easier debugging
- The encoder now has a circular reference checker, which can be optionally disabled with check_circular=False
- dump, dumps, load, loads now accept an optional cls kwarg to use an alternate JSONEncoder or JSONDecoder class for convenience.
- The read/write compatibility shim for json-py now have deprecation warnings
Hi,
Just wanted to let you know that I am using simplejson on http://www.labelr.com.
There is a “bug”: it does not encodes new lines. I try to eval json data returned from server, and I get javascript parsing error if data included new line anywhere, as its no longer a valid string. I don’t know if its bug or intended behaviour, but if we could assume that json data can always be passed to eval without worries, it would be really cool. [right now i do a string replace for \n/\r=> , which works out fine as I was anyways going to do it while converting it to html, but may not be acceptable otherwise.
Thanks for the cool library tho.
Comment by Amit Upadhyay — 2006-02-01 @ 7:05 am
simplejson does not have any bugs with regard to newlines. The unit tests cover fully cover the JSON spec and it passes all of the JSON.org conformance tests. This must be a problem in usage.
Comment by bob — 2006-02-01 @ 2:21 pm
I agree with Bob, they are not bugs in simplejson’s implementation, just some gotchas on my side while using them with javascript. Things to note:
* if a string contains json encoded data, the right was to eval is: eval(’('+json_str+’)') and not eval(json_str) as I previously thought.
* simplejson escapes backslashes, while passing it around make sure this is preserved.
Thanks for all the help!
Comment by Amit Upadhyay — 2006-02-01 @ 11:08 pm
simplejson returns JSON. The way it “escapes backslashes” is part of the specification. If it didn’t return the data the way it does, then it wouldn’t be a compliant JSON encoder.
Comment by bob — 2006-02-01 @ 11:23 pm
I’ve got two requests. First, could the decoder use a relative import — the absolute import makes it hard to use simplejson as an external svn module. Second, while it’s not required by JSON, could you add an extra space after the colon? I’d like to mix JSON and YAML, and this extra space is problematic; we’re working on a YAML fix — but it will take some time to coordinate.
Thanks!
Index: encoder.py
===================================================================
— encoder.py (revision 869)
+++ encoder.py (working copy)
@@ -181,7 +181,7 @@
else:
yield ‘, ‘
yield encoder(key)
- yield ‘:’
+ yield ‘: ‘
for chunk in self._iterencode(value, markers):
yield chunk
yield ‘}’
Index: decoder.py
===================================================================
— decoder.py (revision 869)
+++ decoder.py (working copy)
@@ -3,7 +3,7 @@
“”"
import re
-from simplejson.scanner import Scanner, pattern
+from scanner import Scanner, pattern
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
Comment by Clark Evans — 2006-02-16 @ 9:37 am