mirror of
https://github.com/taigaio/taiga-back
synced 2025-10-06 00:02:52 +02:00
97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
#
|
|
# Copyright (c) 2021-present Kaleidos INC
|
|
|
|
# The code is partially taken (and modified) from django rest framework
|
|
# that is licensed under the following terms:
|
|
#
|
|
# Copyright (c) 2011-2014, Tom Christie
|
|
# All rights reserved.
|
|
#
|
|
# Redistribution and use in source and binary forms, with or without
|
|
# modification, are permitted provided that the following conditions are met:
|
|
#
|
|
# Redistributions of source code must retain the above copyright notice, this
|
|
# list of conditions and the following disclaimer.
|
|
# Redistributions in binary form must reproduce the above copyright notice, this
|
|
# list of conditions and the following disclaimer in the documentation and/or
|
|
# other materials provided with the distribution.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
"""
|
|
Helper classes for parsers.
|
|
"""
|
|
from django.db.models.query import QuerySet
|
|
from django.utils.functional import Promise
|
|
from django.utils import timezone
|
|
# from django.utils.deprecation import CallableBool
|
|
from django.utils.encoding import force_str
|
|
|
|
import datetime
|
|
import decimal
|
|
import types
|
|
import json
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
"""
|
|
JSONEncoder subclass that knows how to encode date/time/timedelta,
|
|
decimal types, and generators.
|
|
"""
|
|
def default(self, o):
|
|
# For Date Time string spec, see ECMA 262
|
|
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
|
if isinstance(o, Promise):
|
|
return force_str(o)
|
|
# elif isinstance(o, CallableBool):
|
|
# return bool(o)
|
|
elif isinstance(o, datetime.datetime):
|
|
r = o.isoformat()
|
|
if o.microsecond:
|
|
r = r[:23] + r[26:]
|
|
if r.endswith("+00:00"):
|
|
r = r[:-6] + "Z"
|
|
return r
|
|
elif isinstance(o, datetime.date):
|
|
return o.isoformat()
|
|
elif isinstance(o, datetime.time):
|
|
if timezone and timezone.is_aware(o):
|
|
raise ValueError("JSON can't represent timezone-aware times.")
|
|
r = o.isoformat()
|
|
if o.microsecond:
|
|
r = r[:12]
|
|
return r
|
|
elif isinstance(o, datetime.timedelta):
|
|
return str(o.total_seconds())
|
|
elif isinstance(o, decimal.Decimal):
|
|
return str(o)
|
|
elif isinstance(o, QuerySet):
|
|
return list(o)
|
|
elif hasattr(o, "tolist"):
|
|
return o.tolist()
|
|
elif hasattr(o, "__getitem__"):
|
|
try:
|
|
return dict(o)
|
|
except:
|
|
pass
|
|
elif hasattr(o, "__iter__"):
|
|
return [i for i in o]
|
|
|
|
return super(JSONEncoder, self).default(o)
|
|
|
|
|
|
SafeDumper = None
|