summaryrefslogtreecommitdiff
path: root/simplejson/encoder.py
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2010-03-08 04:10:07 +0000
committerBob Ippolito <bob@redivi.com>2010-03-08 04:10:07 +0000
commit32b8a4d5cdb74466671df6006fa492004e6da408 (patch)
tree3b1b2e3130d0709895cd3f853fec103356d657d1 /simplejson/encoder.py
parent98f42b54dd17096215995f0848ab5c26c81c805d (diff)
downloadsimplejson-32b8a4d5cdb74466671df6006fa492004e6da408.tar.gz
http://code.google.com/p/simplejson/issues/detail?id=34
git-svn-id: http://simplejson.googlecode.com/svn/trunk@219 a4795897-2c25-0410-b006-0d3caba88fa1
Diffstat (limited to 'simplejson/encoder.py')
-rw-r--r--simplejson/encoder.py37
1 files changed, 25 insertions, 12 deletions
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index 9a6ffab..cab8456 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -1,16 +1,15 @@
"""Implementation of JSONEncoder
"""
import re
+from decimal import Decimal
-try:
- from simplejson._speedups import encode_basestring_ascii as \
- c_encode_basestring_ascii
-except ImportError:
- c_encode_basestring_ascii = None
-try:
- from simplejson._speedups import make_encoder as c_make_encoder
-except ImportError:
- c_make_encoder = None
+def _import_speedups():
+ try:
+ from simplejson import _speedups
+ return _speedups.encode_basestring_ascii, _speedups.make_encoder
+ except ImportError:
+ return None, None
+c_encode_basestring_ascii, c_make_encoder = _import_speedups()
from simplejson.decoder import PosInf
@@ -104,7 +103,8 @@ class JSONEncoder(object):
key_separator = ': '
def __init__(self, skipkeys=False, ensure_ascii=True,
check_circular=True, allow_nan=True, sort_keys=False,
- indent=None, separators=None, encoding='utf-8', default=None):
+ indent=None, separators=None, encoding='utf-8', default=None,
+ use_decimal=False):
"""Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt
@@ -147,6 +147,10 @@ class JSONEncoder(object):
If encoding is not None, then all input strings will be
transformed into unicode using that encoding prior to JSON-encoding.
The default is UTF-8.
+
+ If use_decimal is true (not the default), ``decimal.Decimal`` will
+ be supported directly by the encoder. For the inverse, decode JSON
+ with ``parse_float=decimal.Decimal``.
"""
@@ -155,6 +159,7 @@ class JSONEncoder(object):
self.check_circular = check_circular
self.allow_nan = allow_nan
self.sort_keys = sort_keys
+ self.use_decimal = use_decimal
if isinstance(indent, (int, long)):
indent = ' ' * indent
self.indent = indent
@@ -267,12 +272,12 @@ class JSONEncoder(object):
_iterencode = c_make_encoder(
markers, self.default, _encoder, self.indent,
self.key_separator, self.item_separator, self.sort_keys,
- self.skipkeys, self.allow_nan, key_memo)
+ self.skipkeys, self.allow_nan, key_memo, self.use_decimal)
else:
_iterencode = _make_iterencode(
markers, self.default, _encoder, self.indent, floatstr,
self.key_separator, self.item_separator, self.sort_keys,
- self.skipkeys, _one_shot)
+ self.skipkeys, _one_shot, self.use_decimal)
try:
return _iterencode(o, 0)
finally:
@@ -308,11 +313,13 @@ class JSONEncoderForHTML(JSONEncoder):
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
+ _use_decimal,
## HACK: hand-optimized bytecode; turn globals into locals
False=False,
True=True,
ValueError=ValueError,
basestring=basestring,
+ Decimal=Decimal,
dict=dict,
float=float,
id=id,
@@ -360,6 +367,8 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield buf + str(value)
elif isinstance(value, float):
yield buf + _floatstr(value)
+ elif _use_decimal and isinstance(value, Decimal):
+ yield buf + str(value)
else:
yield buf
if isinstance(value, (list, tuple)):
@@ -438,6 +447,8 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield str(value)
elif isinstance(value, float):
yield _floatstr(value)
+ elif _use_decimal and isinstance(value, Decimal):
+ yield str(value)
else:
if isinstance(value, (list, tuple)):
chunks = _iterencode_list(value, _current_indent_level)
@@ -473,6 +484,8 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
elif isinstance(o, dict):
for chunk in _iterencode_dict(o, _current_indent_level):
yield chunk
+ elif _use_decimal and isinstance(o, Decimal):
+ yield str(o)
else:
if markers is not None:
markerid = id(o)