Metadata-Version: 2.1
Name: tomaserial
Version: 1.0.3
Summary: python serial and deserial
Project-URL: Homepage, https://github.com/sunlulu427/tomato
Project-URL: Bug Tracker, https://github.com/sunlulu427/tomato/issues
Author-email: "sunlulu.tomato" <sunlulu.tomato@bytedance.com>
License: Copyright (c) <2023> <copyright sunlulu.tomato@bytedance.com>
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Requires-Dist: configparser; python_version >= '3.7'
Description-Content-Type: text/markdown

# Serial-Py

这是一个基于`python typing`的反序列化框架，它已经发布到Pypi，因此，你可以通过如下方式依赖它：
```shell
pip3 install 
```

## 数据类定义
使用时需要将数据按照如下方式定义：这里面包含嵌套类、枚举类
等，如果需要序列化的字段名称和变量名称不完全一致，可以实现`Serial.name_strategy`实现字段名称之间的映射。

```python
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`转换为
字符串。
```python
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`来实现字段名称之间的映射。

```python
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)
```