Skip to content

Instantly share code, notes, and snippets.

@jiteshk23
Last active May 3, 2020 06:22
Show Gist options
  • Save jiteshk23/c9fca9a71feabab4042e688a108826a1 to your computer and use it in GitHub Desktop.
Save jiteshk23/c9fca9a71feabab4042e688a108826a1 to your computer and use it in GitHub Desktop.
Compress json with lots of small strings
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