Value to Bytes Encoder
This document outlines the creation of a JSON schema for constructing custom byte arrays (packets or payloads). A schema dictates the sequential assembly of bytes. The system parses the schema sequentially to construct the final byte array.
Schema Fundamentals
The schema is a JSON array containing specific “fields”. Three field types are supported for payload construction:
- Static: Fixed, hardcoded bytes.
- Dynamic: Live input data.
- Metadata Length: Automated byte calculation for referenced fields.
General structure:
[
{ "type": "...", ... },
{ "type": "...", ... }
]1. Static Fields
Used for fixed byte sequences (e.g., headers, footers).
Data formats accepted:
- Numbers: Integer values between
0and255(1 byte). - Hexadecimal Strings: Strings prefixed with
"0x"(e.g.,"0xA1","0xFF").
Example:
{
"type": "static",
"id": "header_field",
"data": ["0xAA", "0xBB", 255, 0]
}id: Unique identifier.data: Array of bytes to output. The example outputsAA BB FF 00.
2. Dynamic Fields
Used for integrating live data streams. Requires defining the data size and byte order.
Example:
{
"type": "dynamic",
"id": "sensor_payload",
"key": "temperature_input",
"data_type": "I32",
"endian": "be"
}id: Unique identifier.key: Target data channel name.data_type: Target format configuration. Supported values:- Numeric:
"U8","I8","U16","I16","U32","I32","U64","I64","F32","F64" - Non-numeric:
"STRING","BYTES","BOOL"
- Numeric:
endian: Byte order for multi-byte numbers."be": Big-Endian (most significant byte first)."le": Little-Endian (least significant byte first).
3. Metadata Length Fields
Calculates the total byte size of specified fields. Required for dynamic payload sizing.
Example:
{
"type": "metadata_length",
"id": "payload_size",
"ids": ["sensor_payload", "extra_data"],
"data_type": "U8",
"endian": "be"
}id: Unique identifier.ids: Array of field IDs to measure. The total byte size of all referenced fields is summed.data_type: Output format for the length value (e.g.,"U8","U16").endian: Byte order ("be"or"le").
Complete Example
Objective: Construct a packet with the following structure:
- 2-byte Header (
0xAA 0xBB). - 1-byte Length indicator.
- Dynamic payload (
I32fromspeed_sensorchannel). - 1-byte Footer (
0xFF).
JSON Schema:
[
{
"type": "static",
"id": "header",
"data": ["0xAA", "0xBB"]
},
{
"type": "metadata_length",
"id": "length_byte",
"ids": ["live_speed"],
"data_type": "U8",
"endian": "be"
},
{
"type": "dynamic",
"id": "live_speed",
"key": "speed_sensor",
"data_type": "I32",
"endian": "be"
},
{
"type": "static",
"id": "footer",
"data": ["0xFF"]
}
]Execution Flow:
Given an incoming speed_sensor integer reading of 1000:
- Static header is evaluated:
AA BB - Dynamic field converts
1000to a 4-byteI32:00 00 03 E8 - Metadata Length evaluates the payload size (4 bytes) and outputs as
U8:04 - Static footer is evaluated:
FF
Final Byte Array Output: AA BB 04 00 00 03 E8 FF