Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'layer1': {
- 'layer2': {
- 'layer3_1': [ long_list_of_stuff ],
- 'layer3_2': 'string'
- }
- }
- json.dumps(data_structure, indent=2)
- {
- "layer1": {
- "layer2": {
- "layer3_1": [
- {
- "x": 1,
- "y": 7
- },
- {
- "x": 0,
- "y": 4
- },
- {
- "x": 5,
- "y": 3
- },
- {
- "x": 6,
- "y": 9
- }
- ],
- "layer3_2": "string"
- }
- }
- }
- {
- "layer1": {
- "layer2": {
- "layer3_1": [{"x":1,"y":7},{"x":0,"y":4},{"x":5,"y":3},{"x":6,"y":9}],
- "layer3_2": "string"
- }
- }
- }
- import json
- class NoIndent(object):
- def __init__(self, value):
- self.value = value
- def __repr__(self):
- if not isinstance(self.value, (list, tuple)):
- return repr(self.value)
- else: # assume it's a list or tuple of coordinates stored as dicts
- delimiters = '[]' if isinstance(self.value, list) else '()'
- pairs = ('{!r}:{}'.format(*component)
- for coordinate in self.value
- for component in sorted(coordinate.items()))
- pairs = ('{{{}, {}}}'.format(*pair)
- for pair in zip(*[iter(pairs)]*2))
- return delimiters[0] + ', '.join(pairs) + delimiters[1]
- class MyEncoder(json.JSONEncoder):
- def default(self, obj):
- return(repr(obj) if isinstance(obj, NoIndent) else
- json.JSONEncoder.default(self, obj))
- data_structure = {
- 'layer1': {
- 'layer2': {
- 'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4},
- {"x":5,"y":3}, {"x":6,"y":9}]),
- 'layer3_2': 'string'
- }
- }
- }
- print json.dumps(data_structure, cls=MyEncoder, indent=2)
- s = json.dumps(data_structure, indent=2)
- s = re.sub('s*{s*"(.)": (d+),s*"(.)": (d+)s*}(,?)s*', r'{"1":2,"3":4}5', s)
- class NoIndentList(list):
- pass
- import json
- import sys
- class NoIndent(object):
- def __init__(self, value):
- self.value = value
- def default(o, encoder=json.JSONEncoder()):
- if isinstance(o, NoIndent):
- return json.dumps(o.value)
- return encoder.default(o)
- L = [dict(x=x, y=y) for x in range(1) for y in range(2)]
- obj = [NoIndent(L), L]
- json.dump(obj, sys.stdout, default=default, indent=4)
- [
- "[{"y": 0, "x": 0}, {"y": 1, "x": 0}]",
- [
- {
- "y": 0,
- "x": 0
- },
- {
- "y": 1,
- "x": 0
- }
- ]
- ]
- import sys
- import yaml
- class NoIndentList(list):
- pass
- def noindent_list_presenter(dumper, data):
- return dumper.represent_sequence(u'tag:yaml.org,2002:seq', data,
- flow_style=True)
- yaml.add_representer(NoIndentList, noindent_list_presenter)
- obj = [
- [dict(x=x, y=y) for x in range(2) for y in range(1)],
- [dict(x=x, y=y) for x in range(1) for y in range(2)],
- ]
- obj[0] = NoIndentList(obj[0])
- yaml.dump(obj, stream=sys.stdout, indent=4)
- - [{x: 0, y: 0}, {x: 1, y: 0}]
- - - {x: 0, y: 0}
- - {x: 0, y: 1}
- def custom_print(data_structure, indent):
- for key, value in data_structure.items():
- print "n%s%s:" % (' '*indent,str(key)),
- if isinstance(value, dict):
- custom_print(value, indent+1)
- else:
- print "%s" % (str(value)),
- >>> custom_print(data_structure,1)
- layer1:
- layer2:
- layer3_2: string
- layer3_1: [{'y': 7, 'x': 1}, {'y': 4, 'x': 0}, {'y': 3, 'x': 5}, {'y': 9, 'x': 6}]
- def default(self, o):
- if isinstance(o, NoIndent):
- key = uuid.uuid4().hex
- self._replacement_map[key] = json.dumps(o.value)
- return "@@%s@@" % (key,)
- else:
- return super(NoIndentEncoder, self).default(o)
- def encode(self, o):
- result = super(NoIndentEncoder, self).encode(o)
- for k, v in self._replacement_map.iteritems():
- result = result.replace('"@@%s@@"' % (k,), v)
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement