python serial and deserial
Project description
Serial-Py
这是一个基于python typing
的反序列化框架,它已经发布到Pypi,因此,你可以通过如下方式依赖它:
pip3 install
数据类定义
使用时需要将数据按照如下方式定义:这里面包含嵌套类、枚举类
等,如果需要序列化的字段名称和变量名称不完全一致,可以实现Serial.name_strategy
实现字段名称之间的映射。
from dataclasses import dataclass
from enum import Enum
from typing import List
from src.serial import Serial
class ShaderType(Enum):
vertex = 1
fragment = 2
@dataclass
class Shader(Serial):
code: str = None
shader: ShaderType = None
attributes: List[int] = None
compiled: bool = False
attached: bool = False
@dataclass
class Program(Serial):
vertex_shader: Shader = None
fragment_shader: Shader = None
linked: bool = False
byte_buffers: List[float] = None
序列化
你可以调用Serial.json()
将class
转为dict
,也可以调用Serial.str()
直接将class
转换为
字符串。
class SerialTestCase(unittest.TestCase):
vertex_shader = Shader(
code="vertex shader",
shader=ShaderType.vertex,
attributes=[1, 2, 3],
compiled=True,
attached=False
)
fragment_shader = Shader(
code="fragment shader",
shader=ShaderType.fragment,
attributes=[],
compiled=False,
attached=True
)
program = Program(
vertex_shader=vertex_shader,
fragment_shader=fragment_shader,
linked=True,
byte_buffers=[0.1, 0.2, 0.3]
)
def test_serialize(self):
program_dict = self.program.json()
print(program_dict)
vertex_shader: dict = program_dict.get('vertex_shader')
self.assertIsInstance(vertex_shader, dict)
self.assertEqual(vertex_shader.get("compiled"), True)
attributes = vertex_shader.get("attributes")
self.assertIsInstance(attributes, list)
self.assertEqual(len(attributes), 3)
self.assertEqual(attributes[0], 1)
program_str = self.program.str(indent=2, ensure_ascii=True)
print(program_str)
反序列化
你可以直接调用如Program.from_str(...)
或Program.from_json(dict)
来将字符串或者字典转换成某种类型,
当然枚举类和嵌套类都是支持的,同样可以通过定义Serial.name_strategy
来实现字段名称之间的映射。
def test_de_serialize(self):
json_str = """
{
"vertex_shader": {
"shader": "vertex",
"attributes": [
1,
2,
3
],
"compiled": true,
"attached": false
},
"fragment_shader": {
"shader": "fragment",
"attributes": [],
"compiled": false,
"attached": true
},
"linked": true,
"byte_buffers": [
0.1,
0.2,
0.3
]
}
""".strip()
de_serial_result = Program.from_str(json_str)
self.assertIsInstance(de_serial_result, Program)
self.assertTrue(de_serial_result.linked)
buffers = de_serial_result.byte_buffers
self.assertIsInstance(buffers, list)
self.assertEqual(len(buffers), 3)
# for nested classes
vertex_shader = de_serial_result.vertex_shader
self.assertIsInstance(vertex_shader, Shader)
self.assertTrue(vertex_shader.compiled)
self.assertFalse(vertex_shader.attached)
self.assertIsInstance(vertex_shader.attributes, list)
self.assertEqual(vertex_shader.attributes.__len__(), 3)
# for enum
self.assertIsInstance(vertex_shader.shader, ShaderType)
self.assertEqual(vertex_shader.shader, ShaderType.vertex)
fragment_shader = de_serial_result.fragment_shader
self.assertIsInstance(fragment_shader, Shader)
self.assertTrue(fragment_shader.attached)
self.assertIsInstance(fragment_shader.attributes, list)
self.assertEqual(len(fragment_shader.attributes), 0)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tomaserial-1.0.3.tar.gz
(7.1 kB
view hashes)
Built Distribution
Close
Hashes for tomaserial-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df3775d3cf37d66cefc475a1b745eb42c86ea8a35432c0e2902e643e8867b356 |
|
MD5 | a041bd6a068cafb4898e77d7079a5084 |
|
BLAKE2b-256 | bf350cb890a4d94bd680937185a0d19ebfb4a54ea109f8235dc40bb55cb04c38 |