MaxKB/apps/common/encoder/encoder.py

38 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""
@project: MaxKB
@Author
@file SystemEncoder.py
@date2025/3/17 16:38
@desc:
"""
import datetime
import decimal
import json
import uuid
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
class SystemEncoder(json.JSONEncoder):
def encode(self, obj):
# 先序列化为字符串
json_str = super().encode(obj)
# 移除所有空字符
json_str = json_str.replace('\\u0000', '')
return json_str
def default(self, obj):
if isinstance(obj, uuid.UUID):
return str(obj)
if isinstance(obj, datetime.datetime):
return obj.strftime("%Y-%m-%d %H:%M:%S")
if isinstance(obj, decimal.Decimal):
return float(obj)
if isinstance(obj, InMemoryUploadedFile):
return {'name': obj.name, 'size': obj.size}
if isinstance(obj, TemporaryUploadedFile):
return {'name': obj.name, 'size': obj.size}
else:
return json.JSONEncoder.default(self, obj)