Protocol Translation Rules
Protocol translation involves not just mapping headers and metadata between different messaging protocols, but also transforming the payload data itself. This document outlines the standardized rules we use for converting between different data formats while preserving semantic meaning.
Payload Transformation Basics
When messages travel between different protocols, the payload often needs to be transformed to accommodate:
- Different data formats (JSON, XML, CBOR, Protocol Buffers, etc.)
- Different data models (hierarchical vs. flat)
- Different encoding schemes (UTF-8, ASCII, binary)
- Different size constraints (some protocols have stricter payload size limits)
Our translation engine applies the following principles to ensure reliable message conversions:
Principle: Preserve semantic equivalence over syntactic exactness
Data Format Transformations
Below are examples of how our system transforms data between common formats:
JSON to XML Transformation
{
"sensor": {
"id": "temp_01",
"reading": 22.5,
"unit": "celsius",
"timestamp": 1633372800
}
}
Transforms to:
<sensor>
<id>temp_01</id>
<reading>22.5</reading>
<unit>celsius</unit>
<timestamp>1633372800</timestamp>
</sensor>
JSON to CBOR Transformation
JSON data is converted to CBOR's efficient binary format while preserving structure:
{
"command": "activate",
"device": "lighting",
"zone": "kitchen",
"level": 80
}
Transforms to CBOR (shown in hex):
A4 # map(4)
67 # text(7)
636F6D6D616E64 # "command"
68 # text(8)
61637469766174 # "activate"
66 # text(6)
646576696365 # "device"
68 # text(8)
6C69676874696E67 # "lighting"
64 # text(4)
7A6F6E65 # "zone"
67 # text(7)
6B69746368656E # "kitchen"
65 # text(5)
6C6576656C # "level"
18 50 # unsigned(80)
Protocol Buffer Serialization
For IoT gateways using Protocol Buffers:
syntax = "proto3";
message SensorReading {
string device_id = 1;
double value = 2;
string unit = 3;
int64 timestamp = 4;
}
Our translation engine handles serialization and deserialization to other formats automatically.
Common Transformation Rules
Source Format | Target Format | Transformation Rule | Special Considerations |
---|---|---|---|
JSON | XML | Object properties become elements; arrays become repeated elements | JSON null values transformed to empty XML elements with xsi:nil="true" |
XML | JSON | Elements become properties; attributes become properties with @ prefix | Namespace information may be lost |
JSON | CBOR | Direct structural mapping with binary encoding | Results in smaller payload size |
MQTT (Binary) | CoAP | Content-Format header added to specify payload type | May need chunking for large payloads |
Plain Text | JSON | Text stored in {"value": "text content"} object | Escaping special characters |
AMQP (AMQP Value) | MQTT | Type information mapped to MQTT properties in 5.0 | Type information lost in MQTT 3.1.1 |
Data Type Mappings
The following type mappings are preserved during protocol translations:
Data Type | JSON | XML | CBOR | Protocol Buffers |
---|---|---|---|---|
Integer | number | integer | integer | int32/int64 |
Float | number | decimal | float | float/double |
String | string | string | text | string |
Boolean | true/false | true/false | true/false | bool |
Array | [] | repeated elements | array | repeated |
Object | nested elements | map | message | |
Binary | base64 string | base64 string | bytes | bytes |
Null | null | xsi:nil="true" | null | default values |
Content Negotiation Process
When a client connects through one protocol (e.g., MQTT) and subscribers expect another format (e.g., AMQP), our broker:
- Identifies the original content type
- Converts payload according to transformation rules
- Adds appropriate content type headers
- Delivers the transformed message
Best Practices for Protocol Integration
When designing systems that will use cross-protocol messaging:
- Define a canonical data model for your domain that maps well to multiple formats
- Include content type metadata whenever possible
- Use schema validation on both ends to ensure transformation integrity
- Test with edge cases including empty values, special characters, and very large payloads
- Document format expectations for each integration point
Advanced Features Enterprise
Enterprise users can create custom transformation rules for proprietary formats and configure validation pipelines to ensure data integrity during translation.
// Example custom transformation configuration
{
"sourceFormat": "CSV",
"targetFormat": "JSON",
"fieldMap": [
{"source": "column1", "target": "deviceId"},
{"source": "column2", "target": "readings.temperature"},
{"source": "column3", "target": "readings.humidity"}
],
"transforms": [
{"field": "readings.temperature", "function": "multiply", "value": 9/5},
{"field": "readings.temperature", "function": "add", "value": 32}
]
}
Performance Considerations
Translation operations have performance implications:
- JSON ↔ XML transformations are more CPU intensive
- Binary formats like CBOR and Protocol Buffers offer better performance
- Pre-compiled schemas can accelerate Protocol Buffer translations
- Caching frequently used transformations improves throughput
By following these translation rules, messages can flow seamlessly across protocol boundaries while maintaining their semantic meaning.