Last active
March 22, 2016 21:50
-
-
Save deeplook/59b018f2d0e15793533c to your computer and use it in GitHub Desktop.
Create a structured, but more compact JSON representation of a native data structure.
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 __future__ import print_function | |
""" | |
Create a structured, but more compact JSON representation of a native data structure. | |
This is inspired by the way native Python data structures are displayed in Jupyter, | |
e.g.: | |
In [1]: d = {"count": 7, "limit": 7, "results": [{"path": "wifi", "meaning": "rssi", "points": [{"timestamp": 1458211728242, "value": -67}, {"timestamp": 1458211736170, "value": -74}, {"timestamp": 1458211740399, "value": -62}, {"timestamp": 1458211746265, "value": -64}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}, {"path": "/", "meaning": "batteryLevel", "points": [{"timestamp": 1458211728413, "value": 90}, {"timestamp": 1458211736216, "value": 90}, {"timestamp": 1458211740443, "value": 90}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}], "offset": 0} | |
In [2]: d | |
Out[2]: | |
{'count': 7, | |
'limit': 7, | |
'offset': 0, | |
'results': [{'deviceId': '1e31f865-93b9-482a-a7be-858b56c396ae', | |
'meaning': 'rssi', | |
'path': 'wifi', | |
'points': [{'timestamp': 1458211728242, 'value': -67}, | |
{'timestamp': 1458211736170, 'value': -74}, | |
{'timestamp': 1458211740399, 'value': -62}, | |
{'timestamp': 1458211746265, 'value': -64}]}, | |
{'deviceId': '1e31f865-93b9-482a-a7be-858b56c396ae', | |
'meaning': 'batteryLevel', | |
'path': '/', | |
'points': [{'timestamp': 1458211728413, 'value': 90}, | |
{'timestamp': 1458211736216, 'value': 90}, | |
{'timestamp': 1458211740443, 'value': 90}]}]} | |
This code is based on using pretty-printing and minimizing indentation | |
(and is not yet finished...). | |
To-do: | |
- treat/test Booleans | |
- test single and double quotes | |
""" | |
import re | |
import sys | |
import pprint | |
import collections | |
if sys.version_info.major < 3: | |
from cStringIO import StringIO | |
else: | |
from io import StringIO | |
def convert_keys(data): | |
"Convert Unicode strings into normal string ones." | |
if sys.version_info.major >= 3: | |
return data | |
else: | |
# http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str | |
if isinstance(data, basestring): | |
return str(data) | |
elif isinstance(data, collections.Mapping): | |
return dict(map(convert_keys, data.iteritems())) | |
elif isinstance(data, collections.Iterable): | |
return type(data)(map(convert_keys, data)) | |
else: | |
return data | |
def pretty_struc(obj): | |
"Return a pretty-printed structured string of a native data structure." | |
obj1 = convert_keys(obj) | |
out = StringIO() | |
pp = pprint.PrettyPrinter(indent=1, stream=out) | |
pp.pprint(obj1) | |
out.seek(0) | |
pretty = out.read() | |
out.close() | |
return pretty | |
def compact_json(obj): | |
"Return a more compact, but still structured valid JSON representation of 'obj'." | |
pp = pretty_struc(obj) | |
# detect all indentation levels | |
indents = list(set(re.findall('\n\s+[^\s]', pp))) | |
counts = sorted([i.count(' ') for i in indents]) | |
if counts: | |
counts2 = [counts[0]] + list(range(counts[0]+1, len(counts)+1)) | |
def repl(m): | |
"Minimize indentation." | |
l = m.end() - m.start() - 2 | |
c = counts2[counts.index(l)] | |
return '\n' + ' ' * c + m.string[m.end()-1] | |
# minimize indentation | |
if indents: | |
pp_min = re.sub('\n\s+[^\s]', repl, pp) | |
else: | |
pp_min = pp | |
# convert single quotes into double quotes if not preceeded by backslash | |
res = re.sub(r"(?!\\)'", '"', pp_min) | |
# handle the rest, mainly Booleans | |
# ... | |
obj_json = res | |
return obj_json | |
if __name__ == '__main__': | |
d = {u"count": 7, "limit": 7, "results": [{"path": "wifi", "meaning": "rssi", "points": [{"timestamp": 1458211728242, "value": -67}, {"timestamp": 1458211736170, "value": -74}, {"timestamp": 1458211740399, "value": -62}, {"timestamp": 1458211746265, "value": -64}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}, {"path": "/", "meaning": "batteryLevel", "points": [{"timestamp": 1458211728413, "value": 90}, {"timestamp": 1458211736216, "value": 90}, {"timestamp": 1458211740443, "value": 90}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}], "offset": 0} | |
print(d) | |
print(convert_keys(d)) | |
print(pretty_struc(d)) | |
print(compact_json(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment