Message11570

Author zyasoft
Recipients birkoff, zyasoft
Date 2017-09-05.18:49:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1504637352.53.0.414228385276.issue2622@psf.upfronthosting.co.za>
In-reply-to
Content
So this can be readily reproduced:

$ jython27
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_144
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> class X(object): pass
...
>>> x = X()
>>> x.foo = 42
>>> x.baz = "abc"
>>> x.__dict__
{'baz': 'abc', 'foo': 42}
>>> json.dumps(x.__dict__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jbaker/jython2.7.1/Lib/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/Users/jbaker/jython2.7.1/Lib/json/encoder.py", line 206, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/jbaker/jython2.7.1/Lib/json/encoder.py", line 269, in iterencode
    return _iterencode(o, 0)
  File "/Users/jbaker/jython2.7.1/Lib/json/encoder.py", line 183, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: {'baz': 'abc', 'foo': 42} is not JSON serializable

The workaround is also trivial, but obivously doesn't work well in existing code like Zabbix:

>>> json.dumps(dict(x.__dict__))
'{"foo": 42, "baz": "abc"}'

So we should fix this bug in the json module. Unfortunately, for a variety of historical reasons, the underlying object in Jython for __dict__ is actually something called PyStringMap, which actually isn't so restricted as its name implies. Maybe we can fix this in Jython 3; previous attempts at this refactoring didn't work however. For now, we have to do this workaround for code that directly sees the underlying Java representation, as is the case for the json module implementation.
History
Date User Action Args
2017-09-05 18:49:12zyasoftsetmessageid: <1504637352.53.0.414228385276.issue2622@psf.upfronthosting.co.za>
2017-09-05 18:49:12zyasoftsetrecipients: + zyasoft, birkoff
2017-09-05 18:49:12zyasoftlinkissue2622 messages
2017-09-05 18:49:11zyasoftcreate