From 5aa76448ef5a7a4cb53540c58c5cf42a04e0a4b2 Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Sat, 31 Dec 2005 21:13:05 +0000 Subject: simplejson git-svn-id: http://simplejson.googlecode.com/svn/trunk@3 a4795897-2c25-0410-b006-0d3caba88fa1 --- docs/class-simple_json.JSONDecoder.html | 186 +++++++++++++ docs/class-simple_json.JSONEncoder.html | 199 ++++++++++++++ docs/index.html | 448 ++++++++++++++++++++++++++++++++ docs/layout.css | 208 +++++++++++++++ docs/module-index.html | 73 ++++++ docs/module-simple_json-index.html | 133 ++++++++++ docs/module-simple_json.html | 448 ++++++++++++++++++++++++++++++++ docs/pudge.css | 60 +++++ docs/rst.css | 142 ++++++++++ docs/simple_json/__init__.py.html | 263 +++++++++++++++++++ setup.py | 45 ++++ simple_json/__init__.py | 193 ++++++++++++++ simple_json/decoder.py | 258 ++++++++++++++++++ simple_json/encoder.py | 275 ++++++++++++++++++++ simple_json/scanner.py | 67 +++++ simple_json/tests/__init__.py | 0 simple_json/tests/test_fail.py | 70 +++++ simple_json/tests/test_pass1.py | 72 +++++ simple_json/tests/test_pass2.py | 11 + simple_json/tests/test_pass3.py | 16 ++ simple_json/tests/test_recursion.py | 62 +++++ 21 files changed, 3229 insertions(+) create mode 100644 docs/class-simple_json.JSONDecoder.html create mode 100644 docs/class-simple_json.JSONEncoder.html create mode 100644 docs/index.html create mode 100644 docs/layout.css create mode 100644 docs/module-index.html create mode 100644 docs/module-simple_json-index.html create mode 100644 docs/module-simple_json.html create mode 100644 docs/pudge.css create mode 100644 docs/rst.css create mode 100644 docs/simple_json/__init__.py.html create mode 100644 setup.py create mode 100644 simple_json/__init__.py create mode 100644 simple_json/decoder.py create mode 100644 simple_json/encoder.py create mode 100644 simple_json/scanner.py create mode 100644 simple_json/tests/__init__.py create mode 100644 simple_json/tests/test_fail.py create mode 100644 simple_json/tests/test_pass1.py create mode 100644 simple_json/tests/test_pass2.py create mode 100644 simple_json/tests/test_pass3.py create mode 100644 simple_json/tests/test_recursion.py diff --git a/docs/class-simple_json.JSONDecoder.html b/docs/class-simple_json.JSONDecoder.html new file mode 100644 index 0000000..e8c4bc4 --- /dev/null +++ b/docs/class-simple_json.JSONDecoder.html @@ -0,0 +1,186 @@ + + + + + simple_json.JSONDecoder -- Simple JSON &lt;<a class="reference" href="http://json.org">http://json.org</a>&gt; decoder + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + + + details + + + tree + + + +
+
+ +
+ +

+ JSONDecoder +

+

+ Simple JSON <http://json.org> decoder +

+ +
+ +
+
+
+
+

Performs the following translations in decoding:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
trueTrue
falseFalse
nullNone
+

It also understands NaN, Infinity, and -Infinity as +their corresponding float values, which is outside the JSON spec.

+
+ +
+ + + + + + + + +

Methods

+
+ + +

+ f + + __init__(self, encoding=None) + ... +

+
+ + + + +
+
+ + + +

+ + + + See + the source + for more information. + +

+
+ + +
+ + \ No newline at end of file diff --git a/docs/class-simple_json.JSONEncoder.html b/docs/class-simple_json.JSONEncoder.html new file mode 100644 index 0000000..dc85ea1 --- /dev/null +++ b/docs/class-simple_json.JSONEncoder.html @@ -0,0 +1,199 @@ + + + + + simple_json.JSONEncoder -- Extensible JSON &lt;<a class="reference" href="http://json.org">http://json.org</a>&gt; encoder for Python data structures. + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + + + details + + + tree + + + +
+
+ +
+ +

+ JSONEncoder +

+

+ Extensible JSON <http://json.org> encoder for Python data structures. +

+ +
+ +
+
+
+
+

Supports the following objects and types by default:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull
+

To extend this to recognize other objects, subclass and implement a +.default(o) method with another method that returns a serializable +object for o if possible, otherwise it should call the superclass +implementation (to raise TypeError).

+
+ +
+ + + + + + + + +

Methods

+
+ + +

+ f + + __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True) + ... +

+
+ +

+ Constructor for JSONEncoder, with sensible defaults. +

+

If skipkeys is False, then it is a TypeError to attempt +encoding of keys that are not str, int, long, float or None. If +skipkeys is True, such items are simply skipped.

+

If ensure_ascii is True, the output is guaranteed to be str +objects with all incoming unicode characters escaped. If ensure_ascii +is false, the output will be unicode object.

+

If check_circular is True, then lists, dicts, and custom encoded +objects will be checked for circular references during encoding to +prevent an infinite recursion (which would cause an OverflowError). +Otherwise, no such check takes place.

+

If allow_nan is True, then NaN, Infinity, and -Infinity will be +encoded as such. This behavior is not JSON specification compliant, +but is consistent with most JavaScript based encoders and decoders. +Otherwise, it will be a ValueError to encode such floats.

+ +
+
+ + + +

+ + + + See + the source + for more information. + +

+
+ + +
+ + \ No newline at end of file diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..0870129 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,448 @@ + + + + + simple_json -- A simple, fast, extensible JSON encoder and decoder + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + + + details + + + tree + + + +
+
+ +
+ +

+ simple_json +

+

+ A simple, fast, extensible JSON encoder and decoder +

+ +
+ +
+
+
+
+

JSON (JavaScript Object Notation) <http://json.org> is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format.

+

simple_json exposes an API familiar to uses of the standard library +marshal and pickle modules.

+

Encoding basic Python object hierarchies:

+
+>>> import simple_json
+>>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
+'["foo", {"bar":["baz", null, 1.0, 2]}]'
+>>> print simple_json.dumps("\"foo\bar")
+"\"foo\bar"
+>>> print simple_json.dumps(u'\u1234')
+"\u1234"
+>>> print simple_json.dumps('\\')
+"\\"
+>>> from StringIO import StringIO
+>>> io = StringIO()
+>>> simple_json.dump(['streaming API'], io)
+>>> io.getvalue()
+'["streaming API"]'
+
+

Decoding JSON:

+
+>>> import simple_json
+>>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
+[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
+>>> simple_json.loads('"\\"foo\\bar"')
+u'"foo\x08ar'
+>>> from StringIO import StringIO
+>>> io = StringIO('["streaming API"]')
+>>> simple_json.load(io)
+[u'streaming API']
+
+

Extending JSONEncoder:

+
+>>> import simple_json
+>>> class ComplexEncoder(simple_json.JSONEncoder):
+...     def default(self, obj):
+...         if isinstance(obj, complex):
+...             return [obj.real, obj.imag]
+...         return simple_json.JSONEncoder.default(self, obj)
+...
+>>> dumps(2 + 1j, cls=ComplexEncoder)
+'[2.0, 1.0]'
+>>> ComplexEncoder().encode(2 + 1j)
+'[2.0, 1.0]'
+>>> list(ComplexEncoder().iterencode(2 + 1j))
+['[', '2.0', ', ', '1.0', ']']
+
+

Note that the JSON produced by this module is a subset of YAML, +so it may be used as a serializer for that as well.

+
+ +
+ + + + + + + + +

Functions

+
+ + +

+ f + + dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None) + ... +

+
+ +

+ Serialize obj as a JSON formatted stream to fp (a +.write()-supporting file-like object). +

+

If skipkeys is True then dict keys that are not basic types +(str, unicode, int, long, float, bool, None) +will be skipped instead of raising a TypeError.

+

If ensure_ascii is False, then the some chunks written to fp +may be unicode instances, subject to normal Python str to +unicode coercion rules. Unless fp.write() explicitly +understands unicode (as in codecs.getwriter()) this is likely +to cause an error.

+

If check_circular is False, then the circular reference check +for container types will be skipped and a circular reference will +result in an OverflowError (or worse).

+

If allow_nan is False, then it will be a ValueError to +serialize out of range float values (nan, inf, -inf) +in strict compliance of the JSON specification, instead of using the +JavaScript equivalents (NaN, Infinity, -Infinity).

+

To use a custom JSONEncoder subclass (e.g. one that overrides the +.default() method to serialize additional types), specify it with +the cls kwarg.

+ +
+
+
+ + +

+ f + + dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None) + ... +

+
+ +

+ Serialize obj to a JSON formatted str. +

+

If skipkeys is True then dict keys that are not basic types +(str, unicode, int, long, float, bool, None) +will be skipped instead of raising a TypeError.

+

If ensure_ascii is False, then the return value will be a +unicode instance subject to normal Python str to unicode +coercion rules instead of being escaped to an ASCII str.

+

If check_circular is False, then the circular reference check +for container types will be skipped and a circular reference will +result in an OverflowError (or worse).

+

If allow_nan is False, then it will be a ValueError to +serialize out of range float values (nan, inf, -inf) in +strict compliance of the JSON specification, instead of using the +JavaScript equivalents (NaN, Infinity, -Infinity).

+

To use a custom JSONEncoder subclass (e.g. one that overrides the +.default() method to serialize additional types), specify it with +the cls kwarg.

+ +
+
+
+ + +

+ f + + load(fp, encoding=None, cls=None) + ... +

+
+ +

+ Deserialize fp (a .read()-supporting file-like object containing +a JSON document) to a Python object. +

+

If the contents of fp is encoded with an ASCII based encoding other +than utf-8 (e.g. latin-1), then an appropriate encoding name must +be specified. Encodings that are not ASCII based (such as UCS-2) are +not allowed, and should be wrapped with +codecs.getreader(fp)(encoding), or simply decoded to a unicode +object and passed to loads()

+

To use a custom JSONDecoder subclass, specify it with the cls +kwarg.

+ +
+
+
+ + +

+ f + + loads(s, encoding=None, cls=None) + ... +

+
+ +

+ Deserialize s (a str or unicode instance containing a JSON +document) to a Python object. +

+

If s is a str instance and is encoded with an ASCII based encoding +other than utf-8 (e.g. latin-1) then an appropriate encoding name +must be specified. Encodings that are not ASCII based (such as UCS-2) +are not allowed and should be decoded to unicode first.

+

To use a custom JSONDecoder subclass, specify it with the cls +kwarg.

+ +
+
+ + + + + + +

Classes

+
+

+ C + + JSONEncoder(...) + ... +

+
+ +

+ Extensible JSON <http://json.org> encoder for Python data structures. +

+

Supports the following objects and types by default:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull
+

To extend this to recognize other objects, subclass and implement a +.default(o) method with another method that returns a serializable +object for o if possible, otherwise it should call the superclass +implementation (to raise TypeError).

+ + +

+ This class contains + 1 member. +

+
+
+
+

+ C + + JSONDecoder(...) + ... +

+
+ +

+ Simple JSON <http://json.org> decoder +

+

Performs the following translations in decoding:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
trueTrue
falseFalse
nullNone
+

It also understands NaN, Infinity, and -Infinity as +their corresponding float values, which is outside the JSON spec.

+ + +

+ This class contains + 1 member. +

+
+
+ + + +

+ + + + See + the source + for more information. + +

