<commit_before>import ujson
from cached_property import cached_property
from peewee import Field, PostgresqlDatabase


try:
    from playhouse.postgres_ext import Json, JsonLookup
    PostgresqlDatabase.register_fields({
        'json': 'json'
    })
except:
    Json = JsonLookup = None


class JSONField(Field):

    def __init__(self, dumps=None, loads=None, *args, **kwargs):
        self.dumps = dumps or ujson.dumps
        self.loads = loads or ujson.loads
        super(JSONField, self).__init__(*args, **kwargs)

    @cached_property
    def db_field(self):
        if Json and isinstance(self.get_database(), PostgresqlDatabase):
            return 'json'
        return 'text'

    def db_value(self, value):
        if self.db_field == 'text':
            return self.dumps(value)

        if not isinstance(value, Json):
            return Json(value, dumps=self.dumps)

        return value

    def coerce(self, value):
        if self.db_field == 'text':
            return self.loads(value)
        return value

    def __getitem__(self, value):
        return JsonLookup(self, [value])
<commit_msg>Fix JSON field<commit_after>import ujson
from cached_property import cached_property
from peewee import Field, PostgresqlDatabase, Proxy


try:
    from playhouse.postgres_ext import Json, JsonLookup
    PostgresqlDatabase.register_fields({
        'json': 'json'
    })
except:
    Json = JsonLookup = None


class JSONField(Field):

    def __init__(self, dumps=None, loads=None, *args, **kwargs):
        self.dumps = dumps or ujson.dumps
        self.loads = loads or ujson.loads
        super(JSONField, self).__init__(*args, **kwargs)

    @cached_property
    def db_field(self):
        database = self.get_database()
        if isinstance(database, Proxy):
            database = database.obj
        if Json and isinstance(database, PostgresqlDatabase):
            return 'json'
        return 'text'

    def db_value(self, value):
        if self.db_field == 'text':
            return self.dumps(value)

        if not isinstance(value, Json):
            return Json(value, dumps=self.dumps)

        return value

    def coerce(self, value):
        if self.db_field == 'text':
            return self.loads(value)
        return value

    def __getitem__(self, value):
        return JsonLookup(self, [value])

{'fraction_non_alphanumeric': 0.09718670076726342, 'fraction_numerical': 0.0, 'mean_word_length': 2.621913580246914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4367368', 'n_tokens_mistral': 701, 'n_tokens_neox': 686, 'n_words': 186}