# Delimited serialization example

**URL:** <https://forum.opencyphal.org/t/delimited-serialization-example/975>\
**Category:** Applications & Usage\
**Created:** [November 1, 2020, 4:01am UTC](https://forum.opencyphal.org/t/delimited-serialization-example/975 "2020-11-01T04:01:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [November 1, 2020, 4:01am UTC](https://forum.opencyphal.org/t/delimited-serialization-example/975/1 "2020-11-01T04:01:12Z")

</div>

Here is an example of delimited serialization in action. I wrote it while working on the serialization test suite for PyUAVCAN, which you can find under [`tests/dsdl/`](https://github.com/UAVCAN/pyuavcan/).

You can reproduce this locally to understand how it works using [PyUAVCAN](https://pyuavcan.readthedocs.io) in REPL or [in Jupyter](https://github.com/pavel-kirienko/jupyter-pyuavcan-demo).

## Serializing A.1.0

### DSDL definitions

```python
# A.1.0
@union
BSealed.1.0 sea
BDelimited.1.0 del
@extent 56 * 8

```

```python
# BSealed.1.0
CVariable.1.0[<=2] var
CFixed.1.0[<=2] fix
@sealed

```

```python
# BDelimited.1.0
CVariable.1.0[<=2] var
CFixed.1.0[<=2] fix
@extent 40 * 8

```

```python
# CVariable.1.0
uint8[<=2] a
int8 b
@extent 4 * 8

```

```python
# CFixed.1.0
uint8[2] a
@extent 4 * 8

```

### Instantiating and serializing

```python
o = A_1_0(
    del_=BDelimited_1_0(
        var=[CVariable_1_0(a=[1, 2], b=0), CVariable_1_0(a=[3], b=4)],
        fix=[CFixed_1_0(a=[5, 6])],
    ),
)

```

```auto
01 17 00 00 00 02 04 00 00 00 02 01 02 00 03 00 00 00 01 03 04 01 02 00 00 00 05 06
| | _________/ | |_________ / | | ___/ | |_________ / | | | | | _________/ |___ /
| | | | | | | | | | | | | |
| | | | | | | | | | | | | Array a (fixed length)
| | | | | | | | | | | | Delimiter header of the only array element
| | | | | | | | | | | Array fix contains 1 element
| | | | | | | | | | Field b
| | | | | | | | | Array a
| | | | | | | | Array a contains 1 element
| | | | | | | Delimiter header of the 2nd array element
| | | | | | Field b left uninitialized
| | | | | Array a
| | | | Array a contains 2 elements
| | | Delimiter header of the 1st array element
| | Array var contains 2 elements
| Delimiter header of BDelimited.1.0 del
Union tag of del

```

## Deserializing A.1.1

Now, the serialized representation obtained above can be deserialized using data types of different versions that contain different fields. The result is well-defined thanks to the data type extensibility capabilities of UAVCAN v1.

### DSDL definitions

```python
# A.1.1
@union
BSealed.1.0 sea
BDelimited.1.1 del
@extent 56 * 8

```

```python
# BDelimited.1.1.uavcan
CVariable.1.1[<=2] var
CFixed.1.1[<=2] fix
@extent 40 * 8

```

```python
# CVariable.1.1.uavcan
uint8[<=2] a
@extent 4 * 8

```

```python
# CFixed.1.1.uavcan
uint8[3] a
int8 b
@extent 4 * 8

```

### Deserializing

```python
o = pyuavcan.dsdl.deserialize(A_1_1, [memoryview(sr)])
assert o.sea is None
assert o.del_ is not None
assert len(o.del_.var) == 2
assert len(o.del_.fix) == 1
assert list(o.del_.var[0].a) == [1, 2]
assert list(o.del_.var[1].a) == [3] # b is implicitly truncated
assert list(o.del_.fix[0].a) == [5, 6, 0] # 3rd is implicitly zero-extended
assert o.del_.fix[0].b == 0 # b is implicitly zero-extended

```

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [September 13, 2021, 4:47pm UTC](https://forum.opencyphal.org/t/delimited-serialization-example/975/2 "2021-09-13T16:47:29Z")

</div>

Cross-linking the background here for future reference:

> [@Data type extensibility and composition](https://forum.opencyphal.org/t/data-type-extensibility-and-composition/827/15):
>
> So I’ve made some changes to PyDSDL which are currently [on a branch](https://github.com/UAVCAN/pydsdl/compare/beta). It is more or less functionally complete but the tests are not yet updated. I had to introduce certain modifications to the proposal I shared here earlier because a closer look revealed some flaws. One minor change is that I renamed “capacity” into “extent” in order to avoid overloading the term “capacity” as applied to arrays. Other than that, it is the same: extent is defined as the amount of buffer memory that is required f…
