Wrote the following two functions for storing and retrieving any Python (built-in or user-defined) object with a combination of json and jsonpickle (in 2.7)
def save(kind, obj):
pickled = jsonpickle.encode(obj)
filename = DATA_DESTINATION[kind] \\returns file destination to store json
if os.path.isfile(filename):
open(filename, 'w').close()
with open(filename, 'w') as f:
json.dump(pickled, f)
def retrieve(kind):
filename = DATA_DESTINATION[kind] \\returns file destination to store json
if os.path.isfile(filename):
with open(filename, 'r') as f:
pickled = json.load(f)
unpickled = jsonpickle.decode(pickled)
print unpickled
I haven't tested these two functions with user-defined objects, but when i attempt to save() a built-in dictionary of strings, (ie. {'Adam': 'Age 19', 'Bill', 'Age 32'}), and i retrieve the same file, i get the same dictionary back in unicode, {u'Adam': u'Age 19', u'Bill', u'Age 32'}. I thought json/jsonpickle encoded by default to utf-8, what's the deal here?
[UPDATE]: Removing all jsonpickle encoding/decoding does not effect output, still in unicode, seems like an issue with json? Perhaps I'm doing something wrong.