+
+ + +
+ + \ No newline at end of file diff --git a/docs/layout.css b/docs/layout.css new file mode 100644 index 0000000..a45c58b --- /dev/null +++ b/docs/layout.css @@ -0,0 +1,208 @@ +@import url("pudge.css"); + +/* Basic Layout +---------------------------------- */ + +body { + margin-left: 1em; + margin-right: 1em; + max-width: 50em; +} +body { + font-size: .80em; + color: #111; +} +div#main-content { + margin-left: 1em; + margin-right: 1em; + max-width: 47em; +} + +/* Top Navigation +----------------------------------- */ + +div#top-nav { + background: #373; + padding: .3em .3em; + margin: 0; + margin-bottom: 1.5em; + font-size: 90%; +} +div#top-nav #doc-title { + font-size: 140%; + font-weight: bold; + margin: 0; + padding-top: .32em; + padding-right: 1em; + padding-left: .3em; + color: #9c9; + float: left; +} +div#top-nav a { + color: #6a6; + text-decoration: none; +} +div#top-nav .online-navigation a:hover, +div#top-nav h1 a +{ + color: #9c9; +} + +/* Footer +----------------------------------- */ + +div#footer { + text-align: right; + margin-top: 1.5em; + border-top: 1px solid #373; + padding-top: .5em; + font-size: 80%; + color: #666; +} +div#footer a { + color: #373; + text-decoration: none; +} +div#footer p { + margin: .2em 1em; +} + +/* Basic Style +----------------------------------- */ +h1, h2, h3, h4 { + margin: 1em auto; + font-family: 'Trebuchet MS', 'Verdana', Sans-serif; + color: #555; + font-weight: normal; +} +h1 { font-size: 200% } +h2 { font-size: 170% } +h3 { font-size: 150% } +h4 { font-size: 120% } +a:link { color: #060; font-weight: bold } +a:visited { color: #060; text-decoration: none } +hr { margin: auto 12px } +pre { color: #036 } + +dl dt { + font-style: italic; + margin-top: .5em; + font-weight: bold; + color: #555; +} +hr { + color: #373; + background-color: #373; + height: 1px; + border: 0; + width: 100%; + margin: 2em 0; +} + +/* Pudge Elements +--------------------------------- */ + +h1.pudge-member-page-heading { + font-size: 300%; + margin-top: .4em; + margin-bottom: .4em; +} +h4.pudge-member-page-subheading { + font-size: 150%; + font-style: italic; + margin-top: -1.3em; + margin-left: 2em; + color: #999; +} +p.pudge-member-blurb { + font-style: italic; + font-weight: bold; + font-size: 120%; + margin-top: 0.2em; + color: #6a6; +} +div.pudge-module-doc { + max-width: 45em; +} +div.pudge-section { + margin-left: 2em; + max-width: 45em; +} +p.pudge-member-blurb { + font-style: italic; + font-weight: bold; + font-size: 120%; +} + +/* Section Navigation +----------------------------------- */ + +div#pudge-section-nav +{ + margin: 1em 0 1.5em 0; + padding: 0; + height: 20px; +} + +div#pudge-section-nav ul { + border: 0; + margin: 0; + padding: 0; + list-style-type: none; + text-align: center; + border-right: 1px solid #aaa; +} +div#pudge-section-nav ul li +{ + display: block; + float: left; + text-align: center; + padding: 0; + margin: 0; +} + +div#pudge-section-nav ul li .pudge-section-link, +div#pudge-section-nav ul li .pudge-missing-section-link +{ + background: #aaa; + width: 11em; + height: 1.8em; + border: 0; + border-right: 3px solid #fff; + padding: 0; + margin: 0 0 10px 0; + color: #ddd; + text-decoration: none; + display: block; + text-align: center; + font: normal 10px/18px "Lucida Grande", "Lucida Sans Unicode", verdana, lucida, sans-serif; + font-weight: bold; + cursor: hand; +} + +div#pudge-section-nav ul li a:hover +{ + color: #fff; + background: #393; +} + +div#pudge-section-nav ul li .pudge-section-link +{ + background: #373; + color: #9c9; +} + +/* Module Lists +----------------------------------- */ +dl.pudge-module-list dt { + font-style: normal; + font-size: 110%; +} +dl.pudge-module-list dd { + color: #555; +} + +/* misc */ +pre, tt { + font-size: 120%; +} diff --git a/docs/module-index.html b/docs/module-index.html new file mode 100644 index 0000000..4775597 --- /dev/null +++ b/docs/module-index.html @@ -0,0 +1,73 @@ + + + + + simple_json 1.1 -- Module Reference + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + +
+
+ +
+ +

+ simple_json 1.1 +

+

+ Module Reference +

+ +

Packages and Modules

+ +
+ +
+ + + + + simple_json + +
+
+ A simple, fast, extensible JSON encoder and decoder +
+ +
+ +
+ + +
+ + \ No newline at end of file diff --git a/docs/module-simple_json-index.html b/docs/module-simple_json-index.html new file mode 100644 index 0000000..f40a51f --- /dev/null +++ b/docs/module-simple_json-index.html @@ -0,0 +1,133 @@ + + + + + Index of simple_json module + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + + + details + + + tree + + + +
+
+ +
+ +

Index of the simple_json module

+ +
    +
  • + m + + simple_json + + ... + + - A simple, fast, extensible JSON encoder and decoder + +
      + + + + + +
    • + f + + load + + ... + + - Deserialize fp (a .read()-supporting file-like object containing +a JSON document) to a Python object. + + +
    • + +
    • + f + + dump + + ... + + - Serialize obj as a JSON formatted stream to fp (a +.write()-supporting file-like object). + + +
    • + +
    • + f + + dumps + + ... + + - Serialize obj to a JSON formatted str. + + +
    • + +
    • + f + + loads + + ... + + - Deserialize s (a str or unicode instance containing a JSON +document) to a Python object. + + +
    • + + + +
    +
  • +
+ + + +
+ + +
+ + \ No newline at end of file diff --git a/docs/module-simple_json.html b/docs/module-simple_json.html new file mode 100644 index 0000000..0870129 --- /dev/null +++ b/docs/module-simple_json.html @@ -0,0 +1,448 @@ + + + + + simple_json -- A simple, fast, extensible JSON encoder and decoder + + + + +
+ +
+

+ simple_json 1.1

+
+ index + + + + + +
+ + + + + simple_json + + + + details + + + tree + + + +
+
+ +
+ +

+ simple_json +

+

+ A simple, fast, extensible JSON encoder and decoder +

+ +
+ +
+
+
+
+

JSON (JavaScript Object Notation) <http://json.org> is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format.

+

simple_json exposes an API familiar to uses of the standard library +marshal and pickle modules.

+

Encoding basic Python object hierarchies:

+
+>>> import simple_json
+>>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
+'["foo", {"bar":["baz", null, 1.0, 2]}]'
+>>> print simple_json.dumps("\"foo\bar")
+"\"foo\bar"
+>>> print simple_json.dumps(u'\u1234')
+"\u1234"
+>>> print simple_json.dumps('\\')
+"\\"
+>>> from StringIO import StringIO
+>>> io = StringIO()
+>>> simple_json.dump(['streaming API'], io)
+>>> io.getvalue()
+'["streaming API"]'
+
+

Decoding JSON:

+
+>>> import simple_json
+>>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
+[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
+>>> simple_json.loads('"\\"foo\\bar"')
+u'"foo\x08ar'
+>>> from StringIO import StringIO
+>>> io = StringIO('["streaming API"]')
+>>> simple_json.load(io)
+[u'streaming API']
+
+

Extending JSONEncoder:

+
+>>> import simple_json
+>>> class ComplexEncoder(simple_json.JSONEncoder):
+...     def default(self, obj):
+...         if isinstance(obj, complex):
+...             return [obj.real, obj.imag]
+...         return simple_json.JSONEncoder.default(self, obj)
+...
+>>> dumps(2 + 1j, cls=ComplexEncoder)
+'[2.0, 1.0]'
+>>> ComplexEncoder().encode(2 + 1j)
+'[2.0, 1.0]'
+>>> list(ComplexEncoder().iterencode(2 + 1j))
+['[', '2.0', ', ', '1.0', ']']
+
+

Note that the JSON produced by this module is a subset of YAML, +so it may be used as a serializer for that as well.

+
+ +
+ + + + + + + + +

Functions

+
+ + +

+ f + + dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None) + ... +

+
+ +

+ Serialize obj as a JSON formatted stream to fp (a +.write()-supporting file-like object). +

+

If skipkeys is True then dict keys that are not basic types +(str, unicode, int, long, float, bool, None) +will be skipped instead of raising a TypeError.

+

If ensure_ascii is False, then the some chunks written to fp +may be unicode instances, subject to normal Python str to +unicode coercion rules. Unless fp.write() explicitly +understands unicode (as in codecs.getwriter()) this is likely +to cause an error.

+

If check_circular is False, then the circular reference check +for container types will be skipped and a circular reference will +result in an OverflowError (or worse).

+

If allow_nan is False, then it will be a ValueError to +serialize out of range float values (nan, inf, -inf) +in strict compliance of the JSON specification, instead of using the +JavaScript equivalents (NaN, Infinity, -Infinity).

+

To use a custom JSONEncoder subclass (e.g. one that overrides the +.default() method to serialize additional types), specify it with +the cls kwarg.

+ +
+
+
+ + +

+ f + + dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None) + ... +

+
+ +

+ Serialize obj to a JSON formatted str. +

+

If skipkeys is True then dict keys that are not basic types +(str, unicode, int, long, float, bool, None) +will be skipped instead of raising a TypeError.

+

If ensure_ascii is False, then the return value will be a +unicode instance subject to normal Python str to unicode +coercion rules instead of being escaped to an ASCII str.

+

If check_circular is False, then the circular reference check +for container types will be skipped and a circular reference will +result in an OverflowError (or worse).

+

If allow_nan is False, then it will be a ValueError to +serialize out of range float values (nan, inf, -inf) in +strict compliance of the JSON specification, instead of using the +JavaScript equivalents (NaN, Infinity, -Infinity).

+

To use a custom JSONEncoder subclass (e.g. one that overrides the +.default() method to serialize additional types), specify it with +the cls kwarg.

+ +
+
+
+ + +

+ f + + load(fp, encoding=None, cls=None) + ... +

+
+ +

+ Deserialize fp (a .read()-supporting file-like object containing +a JSON document) to a Python object. +

+

If the contents of fp is encoded with an ASCII based encoding other +than utf-8 (e.g. latin-1), then an appropriate encoding name must +be specified. Encodings that are not ASCII based (such as UCS-2) are +not allowed, and should be wrapped with +codecs.getreader(fp)(encoding), or simply decoded to a unicode +object and passed to loads()

+

To use a custom JSONDecoder subclass, specify it with the cls +kwarg.

+ +
+
+
+ + +

+ f + + loads(s, encoding=None, cls=None) + ... +

+
+ +

+ Deserialize s (a str or unicode instance containing a JSON +document) to a Python object. +

+

If s is a str instance and is encoded with an ASCII based encoding +other than utf-8 (e.g. latin-1) then an appropriate encoding name +must be specified. Encodings that are not ASCII based (such as UCS-2) +are not allowed and should be decoded to unicode first.

+

To use a custom JSONDecoder subclass, specify it with the cls +kwarg.

+ +
+
+ + + + + + +

Classes

+
+

+ C + + JSONEncoder(...) + ... +

+
+ +

+ Extensible JSON <http://json.org> encoder for Python data structures. +

+

Supports the following objects and types by default:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull
+

To extend this to recognize other objects, subclass and implement a +.default(o) method with another method that returns a serializable +object for o if possible, otherwise it should call the superclass +implementation (to raise TypeError).

+ + +

+ This class contains + 1 member. +

+
+
+
+

+ C + + JSONDecoder(...) + ... +

+
+ +

+ Simple JSON <http://json.org> decoder +

+

Performs the following translations in decoding:

+ +++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
trueTrue
falseFalse
nullNone
+

It also understands NaN, Infinity, and -Infinity as +their corresponding float values, which is outside the JSON spec.

+ + +

+ This class contains + 1 member. +

