Last active
May 3, 2020 06:22
-
-
Save jiteshk23/c9fca9a71feabab4042e688a108826a1 to your computer and use it in GitHub Desktop.
Compress json with lots of small strings
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
def compress_json(obj): | |
''' | |
Performs string interning for non-compile time small strings. | |
''' | |
if isinstance(obj, (str, unicode)): | |
return intern(str(obj)) | |
elif isinstance(obj, dict): | |
return { intern(str(k)) : compress_json(v) for k, v in obj.iteritems() } | |
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): | |
return [ compress_json(e) for e in obj ] | |
else: | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment