diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-11-09 10:47:11 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-11-09 10:47:11 +0100 |
commit | 68ea6b9ef755296c169b5416fe46ac48250e3c62 (patch) | |
tree | 8a44637378f20053b3c22ed342bdb2d13805dc97 /representer.py | |
parent | 11e092f5746f4437628a4131ea8727db6c4c5692 (diff) | |
download | ruamel.yaml-68ea6b9ef755296c169b5416fe46ac48250e3c62.tar.gz |
fix issue #194 object attributes always sorted0.15.77
Test for this issue in:
https://bitbucket.org/ruamel/yaml.data/src/default/object/control_base_representer_mapping_sort.yaml
*When this change indeed resolves your problem, please **Close** this issue*.
*(You can do so using the WorkFlow pull-down (close to the top right of this page))*
Diffstat (limited to 'representer.py')
-rw-r--r-- | representer.py | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/representer.py b/representer.py index e0ac31d..143b7d5 100644 --- a/representer.py +++ b/representer.py @@ -65,6 +65,7 @@ class BaseRepresenter(object): self.represented_objects = {} # type: Dict[Any, Any] self.object_keeper = [] # type: List[Any] self.alias_key = None # type: Optional[int] + self.sort_base_mapping_type_on_output = True @property def serializer(self): @@ -209,10 +210,11 @@ class BaseRepresenter(object): best_style = True if hasattr(mapping, 'items'): mapping = list(mapping.items()) - try: - mapping = sorted(mapping) - except TypeError: - pass + if self.sort_base_mapping_type_on_output: + try: + mapping = sorted(mapping) + except TypeError: + pass for item_key, item_value in mapping: node_key = self.represent_key(item_key) node_value = self.represent_data(item_value) @@ -986,8 +988,7 @@ class RoundTripRepresenter(SafeRepresenter): best_style = False value.append((node_key, node_value)) if flow_style is None: - if ((item_count != 0) or bool(merge_list)) \ - and self.default_flow_style is not None: + if ((item_count != 0) or bool(merge_list)) and self.default_flow_style is not None: node.flow_style = self.default_flow_style else: node.flow_style = best_style |