<commit_before># -*- test-case-name: ranger-ims-server.ext.test.test_json -*-
"""
Extensions to :mod:`json`
"""

from datetime import date as Date, datetime as DateTime
from io import TextIOWrapper
from json import JSONDecodeError, JSONEncoder, dumps, load, loads
from typing import Any, Optional
from typing.io import BinaryIO

from arrow.parser import DateTimeParser


__all__ = (
    "dateAsRFC3339Text",
    "dateTimeAsRFC3339Text",
    "jsonTextFromObject",
    "objectFromJSONText",
    "rfc3339TextAsDate",
    "rfc3339TextAsDateTime",
)



Optional  # pyflakes



class Encoder(JSONEncoder):
    """
    JSON encoder that converts treats any iterable as a list.
    """

    def default(self, obj: Any) -> Any:
        iterate = getattr(obj, "__iter__", None)
        if iterate is not None:
            return list(iterate())

        return JSONEncoder.default(self, obj)
<commit_msg>Handle mappings as well as iterables<commit_after># -*- test-case-name: ranger-ims-server.ext.test.test_json -*-
"""
Extensions to :mod:`json`
"""

from datetime import date as Date, datetime as DateTime
from io import TextIOWrapper
from json import JSONDecodeError, JSONEncoder, dumps, load, loads
from typing import Any, Optional
from typing.io import BinaryIO

from arrow.parser import DateTimeParser


__all__ = (
    "dateAsRFC3339Text",
    "dateTimeAsRFC3339Text",
    "jsonTextFromObject",
    "objectFromJSONText",
    "rfc3339TextAsDate",
    "rfc3339TextAsDateTime",
)



Optional  # pyflakes



class Encoder(JSONEncoder):
    """
    JSON encoder that attempts to convert :class:`Mapping` to :class:`dict`,
    and other types of :class:`Iterable` to :class:`list`.
    """

    def default(self, obj: Any) -> Any:
        iterate = getattr(obj, "__iter__", None)
        if iterate is not None:
            # We have an Iterable
            if hasattr(obj, "__getitem__"):
                # We have a Mapping
                return dict(obj)
            return list(iterate())

        return JSONEncoder.default(self, obj)

{'fraction_non_alphanumeric': 0.10285433070866142, 'fraction_numerical': 0.015748031496062992, 'mean_word_length': 3.226611226611227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '615736', 'n_tokens_mistral': 644, 'n_tokens_neox': 605, 'n_words': 184}