Last active
July 7, 2022 20:45
-
-
Save nickolaskraus-wf/09c2fcd2060d1cb158b0b4ae82a2ebf4 to your computer and use it in GitHub Desktop.
Given a list of arguments, return their JSON serializable equivalents
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
from typing import Any, Union | |
import json | |
def make_serializable_helper(x: Union[dict, list]) -> Union[dict, list]: | |
""" | |
Given a list or dict, return its JSON serializable equivalent. | |
""" | |
if len(x) == 0: | |
return x | |
if isinstance(x, list): | |
l = [] | |
for i in x: | |
if isinstance(i, bytes): | |
l.append(i.decode()) | |
else: | |
l.append(make_serializable_helper(i)) | |
return l | |
for k, v in x.items(): | |
if isinstance(v, (list, dict)): | |
x[k] = make_serializable_helper(v) | |
elif isinstance(v, bytes): | |
x[k] = v.decode() | |
return x | |
def make_serializable(arg: Any) -> Any: | |
""" | |
Given an argument, return its JSON serializable equivalent. | |
""" | |
if isinstance(arg, str): | |
return arg | |
elif isinstance(arg, bytes): | |
return arg.decode() | |
elif isinstance(arg, dict): | |
return make_serializable_helper(arg) | |
else: | |
return arg | |
def serialize(*args: Any) -> tuple: | |
""" | |
Given a list of arguments, return their JSON serializable equivalents. | |
""" | |
ret = [] | |
for arg in args: | |
ret.append(make_serializable(arg)) | |
return tuple(ret) | |
if __name__ == "__main__": | |
d0 = {"a": "string"} | |
d1 = {"a": b"byte"} | |
d2 = {"a": {"a": b"byte"}} | |
d3 = {"a": [{"a": b"byte"}]} | |
d4 = { | |
'name': | |
'Ticket: TEST-123', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBlRpY2tldBgNDA', | |
'children_keys': [b'agx0ZXN0YmVkLXRlc3RyCgsSBFB1bGwYDgw'], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [{ | |
'name': | |
'Pull: Workiva/test/#123', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyCgsSBFB1bGwYDgw', | |
'children_keys': [b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBQw'], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': True, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [{ | |
'name': | |
'Release: release 1.0.0', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBQw', | |
'children_keys': [ | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBww', | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgBDA', | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBgw' | |
], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': True, | |
'qa_review': None | |
}, | |
'children': [{ | |
'name': | |
'Release: dep-release 2.0.0', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBww', | |
'children_keys': [ | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBQw', | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgCDA', | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgDDA' | |
], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': False, | |
'qa_review': None | |
}, | |
'children': [{ | |
'name': 'Deploy to location-blah-2', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgCDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}, { | |
'name': 'Deploy to location-blah-3', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgDDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}] | |
}, { | |
'name': 'Deploy to location-blah', | |
'urlsafe_key': b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgBDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}, { | |
'name': | |
'Release: release 1.0.1', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDQsSB1JlbGVhc2UYBgw', | |
'children_keys': [ | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgIDA', | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgJDA', | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgLDA' | |
], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': False, | |
'qa_review': None | |
}, | |
'children': [{ | |
'name': 'Deploy to location-taco', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgIDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}, { | |
'name': 'Deploy to location-blah', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgJDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}, { | |
'name': 'Deploy to location-sauce', | |
'urlsafe_key': | |
b'agx0ZXN0YmVkLXRlc3RyDAsSBkRlcGxveRgLDA', | |
'children_keys': [], | |
'node_reviews': { | |
'code_review': None, | |
'security_review': None, | |
'rm_approval': None, | |
'qa_review': None | |
}, | |
'children': [] | |
}] | |
}] | |
}] | |
}] | |
} | |
d0, d1, d2, d3, d4 = serialize(d0, d1, d2, d3, d4) | |
print(d0, d1, d2, d3, d4) | |
json.dumps(d4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment