Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Render step 'data' JSON with indenting and sorted keys
Browse files Browse the repository at this point in the history
Summary:
The step configuration data is a config, rendered to JSON for human reading/editing.
Recognizing this, seems worthwhile to return it with indenting and sorted keys.
Note that it is also stored as JSON, but in that cases the JSON is just serialization.
This API json.dumps is effectively a rendering, and the spacing and ordering don't affect
meaning (and make order of keys defined, which is nice), so it doesn't feel too dirty to me.
An alternative would be to JSON.stringify(JSON.parse(data), null, 4) in the UI, but I don't know
how to do that, and at the moment the UI treatment of that data is just as text with the validation
happening on the server, so that'd be new territory.

Test Plan: Unit

Reviewers: vishal

Reviewed By: vishal

Subscribers: changesbot, wwu

Differential Revision: https://tails.corp.dropbox.com/D95663
  • Loading branch information
kylec1 committed Mar 14, 2015
1 parent 361cd24 commit f9a34da
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions changes/api/serializer/models/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def serialize(self, instance, attrs):
'implementation': instance.implementation,
'name': instance.implementation.rsplit('.', 1)[-1],
'order': instance.order,
'data': json.dumps(dict(instance.data or {})),
# 'data' is rendered as JSON string for human reading/editing,
# so we make it pretty.
'data': _pretty_json_dump(dict(instance.data or {})),
'dateCreated': instance.date_created,
'options': attrs['options'],
}
Expand All @@ -55,6 +57,13 @@ def serialize(self, instance, attrs):
'id': instance.id.hex,
'implementation': instance.implementation,
'name': implementation.get_label() if implementation else '',
'data': json.dumps(dict(instance.data or {})),
# 'data' is rendered as JSON string for human reading/editing,
# so we make it pretty.
'data': _pretty_json_dump(dict(instance.data or {})),
'options': instance.options,
}


def _pretty_json_dump(d):
"""Returns a human-readable JSON serialization of a value."""
return json.dumps(d, sort_keys=True, indent=3)

0 comments on commit f9a34da

Please sign in to comment.