Quellcode für text_lint.operations.lookups.encoders.reversed

"""ReversedEncoder class."""

import json
from typing import Any, Dict


[Doku] class ReversedEncoder(json.JSONEncoder): """JSON encoder that reverses dictionaries, lists and tuples."""
[Doku] def encode(self, o: Any) -> Any: """Encode as JSON while reversing dictionaries, lists and tuples. :param o: The object being converted. :returns: The converted object. """ value = self._recursive_reversed(o) return super().encode(value)
def _recursive_reversed( self, target: Any, ) -> Any: if isinstance(target, (list, tuple)): target = list( reversed([self._recursive_reversed(element) for element in target]) ) if isinstance(target, dict): new_dictionary: Dict[Any, Any] = {} for key in reversed(target.keys()): new_dictionary[key] = self._recursive_reversed(target[key]) target = new_dictionary return target