|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Self-contained demo of pydsdl serialization and deserialization. |
| 4 | +
|
| 5 | +Demonstrates the complete workflow: |
| 6 | +1. Load a custom DSDL type using read_files() |
| 7 | +2. Create an object using the representation convention (dict, list, str, bytes, primitives) |
| 8 | +3. Serialize the object to bytes |
| 9 | +4. Deserialize the bytes back to an object |
| 10 | +5. Verify roundtrip equality |
| 11 | +""" |
| 12 | + |
| 13 | +import pydsdl |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def main() -> None: |
| 18 | + SCRIPT_DIR = Path(__file__).parent |
| 19 | + DSDL_FILE = SCRIPT_DIR / "DemoMessage.1.0.dsdl" |
| 20 | + |
| 21 | + print("=" * 70) |
| 22 | + print("PyDSDL Serialization/Deserialization Demo") |
| 23 | + print("=" * 70) |
| 24 | + |
| 25 | + print("\n[Step 1] Loading DSDL type from:", DSDL_FILE.name) |
| 26 | + |
| 27 | + types, _ = pydsdl.read_files( |
| 28 | + dsdl_files=DSDL_FILE, |
| 29 | + root_namespace_directories_or_names=SCRIPT_DIR, |
| 30 | + lookup_directories=[] |
| 31 | + ) |
| 32 | + |
| 33 | + schema = types[0] |
| 34 | + print(f"✓ Loaded type: {schema.full_name} v{schema.version.major}.{schema.version.minor}") |
| 35 | + print(f" Fields: {[f.name for f in schema.fields_except_padding]}") |
| 36 | + |
| 37 | + print("\n[Step 2] Creating example object") |
| 38 | + print(" Representation convention: dict→struct, list→array, primitives as-is") |
| 39 | + obj = { |
| 40 | + "flag": True, |
| 41 | + "counter": 42, |
| 42 | + "temperature": 23.5, |
| 43 | + "numeric_data": [1.0, 2.0, 3.0, 4.0], |
| 44 | + "text_data": list("Hello, SerDes!".encode("utf-8")), |
| 45 | + "binary_data": [0x00, 0x01, 0x02, 0x03] |
| 46 | + } |
| 47 | + |
| 48 | + print(" Object structure:") |
| 49 | + for key, value in obj.items(): |
| 50 | + value_repr = repr(value) |
| 51 | + if len(value_repr) > 50: |
| 52 | + value_repr = value_repr[:47] + "..." |
| 53 | + print(f" {key:15} = {value_repr} ({type(value).__name__})") |
| 54 | + |
| 55 | + print("\n[Step 3] Serializing object to bytes") |
| 56 | + serialized_data = pydsdl.serialize(schema, obj) |
| 57 | + print(f"✓ Serialized to {len(serialized_data)} bytes") |
| 58 | + print(f" Hex: {serialized_data.hex()}") |
| 59 | + |
| 60 | + print("\n[Step 4] Deserializing bytes back to object") |
| 61 | + deserialized = pydsdl.deserialize(schema, serialized_data) |
| 62 | + print("✓ Deserialized successfully") |
| 63 | + print(" Deserialized structure:") |
| 64 | + for key, value in deserialized.items(): |
| 65 | + value_repr = repr(value) |
| 66 | + if len(value_repr) > 50: |
| 67 | + value_repr = value_repr[:47] + "..." |
| 68 | + print(f" {key:15} = {value_repr} ({type(value).__name__})") |
| 69 | + |
| 70 | + print("\n[Step 5] Verifying roundtrip equality") |
| 71 | + assert obj == deserialized, "Roundtrip failed! Objects don't match." |
| 72 | + print("✓ Roundtrip verification passed: Original == Deserialized") |
| 73 | + |
| 74 | + print("\n" + "=" * 70) |
| 75 | + print("Demo completed successfully!") |
| 76 | + print("=" * 70) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments