From 68ea6b9ef755296c169b5416fe46ac48250e3c62 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Fri, 9 Nov 2018 10:47:11 +0100 Subject: fix issue #194 object attributes always sorted 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))* --- representer.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'representer.py') 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 -- cgit v1.2.1