Last active
August 29, 2015 14:10
-
-
Save blink1073/f2de8ee1b7c4857b74aa to your computer and use it in GitHub Desktop.
Simple JSON protocol that falls back on pickle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import json | |
import sys | |
try: | |
import cPickle as pickle | |
except ImportError: | |
import pickle | |
if sys.version.startswith('3'): | |
_str_type = str | |
else: | |
_str_type = unicode | |
class _MyEncoder(json.JSONEncoder): | |
def default(self, obj): | |
return pickle.dumps(obj, protocol=-1).decode('latin-1') | |
class _MyDecoder(json.JSONDecoder): | |
def __init__(self): | |
json.JSONDecoder.__init__(self, object_hook=self.dict_to_obj) | |
def dict_to_obj(self, d): | |
return dict_to_obj(d) | |
def json_pickle_dumps(obj): | |
return _MyEncoder().encode(obj) | |
def json_pickle_loads(str_): | |
return _MyDecoder().decode(str_) | |
def dict_to_obj(d): | |
for (key, value) in d.items(): | |
if isinstance(value, dict): | |
d[key] = dict_to_obj(value) | |
continue | |
if isinstance(value, _str_type): | |
try: | |
d[key] = pickle.loads(value.encode('latin-1')) | |
except Exception: | |
pass | |
return d | |
if __name__ == '__main__': | |
import time | |
test = dict(a=np.ones((4, 4))) | |
t0 = time.time() | |
d = json_pickle_dumps(test) | |
out = json_pickle_loads(d) | |
print(time.time() - t0) | |
print(out) | |
t0 = time.time() | |
d2 = pickle.dumps(test, protocol=-1) | |
out2 = pickle.loads(d2) | |
print(time.time() - t0) | |
print(out2) | |
print(len(d), len(d2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment