Skip to Content
CloudBlueprint EditorValue to Bytes Encoder

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:

  1. Static: Fixed, hardcoded bytes.
  2. Dynamic: Live input data.
  3. 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 0 and 255 (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 outputs AA 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"
  • 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:

  1. 2-byte Header (0xAA 0xBB).
  2. 1-byte Length indicator.
  3. Dynamic payload (I32 from speed_sensor channel).
  4. 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:

  1. Static header is evaluated: AA BB
  2. Dynamic field converts 1000 to a 4-byte I32: 00 00 03 E8
  3. Metadata Length evaluates the payload size (4 bytes) and outputs as U8: 04
  4. Static footer is evaluated: FF

Final Byte Array Output: AA BB 04 00 00 03 E8 FF

Last updated on