Created
May 22, 2019 06:30
-
-
Save gustavorv86/6b5c962184ad2181aa7f4c5fcf39f3c2 to your computer and use it in GitHub Desktop.
Serialize and deserialize python dictionaries to JSON format.
This file contains hidden or 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 json | |
import sys | |
def serialize(json_data, json_filename): | |
try: | |
json_string = json.dumps(json_data, sort_keys=True, indent=4) | |
fd = open(json_filename, "w") | |
fd.write(json_string) | |
fd.close() | |
return True | |
except Exception as ex: | |
print("ERROR {}: {}".format(ex.__class__.__name__, ex), file=sys.stderr) | |
return False | |
def deserialize(json_filename): | |
try: | |
fd = open(json_filename, "r") | |
json_string = fd.read() | |
fd.close() | |
return json.loads(json_string) | |
except Exception as ex: | |
print("ERROR {}: {}".format(ex.__class__.__name__, ex), file=sys.stderr) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment