Method SerializeBinary
| Edit this page View SourceSerializeBinary(ChannelGrid)
Serializes a ChannelGrid to a byte array using a custom binary format that includes metadata and channel data.
Declaration
public static byte[] SerializeBinary(this ChannelGrid grid)
Parameters
| Type | Name | Description |
|---|---|---|
| ChannelGrid | grid | The ChannelGrid instance to serialize. This grid can contain multiple channels of different types, and the serialization will include all channels along with their metadata. |
Returns
| Type | Description |
|---|---|
| byte[] | A byte array containing the complete serialized ChannelGrid, including metadata and all channel data. |
Remarks
This method is specifically designed for ChannelGrid, which can contain multiple channels of different types.
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when a channel's element type does not have a full name, which is required for serialization. |
SerializeBinary(IGrid<float>)
Serializes float-based grids (such as DistanceGrid) using optimized binary encoding. This provides significant space and performance improvements over JSON serialization.
Declaration
public static byte[] SerializeBinary(this IGrid<float> grid)
Parameters
| Type | Name | Description |
|---|---|---|
| IGrid<float> | grid | The float-based grid to serialize. |
Returns
| Type | Description |
|---|---|
| byte[] | A byte array containing the binary-encoded grid data. |
Remarks
This overload specifically targets common float grids like distance fields, using direct memory copying for maximum performance.
SerializeBinary<T>(IGrid<T>)
Serializes grids containing unmanaged value types using optimized binary encoding. This method provides the best performance for simple value types, enums, and structs by directly copying memory without JSON overhead.
Declaration
public static byte[] SerializeBinary<T>(this IGrid<T> grid) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| IGrid<T> | grid | The grid to serialize. |
Returns
| Type | Description |
|---|---|
| byte[] | A byte array containing the binary-encoded grid data. |
Type Parameters
| Name | Description |
|---|---|
| T | The unmanaged value type stored in the grid (e.g., int, byte, enum, simple struct). |
Remarks
This method is ideal for numeric grids, enum-based grids, and simple struct grids. It uses AsBytes<T>(ReadOnlySpan<T>) for zero-copy serialization.
The unmanaged constraint ensures the type can be safely copied as raw bytes, providing significant performance benefits over JSON serialization.
SerializeBinary(IGrid<Occupancy>)
Serializes occupancy grids using standard binary serialization without compression. (Every occupancy value is stored as a full byte.)
Declaration
public static byte[] SerializeBinary(this IGrid<Occupancy> grid)
Parameters
| Type | Name | Description |
|---|---|---|
| IGrid<Occupancy> | grid | The occupancy grid to serialize. |
Returns
| Type | Description |
|---|---|
| byte[] | A byte array containing the binary-encoded occupancy data. |