Skip to content
This repository

avoid jsonlib returning Decimal #1364

Merged
merged 1 commit into from over 2 years ago

2 participants

Min RK Brian E. Granger
Min RK
Owner

The only functional change is that Decimal objects will no longer come out of jsonlib, but the code that handles jsonlib's two minor differences wrt other json libraries is now a bit clearer and better documented.

Relevant test is added.

Min RK minrk avoid jsonlib returning Decimal
The only functional change is that Decimal objects will no longer come out of jsonlib, but the code that handles jsonlib's two minor differences wrt other json libraries is now a bit clearer and better documented.

Relevant test is added.
4506273
Brian E. Granger
Owner

This looks straitforward and clean. I would say it is merge ready.

Min RK minrk merged commit cf6b427 into from
Min RK minrk closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 01, 2012
Min RK minrk avoid jsonlib returning Decimal
The only functional change is that Decimal objects will no longer come out of jsonlib, but the code that handles jsonlib's two minor differences wrt other json libraries is now a bit clearer and better documented.

Relevant test is added.
4506273
This page is out of date. Refresh to see the latest.
19 IPython/zmq/session.py
@@ -73,9 +73,22 @@ def squash_unicode(obj):
73 73 # globals and defaults
74 74 #-----------------------------------------------------------------------------
75 75
76   -key = 'on_unknown' if jsonapi.jsonmod.__name__ == 'jsonlib' else 'default'
77   -json_packer = lambda obj: jsonapi.dumps(obj, **{key:date_default})
78   -json_unpacker = lambda s: extract_dates(jsonapi.loads(s))
  76 +
  77 +# jsonlib behaves a bit differently, so handle that where it affects us
  78 +if jsonapi.jsonmod.__name__ == 'jsonlib':
  79 + # kwarg for serializing unknown types (datetime) is different
  80 + dumps_kwargs = dict(on_unknown=date_default)
  81 + # By default, jsonlib unpacks floats as Decimal instead of float,
  82 + # which can foul things up
  83 + loads_kwargs = dict(use_float=True)
  84 +else:
  85 + # ISO8601-ify datetime objects
  86 + dumps_kwargs = dict(default=date_default)
  87 + # nothing to specify for loads
  88 + loads_kwargs = dict()
  89 +
  90 +json_packer = lambda obj: jsonapi.dumps(obj, **dumps_kwargs)
  91 +json_unpacker = lambda s: extract_dates(jsonapi.loads(s, **loads_kwargs))
79 92
80 93 pickle_packer = lambda o: pickle.dumps(o,-1)
81 94 pickle_unpacker = pickle.loads
4 IPython/zmq/tests/test_session.py
@@ -59,7 +59,7 @@ def test_msg(self):
59 59 self.assertEquals(msg['msg_type'], 'execute')
60 60
61 61 def test_serialize(self):
62   - msg = self.session.msg('execute',content=dict(a=10))
  62 + msg = self.session.msg('execute', content=dict(a=10, b=1.1))
63 63 msg_list = self.session.serialize(msg, ident=b'foo')
64 64 ident, msg_list = self.session.feed_identities(msg_list)
65 65 new_msg = self.session.unserialize(msg_list)
@@ -69,6 +69,8 @@ def test_serialize(self):
69 69 self.assertEquals(new_msg['header'],msg['header'])
70 70 self.assertEquals(new_msg['content'],msg['content'])
71 71 self.assertEquals(new_msg['parent_header'],msg['parent_header'])
  72 + # ensure floats don't come out as Decimal:
  73 + self.assertEquals(type(new_msg['content']['b']),type(new_msg['content']['b']))
72 74
73 75 def test_send(self):
74 76 socket = MockSocket(zmq.Context.instance(),zmq.PAIR)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.