File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -465,6 +465,40 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
465465
466466def convert_value (value ):
467467 """转换值为JSON可序列化的类型"""
468+ if value is None :
469+ return None
470+ # 处理 bytes 类型(包括 BIT 字段)
471+ if isinstance (value , bytes ):
472+ # 1. 尝试判断是否是 BIT 类型
473+ if len (value ) <= 8 : # BIT 类型通常不会很长
474+ try :
475+ # 转换为整数
476+ int_val = int .from_bytes (value , 'big' )
477+
478+ # 如果是 0 或 1,返回布尔值更直观
479+ if int_val in (0 , 1 ):
480+ return bool (int_val )
481+ else :
482+ return int_val
483+ except :
484+ # 如果转换失败,尝试解码为字符串
485+ pass
486+
487+ # 2. 尝试解码为 UTF-8 字符串
488+ try :
489+ return value .decode ('utf-8' )
490+ except UnicodeDecodeError :
491+ # 3. 如果包含非打印字符,返回十六进制
492+ if any (b < 32 and b not in (9 , 10 , 13 ) for b in value ): # 非打印字符
493+ return f"0x{ value .hex ()} "
494+ else :
495+ # 4. 尝试 Latin-1 解码(不会失败)
496+ return value .decode ('latin-1' )
497+
498+ elif isinstance (value , bytearray ):
499+ # 处理 bytearray
500+ return convert_value (bytes (value ))
501+
468502 if isinstance (value , timedelta ):
469503 # 将 timedelta 转换为秒数(整数)或字符串
470504 return str (value ) # 或 value.total_seconds()
You can’t perform that action at this time.
0 commit comments