<commit_before>from __future__ import absolute_import, unicode_literals

import json

import datetime
from django.core.serializers.json import (
    Serializer as JsonSerializer,
    DjangoJSONEncoder)

from dimagi.utils.parsing import json_format_datetime


class JsonLinesSerializer(JsonSerializer):
    """
    Convert a queryset to JSON outputting one object per line
    """
    def start_serialization(self):
        self._init_options()

    def end_serialization(self):
        self.stream.write("\n")

    def end_object(self, obj):
        # self._current has the field data
        self.stream.write("\n")
        json.dump(self.get_dump_object(obj), self.stream,
                  cls=CommCareJSONEncoder, **self.json_kwargs)
        self._current = None


class CommCareJSONEncoder(DjangoJSONEncoder):
    """
    Custom version of the DjangoJSONEncoder that formats datetime's with all 6 microsecond digits
    """
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return json_format_datetime(o)
        else:
            super(CommCareJSONEncoder, self).default(o)<commit_msg>fix JSON encoder<commit_after>from __future__ import absolute_import, unicode_literals

import json

import datetime
from django.core.serializers.json import (
    Serializer as JsonSerializer,
    DjangoJSONEncoder)

from dimagi.utils.parsing import json_format_datetime


class JsonLinesSerializer(JsonSerializer):
    """
    Convert a queryset to JSON outputting one object per line
    """
    def start_serialization(self):
        self._init_options()

    def end_serialization(self):
        self.stream.write("\n")

    def end_object(self, obj):
        # self._current has the field data
        self.stream.write("\n")
        json.dump(self.get_dump_object(obj), self.stream,
                  cls=CommCareJSONEncoder, **self.json_kwargs)
        self._current = None


class CommCareJSONEncoder(DjangoJSONEncoder):
    """
    Custom version of the DjangoJSONEncoder that formats datetime's with all 6 microsecond digits
    """
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return json_format_datetime(o)
        else:
            return super(CommCareJSONEncoder, self).default(o)
{'fraction_non_alphanumeric': 0.08325952170062002, 'fraction_numerical': 0.0008857395925597874, 'mean_word_length': 3.183333333333333, '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': '2225164', 'n_tokens_mistral': 635, 'n_tokens_neox': 604, 'n_words': 160}