From 346ca11a92ac5d4e564d2be5ddbc3be45aef17d2 Mon Sep 17 00:00:00 2001 From: Alinson S Xavier Date: Tue, 18 Aug 2020 10:50:40 -0500 Subject: [PATCH] Serialize instances as JSON --- src/python/miplearn/instance.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/python/miplearn/instance.py b/src/python/miplearn/instance.py index 7de5311..a5c348c 100644 --- a/src/python/miplearn/instance.py +++ b/src/python/miplearn/instance.py @@ -3,7 +3,7 @@ # Released under the modified BSD license. See COPYING.md for more details. from abc import ABC, abstractmethod -import pickle, gzip +import pickle, gzip, json class Instance(ABC): @@ -121,9 +121,11 @@ class Instance(ABC): pass def load(self, filename): - with gzip.open(filename, "rb") as f: - self.__dict__ = pickle.load(f) + with gzip.GzipFile(filename, 'r') as f: + data = json.loads(f.read().decode('utf-8')) + self.__dict__ = data def dump(self, filename): - with gzip.open(filename, "wb") as f: - pickle.dump(self.__dict__, f) \ No newline at end of file + data = json.dumps(self.__dict__, indent=2).encode('utf-8') + with gzip.GzipFile(filename, 'w') as f: + f.write(data) \ No newline at end of file