Jsonpickle not working on PyCharm only

This same code will produce two completely different outputs on regular Python and IDLE compared to PyCharm.

Code:

import jsonpickle
class TestClass(object):
    def __init__(self, name):
        self.name = name
pickled = jsonpickle.encode(TestClass("Testing"))
pickled
unpickled = jsonpickle.decode(pickled)
unpickled

Output in Python console:

>>> import jsonpickle
>>> class TestClass(object):
...     def __init__(self, name):
...         self.name = name
>>> pickled = jsonpickle.encode(TestClass("Testing"))
>>> pickled
'{"py/object": "__main__.TestClass", "name": "Testing"}'
>>> unpickled = jsonpickle.decode(pickled)
>>> unpickled
<__main__.TestClass object at 0x03AAD130>

Output in IDLE console (almost identical):

>>> import jsonpickle
>>> class TestClass(object):
        def __init__(self, name):
            self.name = name
>>> pickled = jsonpickle.encode(TestClass("Testing"))
>>> pickled
'{"py/object": "__main__.TestClass", "name": "Testing"}'
>>> unpickled = jsonpickle.decode(pickled)
>>> unpickled
<__main__.TestClass object at 0x039C94B0>

Output in PyCharm console:

>>> import jsonpickle
>>> class TestClass(object):
        def __init__(self, name):
            self.name = name
>>> pickled = jsonpickle.encode(TestClass("Testing"))
>>> pickled
'{"py/object": "__builtin__.TestClass", "name": "Testing"}'
>>> unpickled = jsonpickle.decode(pickled)
>>> unpickled
{'py/object': '__builtin__.TestClass', 'name': 'Testing'}

My main problem is that as you may see, the string does not get properly "unpickled" on PyCharm. It goes from an object to a string when encoded (which is correct), but does not go back from string to object when decoded. It merely becomes a dictionary which makes unpickling completely useless.

Another difference which might be of interest is that in both regular Python and IDLE the class is named as __main__.TestClass whereas in PyCharm it's __builtin__.TestClass, which is somewhat weird considering it's most definitely not a built-in class. Also, I've found that the proper built-in classes are inside __builtins__ (with an "s") and not __builtin__.

Please sign in to leave a comment.