+
+
+ + + +

+ + + + See + the source + for more information. + +

+
+ + +
+ + \ No newline at end of file diff --git a/docs/pudge.css b/docs/pudge.css new file mode 100644 index 0000000..b370217 --- /dev/null +++ b/docs/pudge.css @@ -0,0 +1,60 @@ +/* Layout +----------------------------------- */ + +@import url("rst.css"); + +/* Pudge Elements +----------------------------------- */ +.note { font-size: 90% } +h4.pudge-member-name { + font-size: 110%; + margin-bottom: 0; +} +h4.pudge-member-name a.obj-link { + font-weight: bold; + text-decoration: none; +} +h4.pudge-member-name .prefix { + font-style: oblique; + padding-right: 6px; + font-weight: bold; + color: #c9c; +} +h1.pudge-member-page-heading { + font-size: 250%; +} +h4.pudge-member-page-subheading { + font-size: 150%; + font-style: italic; +} +h4.pudge-member-page-subheading p { + display: inline; +} +div.pudge-member { + margin-top: 1.5em; + margin-bottom: 1.5em; +} +ul.pudge-module-index { + margin-left: 0; + padding-left: 0; +} +ul.pudge-module-index ul { + padding-left: 1.5em; + margin-left: 0; +} +ul.pudge-module-index li { + list-style-type: none; +} +ul.pudge-module-index .prefix { + font-style: oblique; + padding-right: 6px; + font-size: 90%; + font-weight: bold; + color: purple; +} +ul.pudge-module-index a { + text-decoration: none; +} +div.pudge-section { + margin-left: 2em; +} \ No newline at end of file diff --git a/docs/rst.css b/docs/rst.css new file mode 100644 index 0000000..f48c723 --- /dev/null +++ b/docs/rst.css @@ -0,0 +1,142 @@ +/* Headings + ------------------------------- */ + +.rst h1 { font-size: 110% } +.rst h2 { font-size: 100% } + +/*.rst-doc h1 { font-size: 200% } +.rst-doc h2 { font-size: 140% } +.rst-doc h3 { font-size: 110% }*/ +.rst-doc h1.title { font-size: 220% } +.rst-doc h1 a, .rst-doc h2 a, .rst-doc h3 a { + color: inherit ; + font-weight: inherit; + text-decoration: inherit; +} + +/* Blockquotes + ------------------------------- */ + +.rst blockquote, +.rst-doc blockquote { + font-style: italic; + font-family: Georgia, serif; + max-width: 30em; +} + +/* Document Info + ------------------------------- */ +.rst-doc table.docinfo { + margin: 2em 0 + width: 100%; +} +.rst-doc table.docinfo th { + text-align: left ; + padding-right: 2em; + color: #555; +} +.rst-doc table.docinfo td { + text-align: left +} + +/* Field Lists + ------------------------------- */ + +.rst table.field-list, +.rst-doc table.field-list { + border: 0; + margin-left: 0; +} +.rst table.field-list ul, +.rst-doc table.field-list ul { + margin-left: 0; +} +.rst-doc table.field-list ul { + border: 0; + margin-left: 2em; + background: #fff; + color: #119; +} + +/* Tables + ------------------------------- */ + +.rst table.docutils, +.rst-doc table.docutils { + border: 0; +} +.rst table.docutils th, +.rst-doc table.docutils th { + border: 0; + background: #777; + color: #fff; + padding: 3px; +} +.rst table.docutils td, +.rst-doc table.docutils td { + border: 0; + border-bottom: 1px solid #ccc; + padding-bottom: 2px; +} + +/* Contents and Back References + ------------------------------- */ + +.rst-doc div.contents { + margin: 2em inherit; +} +.rst-doc div.contents ul { + margin-left: 0; + padding-left: 2em; + line-height: 150%; +} +.rst-doc div.contents ul li { + font-weight: bold; + list-style-type: none; +} +.rst-doc div.contents p.topic-title { + font-size: 160%; + font-weight: normal; + color: #555; + margin-top: .5em; +} +.rst-doc .contents .reference, +.rst-doc .toc-backref { + text-decoration: none; +} + +/* Admonitions + ------------------------------- */ + +.rst-doc div.admonition, +.rst-doc div.warning, +.rst-doc div.note { + margin: 2.5em 6em; + padding: .6em 2.5em; + background: #ddf; + border: 2px solid #ccc; + font-family: Georgia, serif; + color: #333; +} +.rst-doc div.warning { + background: #ff8; + border-color: #fe2; +} +.rst-doc div .admonition-title { + font-weight: bold; + font-style: italic; + color: #223; + font-size: 110%; + font-family: sans-serif; +} + +/* Misc + ------------------------------- */ + +tt.literal { + color: #333; +} +.footnote-reference { + vertical-align: super; + font-size: 20%; +} \ No newline at end of file diff --git a/docs/simple_json/__init__.py.html b/docs/simple_json/__init__.py.html new file mode 100644 index 0000000..cc8e849 --- /dev/null +++ b/docs/simple_json/__init__.py.html @@ -0,0 +1,263 @@ +/src/simple_json/simple_json/__init__.py + +
0001r"""
+0002A simple, fast, extensible JSON encoder and decoder
+0003
+0004JSON (JavaScript Object Notation) <http://json.org> is a subset of
+0005JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
+0006interchange format.
+0007
+0008simple_json exposes an API familiar to uses of the standard library
+0009marshal and pickle modules.
+0010
+0011Encoding basic Python object hierarchies::
+0012    
+0013    >>> import simple_json
+0014    >>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
+0015    '["foo", {"bar":["baz", null, 1.0, 2]}]'
+0016    >>> print simple_json.dumps("\"foo\bar")
+0017    "\"foo\bar"
+0018    >>> print simple_json.dumps(u'\u1234')
+0019    "\u1234"
+0020    >>> print simple_json.dumps('\\')
+0021    "\\"
+0022    >>> from StringIO import StringIO
+0023    >>> io = StringIO()
+0024    >>> simple_json.dump(['streaming API'], io)
+0025    >>> io.getvalue()
+0026    '["streaming API"]'
+0027
+0028Decoding JSON::
+0029    
+0030    >>> import simple_json
+0031    >>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
+0032    [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
+0033    >>> simple_json.loads('"\\"foo\\bar"')
+0034    u'"foo\x08ar'
+0035    >>> from StringIO import StringIO
+0036    >>> io = StringIO('["streaming API"]')
+0037    >>> simple_json.load(io)
+0038    [u'streaming API']
+0039
+0040Extending JSONEncoder::
+0041    
+0042    >>> import simple_json
+0043    >>> class ComplexEncoder(simple_json.JSONEncoder):
+0044    ...     def default(self, obj):
+0045    ...         if isinstance(obj, complex):
+0046    ...             return [obj.real, obj.imag]
+0047    ...         return simple_json.JSONEncoder.default(self, obj)
+0048    ... 
+0049    >>> dumps(2 + 1j, cls=ComplexEncoder)
+0050    '[2.0, 1.0]'
+0051    >>> ComplexEncoder().encode(2 + 1j)
+0052    '[2.0, 1.0]'
+0053    >>> list(ComplexEncoder().iterencode(2 + 1j))
+0054    ['[', '2.0', ', ', '1.0', ']']
+0055    
+0056
+0057Note that the JSON produced by this module is a subset of YAML,
+0058so it may be used as a serializer for that as well.
+0059"""
+0060__version__ = '1.1'
+0061__all__ = [
+0062    'dump', 'dumps', 'load', 'loads',
+0063    'JSONDecoder', 'JSONEncoder',
+0064]
+0065
+0066from decoder import JSONDecoder
+0067from encoder import JSONEncoder
+0068
+0069def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
+0070        allow_nan=True, cls=None, **kw):
+0071    """
+0072    Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
+0073    ``.write()``-supporting file-like object).
+0074
+0075    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
+0076    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) 
+0077    will be skipped instead of raising a ``TypeError``.
+0078
+0079    If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``
+0080    may be ``unicode`` instances, subject to normal Python ``str`` to
+0081    ``unicode`` coercion rules.  Unless ``fp.write()`` explicitly
+0082    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
+0083    to cause an error.
+0084
+0085    If ``check_circular`` is ``False``, then the circular reference check
+0086    for container types will be skipped and a circular reference will
+0087    result in an ``OverflowError`` (or worse).
+0088
+0089    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
+0090    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
+0091    in strict compliance of the JSON specification, instead of using the
+0092    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
+0093
+0094    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
+0095    ``.default()`` method to serialize additional types), specify it with
+0096    the ``cls`` kwarg.
+0097    """
+0098    if cls is None:
+0099        cls = JSONEncoder
+0100    iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
+0101        check_circular=check_circular, allow_nan=allow_nan,
+0102        **kw).iterencode(obj)
+0103    # could accelerate with writelines in some versions of Python, at
+0104    # a debuggability cost
+0105    for chunk in iterable:
+0106        fp.write(chunk)
+0107
+0108def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
+0109        allow_nan=True, cls=None, **kw):
+0110    """
+0111    Serialize ``obj`` to a JSON formatted ``str``.
+0112
+0113    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
+0114    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) 
+0115    will be skipped instead of raising a ``TypeError``.
+0116
+0117    If ``ensure_ascii`` is ``False``, then the return value will be a
+0118    ``unicode`` instance subject to normal Python ``str`` to ``unicode``
+0119    coercion rules instead of being escaped to an ASCII ``str``.
+0120
+0121    If ``check_circular`` is ``False``, then the circular reference check
+0122    for container types will be skipped and a circular reference will
+0123    result in an ``OverflowError`` (or worse).
+0124
+0125    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
+0126    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
+0127    strict compliance of the JSON specification, instead of using the
+0128    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
+0129
+0130    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
+0131    ``.default()`` method to serialize additional types), specify it with
+0132    the ``cls`` kwarg.
+0133    """
+0134    if cls is None:
+0135        cls = JSONEncoder
+0136    return cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
+0137        check_circular=check_circular, allow_nan=allow_nan, **kw).encode(obj)
+0138
+0139def load(fp, encoding=None, cls=None, **kw):
+0140    """
+0141    Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
+0142    a JSON document) to a Python object.
+0143
+0144    If the contents of ``fp`` is encoded with an ASCII based encoding other
+0145    than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must
+0146    be specified.  Encodings that are not ASCII based (such as UCS-2) are
+0147    not allowed, and should be wrapped with
+0148    ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``
+0149    object and passed to ``loads()``
+0150    
+0151    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
+0152    kwarg.
+0153    """
+0154    if cls is None:
+0155        cls = JSONDecoder
+0156    return cls(encoding=encoding, **kw).decode(fp.read())
+0157
+0158def loads(s, encoding=None, cls=None, **kw):
+0159    """
+0160    Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
+0161    document) to a Python object.
+0162
+0163    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding
+0164    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name
+0165    must be specified.  Encodings that are not ASCII based (such as UCS-2)
+0166    are not allowed and should be decoded to ``unicode`` first.
+0167
+0168    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
+0169    kwarg.
+0170    """
+0171    if cls is None:
+0172        cls = JSONDecoder
+0173    return cls(encoding=encoding, **kw).decode(s)
+0174
+0175def read(s):
+0176    """
+0177    json-py API compatibility hook.  Use loads(s) instead.
+0178    """
+0179    import warnings
+0180    warnings.warn("simple_json.loads(s) should be used instead of read(s)",
+0181        DeprecationWarning)
+0182    return loads(s)
+0183
+0184def write(obj):
+0185    """
+0186    json-py API compatibility hook.  Use dumps(s) instead.
+0187    """
+0188    import warnings
+0189    warnings.warn("simple_json.dumps(s) should be used instead of write(s)",
+0190        DeprecationWarning)
+0191    return dumps(obj)
\ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f48cba0 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import ez_setup +ez_setup.use_setuptools() + +from setuptools import setup, find_packages + +VERSION = '1.1' +DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" +LONG_DESCRIPTION = """ +simple_json is a simple, fast, complete, correct and extensible +JSON encoder and decoder for Python 2.3+. It is +pure Python code with no dependencies. + +The encoder may be subclassed to provide serialization in any kind of +situation, without any special support by the objects to be serialized +(somewhat like pickle). + +The decoder can handle incoming JSON strings of any specified encoding +(UTF-8 by default). +""" + +CLASSIFIERS = filter(None, map(str.strip, +""" +Intended Audience :: Developers +License :: OSI Approved :: MIT License +Programming Language :: Python +Topic :: Software Development :: Libraries :: Python Modules +""".splitlines())) + +setup( + name="simple_json", + version=VERSION, + description=DESCRIPTION, + long_description=LONG_DESCRIPTION, + classifiers=CLASSIFIERS, + author="Bob Ippolito", + author_email="bob@redivi.com", + url="http://undefined.org/python/#simple_json", + license="MIT License", + packages=find_packages(exclude=['ez_setup']), + platforms=['any'], + test_suite="nose.collector", + zip_safe=True, +) diff --git a/simple_json/__init__.py b/simple_json/__init__.py new file mode 100644 index 0000000..5354999 --- /dev/null +++ b/simple_json/__init__.py @@ -0,0 +1,193 @@ +r""" +A simple, fast, extensible JSON encoder and decoder + +JSON (JavaScript Object Notation) is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format. + +simple_json exposes an API familiar to uses of the standard library +marshal and pickle modules. + +Encoding basic Python object hierarchies:: + + >>> import simple_json + >>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) + '["foo", {"bar":["baz", null, 1.0, 2]}]' + >>> print simple_json.dumps("\"foo\bar") + "\"foo\bar" + >>> print simple_json.dumps(u'\u1234') + "\u1234" + >>> print simple_json.dumps('\\') + "\\" + >>> from StringIO import StringIO + >>> io = StringIO() + >>> simple_json.dump(['streaming API'], io) + >>> io.getvalue() + '["streaming API"]' + +Decoding JSON:: + + >>> import simple_json + >>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') + [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] + >>> simple_json.loads('"\\"foo\\bar"') + u'"foo\x08ar' + >>> from StringIO import StringIO + >>> io = StringIO('["streaming API"]') + >>> simple_json.load(io) + [u'streaming API'] + +Extending JSONEncoder:: + + >>> import simple_json + >>> class ComplexEncoder(simple_json.JSONEncoder): + ... def default(self, obj): + ... if isinstance(obj, complex): + ... return [obj.real, obj.imag] + ... return simple_json.JSONEncoder.default(self, obj) + ... + >>> dumps(2 + 1j, cls=ComplexEncoder) + '[2.0, 1.0]' + >>> ComplexEncoder().encode(2 + 1j) + '[2.0, 1.0]' + >>> list(ComplexEncoder().iterencode(2 + 1j)) + ['[', '2.0', ', ', '1.0', ']'] + + +Note that the JSON produced by this module is a subset of YAML, +so it may be used as a serializer for that as well. +""" +__version__ = '1.1' +__all__ = [ + 'dump', 'dumps', 'load', 'loads', + 'JSONDecoder', 'JSONEncoder', +] + +from decoder import JSONDecoder +from encoder import JSONEncoder + +def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, **kw): + """ + Serialize ``obj`` as a JSON formatted stream to ``fp`` (a + ``.write()``-supporting file-like object). + + If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp`` + may be ``unicode`` instances, subject to normal Python ``str`` to + ``unicode`` coercion rules. Unless ``fp.write()`` explicitly + understands ``unicode`` (as in ``codecs.getwriter()``) this is likely + to cause an error. + + If ``check_circular`` is ``False``, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) + in strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + if cls is None: + cls = JSONEncoder + iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, + **kw).iterencode(obj) + # could accelerate with writelines in some versions of Python, at + # a debuggability cost + for chunk in iterable: + fp.write(chunk) + +def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, **kw): + """ + Serialize ``obj`` to a JSON formatted ``str``. + + If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is ``False``, then the return value will be a + ``unicode`` instance subject to normal Python ``str`` to ``unicode`` + coercion rules instead of being escaped to an ASCII ``str``. + + If ``check_circular`` is ``False``, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in + strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + if cls is None: + cls = JSONEncoder + return cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, **kw).encode(obj) + +def load(fp, encoding=None, cls=None, **kw): + """ + Deserialize ``fp`` (a ``.read()``-supporting file-like object containing + a JSON document) to a Python object. + + If the contents of ``fp`` is encoded with an ASCII based encoding other + than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must + be specified. Encodings that are not ASCII based (such as UCS-2) are + not allowed, and should be wrapped with + ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` + object and passed to ``loads()`` + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + """ + if cls is None: + cls = JSONDecoder + return cls(encoding=encoding, **kw).decode(fp.read()) + +def loads(s, encoding=None, cls=None, **kw): + """ + Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON + document) to a Python object. + + If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding + other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name + must be specified. Encodings that are not ASCII based (such as UCS-2) + are not allowed and should be decoded to ``unicode`` first. + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + """ + if cls is None: + cls = JSONDecoder + return cls(encoding=encoding, **kw).decode(s) + +def read(s): + """ + json-py API compatibility hook. Use loads(s) instead. + """ + import warnings + warnings.warn("simple_json.loads(s) should be used instead of read(s)", + DeprecationWarning) + return loads(s) + +def write(obj): + """ + json-py API compatibility hook. Use dumps(s) instead. + """ + import warnings + warnings.warn("simple_json.dumps(s) should be used instead of write(s)", + DeprecationWarning) + return dumps(obj) + + diff --git a/simple_json/decoder.py b/simple_json/decoder.py new file mode 100644 index 0000000..ec52215 --- /dev/null +++ b/simple_json/decoder.py @@ -0,0 +1,258 @@ +""" +Implementation of JSONDecoder +""" +import re + +from simple_json.scanner import Scanner, pattern + +FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL + +def _floatconstants(): + import struct + import sys + _BYTES = '7FF80000000000007FF0000000000000'.decode('hex') + if sys.byteorder != 'big': + _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1] + nan, inf = struct.unpack('dd', _BYTES) + return nan, inf, -inf + +NaN, PosInf, NegInf = _floatconstants() + +def linecol(doc, pos): + lineno = doc.count('\n', 0, pos) + 1 + if lineno == 1: + colno = pos + else: + colno = pos - doc.rindex('\n', 0, pos) + return lineno, colno + +def errmsg(msg, doc, pos, end=None): + lineno, colno = linecol(doc, pos) + if end is None: + return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos) + endlineno, endcolno = linecol(doc, end) + return '%s: line %d column %d - line %d column %d (char %d - %d)' % ( + msg, lineno, colno, endlineno, endcolno, pos, end) + +def JSONInfinity(match, context): + return PosInf, None +pattern('Infinity')(JSONInfinity) + +def JSONNegInfinity(match, context): + return NegInf, None +pattern('-Infinity')(JSONNegInfinity) + +def JSONNaN(match, context): + return NaN, None +pattern('NaN')(JSONNaN) + +def JSONTrue(match, context): + return True, None +pattern('true')(JSONTrue) + +def JSONFalse(match, context): + return False, None +pattern('false')(JSONFalse) + +def JSONNull(match, context): + return None, None +pattern('null')(JSONNull) + +def JSONNumber(match, context): + match = JSONNumber.regex.match(match.string, *match.span()) + integer, frac, exp = match.groups() + if frac or exp: + res = float(integer + (frac or '') + (exp or '')) + else: + res = int(integer) + return res, None +pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber) + +STRINGCHUNK = re.compile(r'("|\\|[^"\\]+)', FLAGS) +STRINGBACKSLASH = re.compile(r'([\\/bfnrt"]|u[A-Fa-f0-9]{4})', FLAGS) +BACKSLASH = { + '"': u'"', '\\': u'\\', '/': u'/', + 'b': u'\b', 'f': u'\f', 'n': u'\n', 'r': u'\r', 't': u'\t', +} + +DEFAULT_ENCODING = "utf-8" + +def scanstring(s, end, encoding=None): + if encoding is None: + encoding = DEFAULT_ENCODING + chunks = [] + while 1: + chunk = STRINGCHUNK.match(s, end) + end = chunk.end() + m = chunk.group(1) + if m == '"': + break + if m == '\\': + chunk = STRINGBACKSLASH.match(s, end) + if chunk is None: + raise ValueError(errmsg("Invalid \\escape", s, end)) + end = chunk.end() + esc = chunk.group(1) + try: + m = BACKSLASH[esc] + except KeyError: + m = unichr(int(esc[1:], 16)) + if not isinstance(m, unicode): + m = unicode(m, encoding) + chunks.append(m) + return u''.join(chunks), end + +def JSONString(match, context): + encoding = getattr(context, 'encoding', None) + return scanstring(match.string, match.end(), encoding) +pattern(r'"')(JSONString) + +WHITESPACE = re.compile(r'\s+', FLAGS) + +def skipwhitespace(s, end): + m = WHITESPACE.match(s, end) + if m is not None: + return m.end() + return end + +def JSONObject(match, context): + pairs = {} + s = match.string + end = skipwhitespace(s, match.end()) + nextchar = s[end:end + 1] + # trivial empty object + if nextchar == '}': + return pairs, end + 1 + if nextchar != '"': + raise ValueError(errmsg("Expecting property name", s, end)) + end += 1 + encoding = getattr(context, 'encoding', None) + while True: + key, end = scanstring(s, end, encoding) + end = skipwhitespace(s, end) + if s[end:end + 1] != ':': + raise ValueError(errmsg("Expecting : delimiter", s, end)) + end = skipwhitespace(s, end + 1) + try: + value, end = JSONScanner.iterscan(s, idx=end).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + pairs[key] = value + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar == '}': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end - 1)) + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar != '"': + raise ValueError(errmsg("Expecting property name", s, end - 1)) + return pairs, end +pattern(r'{')(JSONObject) + +def JSONArray(match, context): + values = [] + s = match.string + end = skipwhitespace(s, match.end()) + # look-ahead for trivial empty array + nextchar = s[end:end + 1] + if nextchar == ']': + return values, end + 1 + while True: + try: + value, end = JSONScanner.iterscan(s, idx=end).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + values.append(value) + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar == ']': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end)) + end = skipwhitespace(s, end) + return values, end +pattern(r'\[')(JSONArray) + +ANYTHING = [ + JSONTrue, + JSONFalse, + JSONNull, + JSONNaN, + JSONInfinity, + JSONNegInfinity, + JSONNumber, + JSONString, + JSONArray, + JSONObject, +] + +JSONScanner = Scanner(ANYTHING) + +class JSONDecoder(object): + """ + Simple JSON decoder + + Performs the following translations in decoding: + + +---------------+-------------------+ + | JSON | Python | + +===============+===================+ + | object | dict | + +---------------+-------------------+ + | array | list | + +---------------+-------------------+ + | string | unicode | + +---------------+-------------------+ + | number (int) | int, long | + +---------------+-------------------+ + | number (real) | float | + +---------------+-------------------+ + | true | True | + +---------------+-------------------+ + | false | False | + +---------------+-------------------+ + | null | None | + +---------------+-------------------+ + + It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as + their corresponding ``float`` values, which is outside the JSON spec. + """ + + scanner = Scanner(ANYTHING) + + def __init__(self, encoding=None): + self.encoding = encoding + + def raw_decode(self, s, **kw): + """ + Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning + with a JSON document) and return a 2-tuple of the Python + representation and the index in ``s`` where the document ended. + + This can be used to decode a JSON document from a string that may + have extraneous data at the end. + """ + kw.setdefault('context', self) + try: + obj, end = self.scanner.iterscan(s, **kw).next() + except StopIteration: + raise ValueError("No JSON object could be decoded") + return obj, end + + def decode(self, s): + """ + Return the Python representation of ``s`` (a ``str`` or ``unicode`` + instance containing a JSON document) + """ + obj, end = self.raw_decode(s, idx=skipwhitespace(s, 0)) + end = skipwhitespace(s, end) + if end != len(s): + raise ValueError(errmsg("Extra data", s, end, len(s))) + return obj + +__all__ = ['JSONDecoder'] diff --git a/simple_json/encoder.py b/simple_json/encoder.py new file mode 100644 index 0000000..71dfdb4 --- /dev/null +++ b/simple_json/encoder.py @@ -0,0 +1,275 @@ +""" +Implementation of JSONEncoder +""" +import re +import math + +# this should match any kind of infinity +INFCHARS = re.compile(r'[infINF]') +ESCAPE = re.compile(r'[\x00-\x19\\"\b\f\n\r\t]') +ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') +ESCAPE_DCT = { + '\\': '\\\\', + '"': '\\"', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', +} +for i in range(20): + ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) + +def floatstr(o, allow_nan=True): + s = str(o) + # If the first non-sign is a digit then it's not a special value + if (o < 0.0 and s[1].isdigit()) or s[0].isdigit(): + return s + elif not allow_nan: + raise ValueError("Out of range float values are not JSON compliant: %r" + % (o,)) + # These are the string representations on the platforms I've tried + if s == 'nan': + return 'NaN' + if s == 'inf': + return 'Infinity' + if s == '-inf': + return '-Infinity' + # NaN should either be inequal to itself, or equal to everything + if o != o or o == 0.0: + return 'NaN' + # Last ditch effort, assume inf + if o < 0: + return '-Infinity' + return 'Infinity' + +def encode_basestring(s): + """ + Return a JSON representation of a Python string + """ + def replace(match): + return ESCAPE_DCT[match.group(0)] + return '"' + ESCAPE.sub(replace, s) + '"' + +def encode_basestring_ascii(s): + def replace(match): + s = match.group(0) + try: + return ESCAPE_DCT[s] + except KeyError: + return '\\u%04x' % (ord(s),) + return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' + + +class JSONEncoder(object): + """ + Extensible JSON encoder for Python data structures. + + Supports the following objects and types by default: + + +-------------------+---------------+ + | Python | JSON | + +===================+===============+ + | dict | object | + +-------------------+---------------+ + | list, tuple | array | + +-------------------+---------------+ + | str, unicode | string | + +-------------------+---------------+ + | int, long, float | number | + +-------------------+---------------+ + | True | true | + +-------------------+---------------+ + | False | false | + +-------------------+---------------+ + | None | null | + +-------------------+---------------+ + + To extend this to recognize other objects, subclass and implement a + ``.default(o)`` method with another method that returns a serializable + object for ``o`` if possible, otherwise it should call the superclass + implementation (to raise ``TypeError``). + """ + def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True): + """ + Constructor for JSONEncoder, with sensible defaults. + + If skipkeys is False, then it is a TypeError to attempt + encoding of keys that are not str, int, long, float or None. If + skipkeys is True, such items are simply skipped. + + If ensure_ascii is True, the output is guaranteed to be str + objects with all incoming unicode characters escaped. If ensure_ascii + is false, the output will be unicode object. + + If check_circular is True, then lists, dicts, and custom encoded + objects will be checked for circular references during encoding to + prevent an infinite recursion (which would cause an OverflowError). + Otherwise, no such check takes place. + + If allow_nan is True, then NaN, Infinity, and -Infinity will be + encoded as such. This behavior is not JSON specification compliant, + but is consistent with most JavaScript based encoders and decoders. + Otherwise, it will be a ValueError to encode such floats. + """ + + self.skipkeys = skipkeys + self.ensure_ascii = ensure_ascii + self.check_circular = check_circular + self.allow_nan = allow_nan + + def _iterencode_list(self, lst, markers=None): + if not lst: + yield '[]' + return + if markers is not None: + markerid = id(lst) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = lst + yield '[' + first = True + for value in lst: + if first: + first = False + else: + yield ', ' + for chunk in self._iterencode(value, markers): + yield chunk + yield ']' + if markers is not None: + del markers[markerid] + + def _iterencode_dict(self, dct, markers=None): + if not dct: + yield '{}' + return + if markers is not None: + markerid = id(dct) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = dct + yield '{' + first = True + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + allow_nan = self.allow_nan + for key, value in dct.iteritems(): + if isinstance(key, basestring): + pass + # JavaScript is weakly typed for these, so it makes sense to + # also allow them. Many encoders seem to do something like this. + elif isinstance(key, float): + key = floatstr(key, allow_nan) + elif isinstance(key, (int, long)): + key = str(key) + elif key is True: + key = 'true' + elif key is False: + key = 'false' + elif key is None: + key = 'null' + elif self.skipkeys: + continue + else: + raise TypeError("key %r is not a string" % (key,)) + if first: + first = False + else: + yield ', ' + yield encoder(key) + yield ':' + for chunk in self._iterencode(value, markers): + yield chunk + yield '}' + if markers is not None: + del markers[markerid] + + def iterencode(self, o): + """ + Encode the given object and yield each string + representation as available. + + For example:: + + for chunk in JSONEncoder().iterencode(bigobject): + mysocket.write(chunk) + """ + if self.check_circular: + markers = {} + else: + markers = None + return self._iterencode(o, markers) + + def _iterencode(self, o, markers=None): + if isinstance(o, basestring): + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + yield encoder(o) + elif o is None: + yield 'null' + elif o is True: + yield 'true' + elif o is False: + yield 'false' + elif isinstance(o, (int, long)): + yield str(o) + elif isinstance(o, float): + yield floatstr(o, self.allow_nan) + elif isinstance(o, (list, tuple)): + for chunk in self._iterencode_list(o, markers): + yield chunk + elif isinstance(o, dict): + for chunk in self._iterencode_dict(o, markers): + yield chunk + else: + if markers is not None: + markerid = id(o) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = o + for chunk in self._iterencode_default(o, markers): + yield chunk + if markers is not None: + del markers[markerid] + + def _iterencode_default(self, o, markers=None): + newobj = self.default(o) + return self._iterencode(newobj, markers) + + def default(self, o): + """ + Implement this method in a subclass such that it returns + a serializable object for ``o``, or calls the base implementation + (to raise a ``TypeError``). + + For example, to support arbitrary iterators, you could + implement default like this:: + + def default(self, o): + try: + iterable = iter(o) + except TypeError: + pass + else: + return list(iterable) + return JSONEncoder.default(self, o) + """ + raise TypeError("%r is not JSON serializable" % (o,)) + + def encode(self, o): + """ + Return a JSON string representation of a Python data structure. + """ + # This doesn't pass the iterator directly to ''.join() because it + # sucks at reporting exceptions. It's going to do this internally + # anyway because it uses PySequence_Fast or similar. + chunks = list(self.iterencode(o)) + return ''.join(chunks) + +__all__ = ['JSONEncoder'] diff --git a/simple_json/scanner.py b/simple_json/scanner.py new file mode 100644 index 0000000..95d3f2b --- /dev/null +++ b/simple_json/scanner.py @@ -0,0 +1,67 @@ +""" +Iterator based sre token scanner +""" +import sre_parse, sre_compile, sre_constants +from sre_constants import BRANCH, SUBPATTERN +from sre import VERBOSE, MULTILINE, DOTALL +import re + +__all__ = ['Scanner', 'pattern'] + +FLAGS = (VERBOSE | MULTILINE | DOTALL) +class Scanner(object): + def __init__(self, lexicon, flags=FLAGS): + self.actions = [None] + # combine phrases into a compound pattern + s = sre_parse.Pattern() + s.flags = flags + p = [] + for idx, token in enumerate(lexicon): + phrase = token.pattern + try: + subpattern = sre_parse.SubPattern(s, + [(SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))]) + except sre_constants.error: + raise + p.append(subpattern) + self.actions.append(token) + + p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) + self.scanner = sre_compile.compile(p) + + + def iterscan(self, string, idx=0, context=None): + """ + Yield match, end_idx for each match + """ + match = self.scanner.scanner(string, idx).match + actions = self.actions + lastend = idx + end = len(string) + while True: + m = match() + if m is None: + break + matchbegin, matchend = m.span() + if lastend == matchend: + break + action = actions[m.lastindex] + if action is not None: + rval, next_pos = action(m, context) + if next_pos is not None and next_pos != matchend: + # "fast forward" the scanner + matchend = next_pos + match = self.scanner.scanner(string, matchend).match + yield rval, matchend + lastend = matchend + +def pattern(pattern, flags=FLAGS): + def decorator(fn): + fn.pattern = pattern + fn.regex = re.compile(pattern, flags) + return fn + return decorator + +def InsignificantWhitespace(match, context): + return None, None +pattern(r'\s+')(InsignificantWhitespace) diff --git a/simple_json/tests/__init__.py b/simple_json/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/simple_json/tests/test_fail.py b/simple_json/tests/test_fail.py new file mode 100644 index 0000000..41d94b0 --- /dev/null +++ b/simple_json/tests/test_fail.py @@ -0,0 +1,70 @@ +# Fri Dec 30 18:57:26 2005 +JSONDOCS = [ + # http://json.org/JSON_checker/test/fail1.json + '"A JSON payload should be an object or array, not a string."', + # http://json.org/JSON_checker/test/fail2.json + '["Unclosed array"', + # http://json.org/JSON_checker/test/fail3.json + '{unquoted_key: "keys must be quoted}', + # http://json.org/JSON_checker/test/fail4.json + '["extra comma",]', + # http://json.org/JSON_checker/test/fail5.json + '["double extra comma",,]', + # http://json.org/JSON_checker/test/fail6.json + '[ , "<-- missing value"]', + # http://json.org/JSON_checker/test/fail7.json + '["Comma after the close"],', + # http://json.org/JSON_checker/test/fail8.json + '["Extra close"]]', + # http://json.org/JSON_checker/test/fail9.json + '{"Extra comma": true,}', + # http://json.org/JSON_checker/test/fail10.json + '{"Extra value after close": true} "misplaced quoted value"', + # http://json.org/JSON_checker/test/fail11.json + '{"Illegal expression": 1 + 2}', + # http://json.org/JSON_checker/test/fail12.json + '{"Illegal invocation": alert()}', + # http://json.org/JSON_checker/test/fail13.json + '{"Numbers cannot have leading zeroes": 013}', + # http://json.org/JSON_checker/test/fail14.json + '{"Numbers cannot be hex": 0x14}', + # http://json.org/JSON_checker/test/fail15.json + '["Illegal backslash escape: \\x15"]', + # http://json.org/JSON_checker/test/fail16.json + '["Illegal backslash escape: \\\'"]', + # http://json.org/JSON_checker/test/fail17.json + '["Illegal backslash escape: \\017"]', + # http://json.org/JSON_checker/test/fail18.json + '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', + # http://json.org/JSON_checker/test/fail19.json + '{"Missing colon" null}', + # http://json.org/JSON_checker/test/fail20.json + '{"Double colon":: null}', + # http://json.org/JSON_checker/test/fail21.json + '{"Comma instead of colon", null}', + # http://json.org/JSON_checker/test/fail22.json + '["Colon instead of comma": false]', + # http://json.org/JSON_checker/test/fail23.json + '["Bad value", truth]', + # http://json.org/JSON_checker/test/fail24.json + "['single quote']", +] + +SKIPS = { + 1: "why not have a string payload?", + 18: "spec doesn't specify any nesting limitations", +} + +def test_failures(): + import simple_json + for idx, doc in enumerate(JSONDOCS): + idx = idx + 1 + if idx in SKIPS: + simple_json.loads(doc) + continue + try: + simple_json.loads(doc) + except ValueError: + pass + else: + assert False, "Expected failure for fail%d.json: %r" % (idx, doc) diff --git a/simple_json/tests/test_pass1.py b/simple_json/tests/test_pass1.py new file mode 100644 index 0000000..d20ef11 --- /dev/null +++ b/simple_json/tests/test_pass1.py @@ -0,0 +1,72 @@ +# from http://json.org/JSON_checker/test/pass1.json +JSON = r''' +[ + "JSON Test Pattern pass1", + {"object with 1 member":["array with 1 element"]}, + {}, + [], + -42, + true, + false, + null, + { + "integer": 1234567890, + "real": -9876.543210, + "e": 0.123456789e-12, + "E": 1.234567890E+34, + "": 23456789012E666, + "zero": 0, + "one": 1, + "space": " ", + "quote": "\"", + "backslash": "\\", + "controls": "\b\f\n\r\t", + "slash": "/ & \/", + "alpha": "abcdefghijklmnopqrstuvwyz", + "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", + "digit": "0123456789", + "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", + "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", + "true": true, + "false": false, + "null": null, + "array":[ ], + "object":{ }, + "address": "50 St. James Street", + "url": "http://www.JSON.org/", + "comment": "// /* */": " ", + " s p a c e d " :[1,2 , 3 + +, + +4 , 5 , 6 ,7 ], + "compact": [1,2,3,4,5,6,7], + "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", + "quotes": "" \u0022 %22 0x22 034 "", + "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" +: "A key can be any string" + }, + 0.5 ,98.6 +, +99.44 +, + +1066 + + +,"rosebud"] +''' + +def test_parse(): + # test in/out equivalence and parsing + import simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) + try: + simple_json.dumps(res, allow_nan=False) + except ValueError: + pass + else: + assert False, "23456789012E666 should be out of range" diff --git a/simple_json/tests/test_pass2.py b/simple_json/tests/test_pass2.py new file mode 100644 index 0000000..6ee1787 --- /dev/null +++ b/simple_json/tests/test_pass2.py @@ -0,0 +1,11 @@ +# from http://json.org/JSON_checker/test/pass2.json +JSON = r''' +[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] +''' + +def test_parse(): + # test in/out equivalence and parsing + import simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) diff --git a/simple_json/tests/test_pass3.py b/simple_json/tests/test_pass3.py new file mode 100644 index 0000000..53d7dfa --- /dev/null +++ b/simple_json/tests/test_pass3.py @@ -0,0 +1,16 @@ +# from http://json.org/JSON_checker/test/pass3.json +JSON = r''' +{ + "JSON Test Pattern pass3": { + "The outermost value": "must be an object or array.", + "In this test": "It is an object." + } +} +''' + +def test_parse(): + # test in/out equivalence and parsing + import simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) diff --git a/simple_json/tests/test_recursion.py b/simple_json/tests/test_recursion.py new file mode 100644 index 0000000..360db2b --- /dev/null +++ b/simple_json/tests/test_recursion.py @@ -0,0 +1,62 @@ +import simple_json + +def test_listrecursion(): + x = [] + x.append(x) + try: + simple_json.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on list recursion" + x = [] + y = [x] + x.append(y) + try: + simple_json.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on alternating list recursion" + y = [] + x = [y, y] + # ensure that the marker is cleared + simple_json.dumps(x) + +def test_dictrecursion(): + x = {} + x["test"] = x + try: + simple_json.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on dict recursion" + x = {} + y = {"a": x, "b": x} + # ensure that the marker is cleared + simple_json.dumps(x) + +class TestObject: + pass + +class RecursiveJSONEncoder(simple_json.JSONEncoder): + recurse = False + def default(self, o): + if o is TestObject: + if self.recurse: + return [TestObject] + else: + return 'TestObject' + simple_json.JSONEncoder.default(o) + +def test_defaultrecursion(): + enc = RecursiveJSONEncoder() + assert enc.encode(TestObject) == '"TestObject"' + enc.recurse = True + try: + enc.encode(TestObject) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on default recursion" -- cgit v1.2.1