Left base folder: C:\temp\python-svn Right base folder: C:\temp\xxx --- doc\json.rst 2009-01-04 18:20:49.000000000 -0200 +++ doc\json.rst 2009-01-14 02:47:34.000000000 -0200 @@ -93,9 +93,9 @@ .. highlight:: none -Using json.tool from the shell to validate and pretty-print:: +Using :mod:`json.tool` from the shell to validate and pretty-print:: - $ echo '{"json":"obj"}' | python -mjson.tool + $ echo '{"json":"obj"}' | python -m json.tool { "json": "obj" } @@ -118,39 +118,42 @@ Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting file-like object). - If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not + If *skipkeys* is true (default: ``False``), then dict keys that are not of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`, :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a :exc:`TypeError`. - If *ensure_ascii* is ``False`` (default: ``True``), then some chunks written + If *ensure_ascii* is false (default: ``True``), then some chunks written to *fp* may be :class:`unicode` instances, subject to normal Python :class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()`` explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this is likely to cause an error. - If *check_circular* is ``False`` (default: ``True``), then the circular + If *check_circular* is false (default: ``True``), then the circular reference check for container types will be skipped and a circular reference will result in an :exc:`OverflowError` (or worse). - If *allow_nan* is ``False`` (default: ``True``), then it will be a + If *allow_nan* is false (default: ``True``), then it will be a :exc:`ValueError` to serialize out of range :class:`float` values (``nan``, - ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of - using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + ``inf``, ``-inf``) in strict compliance of the JSON specification. + If *allow_nan* is true, their JavaScript equivalents will be used + (``NaN``, ``Infinity``, ``-Infinity``). If *indent* is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. ``None`` (the default) selects the most compact representation. - If *separators* is an ``(item_separator, dict_separator)`` tuple, then it - will be used instead of the default ``(', ', ': ')`` separators. ``(',', - ':')`` is the most compact JSON representation. + If specified, *separators* should be an ``(item_separator, key_separator)`` + tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON + representation, you should specify ``(',', ':')`` to eliminate whitespace. *encoding* is the character encoding for str instances, default is UTF-8. - *default(obj)* is a function that should return a serializable version of - *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`. + If specified, *default* should be a function that gets called for objects that + can't otherwise be serialized. It should return a JSON encodable version of + the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` + is always raised in those cases. To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the :meth:`default` method to serialize additional types), specify it with the @@ -161,7 +164,7 @@ Serialize *obj* to a JSON formatted :class:`str`. - If *ensure_ascii* is ``False``, then the return value will be a + If *ensure_ascii* is false, then the return value will be a :class:`unicode` instance. The other arguments have the same meaning as in :func:`dump`. @@ -212,7 +215,7 @@ specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to :class:`unicode` first. - The other arguments have the same meaning as in :func:`dump`. + The other arguments have the same meaning as in :func:`load`. Encoders and decoders @@ -319,26 +322,26 @@ for ``o`` if possible, otherwise it should call the superclass implementation (to raise :exc:`TypeError`). - If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to + If *skipkeys* is false (the default), then it is a :exc:`TypeError` to attempt encoding of keys that are not str, int, long, float or None. If - *skipkeys* is ``True``, such items are simply skipped. + *skipkeys* is true, such items are simply skipped. - If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be + If *ensure_ascii* is true (the default), the output is guaranteed to be :class:`str` objects with all incoming unicode characters escaped. If - *ensure_ascii* is ``False``, the output will be a unicode object. + *ensure_ascii* is false, the output will be a unicode object. - If *check_circular* is ``True`` (the default), then lists, dicts, and custom + If *check_circular* is true (the default), then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an :exc:`OverflowError`). Otherwise, no such check takes place. - If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and + If *allow_nan* is true (the default), 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 :exc:`ValueError` to encode such floats. - If *sort_keys* is ``True`` (the default), then the output of dictionaries + If *sort_keys* is true (the default), then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis. @@ -348,12 +351,13 @@ compact representation. If specified, *separators* should be an ``(item_separator, key_separator)`` - tuple. The default is ``(', ', ': ')``. To get the most compact JSON + tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON representation, you should specify ``(',', ':')`` to eliminate whitespace. - If specified, *default* is a function that gets called for objects that can't - otherwise be serialized. It should return a JSON encodable version of the - object or raise a :exc:`TypeError`. + If specified, *default* should be a function that gets called for objects that + can't otherwise be serialized. It should return a JSON encodable version of + the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` + is always raised in those cases. If *encoding* is not ``None``, then all input strings will be transformed into unicode using that encoding prior to JSON-encoding. The default is --- json\tests\test_dump.py 2008-07-30 15:26:06.000000000 -0200 +++ json\tests\test_dump.py 2009-01-14 03:24:43.000000000 -0200 @@ -11,3 +11,11 @@ def test_dumps(self): self.assertEquals(json.dumps({}), '{}') + + def test_encode_truefalse(self): + self.assertEquals(json.dumps( + {True: False, False: True}, sort_keys=True), + '{"false": true, "true": false}') + self.assertEquals(json.dumps( + {2: 3.0, 4.0: 5L, False: 1, 6L: True, "7": 0}, sort_keys=True), + '{"false": 1, "2": 3.0, "4.0": 5, "6": true, "7": 0}') --- json\__init__.py 2008-07-30 15:26:11.000000000 -0200 +++ json\__init__.py 2009-01-14 02:22:43.000000000 -0200 @@ -125,21 +125,21 @@ """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 + 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`` + 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 + 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 + 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``). @@ -163,8 +163,8 @@ """ # cached encoder - if (skipkeys is False and ensure_ascii is True and - check_circular is True and allow_nan is True and + if (not skipkeys and ensure_ascii and + check_circular and allow_nan and cls is None and indent is None and separators is None and encoding == 'utf-8' and default is None and not kw): iterable = _default_encoder.iterencode(obj) @@ -186,19 +186,19 @@ encoding='utf-8', default=None, **kw): """Serialize ``obj`` to a JSON formatted ``str``. - If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types + 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 + 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 + 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 + 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``). @@ -223,8 +223,8 @@ """ # cached encoder - if (skipkeys is False and ensure_ascii is True and - check_circular is True and allow_nan is True and + if (not skipkeys and ensure_ascii and + check_circular and allow_nan and cls is None and indent is None and separators is None and encoding == 'utf-8' and default is None and not kw): return _default_encoder.encode(obj) --- json\encoder.py 2008-07-30 15:26:11.000000000 -0200 +++ json\encoder.py 2009-01-14 03:16:58.000000000 -0200 @@ -121,25 +121,25 @@ indent=None, separators=None, encoding='utf-8', default=None): """Constructor for JSONEncoder, with sensible defaults. - If skipkeys is False, then it is a TypeError to attempt + 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. + skipkeys is true, such items are simply skipped. - If ensure_ascii is True, the output is guaranteed to be str + 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 + 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 + 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. - If sort_keys is True, then the output of dictionaries will be + If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis. @@ -254,12 +254,12 @@ # 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 isinstance(key, (int, long)): + key = str(key) elif key is None: key = 'null' elif self.skipkeys: