deepsort
deepsort copied to clipboard
Add the ability to only patch `Array`, or only `Hash`
I have some code that serializes both Hash and Array objects as YAML.
I had originally written:
data = data.deep_stringify_keys if data.respond_to?(:deep_stringify_keys)
data = data.deep_sort if data.respond_to?(:deep_sort)
This is fine for Hash, since the YAML spec specifies that order in mappings is a serialization detail.
But of course deepsort also adds deep_sort to Array, which is not a serialization detail.
I ended up changing the code to check .is_a?(Hash)
data = data.deep_sort if data.is_a?(Hash)
but it would have been nice if there were a way to only patch Hash and not Array when using deepsort.