美文网首页js css html
Python enum 的序列化

Python enum 的序列化

作者: alue | 来源:发表于2022-10-04 21:13 被阅读0次

枚举型的序列化

dataclass 数据封装里介绍了,dataclass 数据类型特别适合做接口定义。而且利用 dataclasses.asdict 方法,能够将dataclass转换为常规字典类型,方便json化处理。

例如

@dataclass
class Point:
     x: int
     y: int


@dataclass
class PointList:
     points: List[Point]

在内部使用时,IDE能够为Point类型数据提供类型提示校验。在对外做json转换时,只用对最上层的PointList数据对象做一次 asdict 处理就能方便的完成所有层的 dataclass与字典的转换:

points = PointList(points=[Point(1,0),Point(2,1)])
# 1行代码完成json转换
points_json = json.dumps(asdict(points))

但是,如果dataclass类型中包含枚举型,序列化就会报错:


import json  
from dataclasses import dataclass, asdict  
from typing import List  
from enum import Enum  
  

class Color(Enum):  
    red = 0  
    green = 1  
    blue = 2  
  
  
@dataclass  
class Point:  
    x: int  
    y: int  
    # 增加一个颜色属性,为枚举型  
    color: Color  
  
  
@dataclass  
class PointList:  
    points: List[Point]  
  
  
points = PointList(points=[Point(1, 0, Color.blue), Point(2, 1, Color.red)])  
# 1行代码完成json转换  
points_json = json.dumps(asdict(points))  
  
print(points_json)

会报出

TypeError: Object of type Color is not JSON serializable

这时候可以这样解决:

from enum import IntEnum  
# 用IntEnum代替Enum
class Color(IntEnum):  
    red = 0  
    green = 1  
    blue = 2

也就是说,通过继承IntEnum,Color有了整数类型的序列化方法。

如果枚举值是字符串,看上去对偶的实现方式应该如下:

from enum import StrEnum  
  
  
class Color(StrEnum):  
    red = "red"  
    green = "green"  
    blue = "blue"

但实际运行中,上述代码会报错,因为 StrEnum 在python 3.11之后才会支持。在低版本中,可以利用多重继承机制,来实现相同功能:

from enum import Enum  
  
  
class Color(str, Enum):  
    red = "red"  
    green = "green"  
    blue = "blue"

这样,str为Color提供了序列化方法,points整体序列化也能够得到正常的结果:

{"points": [{"x": 1, "y": 0, "color": "blue"}, {"x": 2, "y": 1, "color": "red"}]}

相关文章

  • Python enum 的序列化

    枚举型的序列化 在dataclass 数据封装[https://www.jianshu.com/p/8425784...

  • enum的序列化

    Enum 常量的序列化不同于普通的 serializable 或 externalizable 对象。enum 常...

  • 枚举序列化

    Enum 常量的序列化不同于普通的 serializable 或 externalizable 对象。enum 常...

  • 使用json模块

    json模块是python内置的用于序列化的模块。 序列化和反序列化 序列化就是将python类型的数据结构转换为...

  • Python_枚举

    一、枚举 Enum类 Python中所有枚举类型都是enum模块下的Enum类的子类。 枚举中的标识最好全部使用大...

  • Android中Serializable和Parcelable的

    前言 对于Android开发者来说,序列化总是一个不能避免的问题。前有“使用enum实现单例模式可以自动序列化”的...

  • Enum定义最佳实践

    Enum 在 jackson 序列化和反序列化时默认使用枚举的name(), 而一般存储的数据可能是自定义字段。可...

  • Android如何选择使用Parcelable和Serializ

    一、前言 对于Android开发者来说,序列化总是一个不能避免的问题。前有“使用enum实现单例模式可以自动序列化...

  • Python Enum

    之前基本不用Enum类型,看到同事写的代码里有。查阅了下基本用法和别人的总结。简单来说就是:可读性好,更安全,ke...

  • Python 入门之 内置模块 -- 序列化模块(json模块、

    Python 入门之 内置模块 -- 序列化模块(json模块、pickle模块) 1、序列化 Python中这种...

网友评论

    本文标题:Python enum 的序列化

    本文链接:https://www.haomeiwen.com/subject/xscuartx.html