Structure Decoding
Overview
OPC UA 1.04+ Place the field definitions for each custom structure into the node’s DataType DataTypeDefinition attribute (AttributeId=27, Part 5 §6.4.2).StructDecoder Deserialize StructureDefinition ExtensionObject the UA Binary byte stream (Part 6 §5.2) provided by the server into a field dictionary; see “Implementation Limitations” below for the scope and limitations of resolvable Builtins.
API
StructDecoder A class in the namespace darra::opcua, header file darra/opcua/struct_decoder.hpp. The decoding result is a field std::unordered_map<std::string, std::any> dictionary — NoneDynamicExtensionObject For this type of packaging, the field name is simply the dot (.) in the map operator[].
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| StructDecoder(Session& ua) | Structure | — | Bind to Session (explicit) |
| decoder.Decode(variant) | Methods | Read | Returns std::unordered_map<std::string, std::any> |
| decoder.DecodeBody(body, data_type_node_id) | Methods | Read | Direct decoding when DataTypeNodeId the "body" byte stream is known |
| decoder.ClearCache() | Methods | Write | Clear the DataTypeDefinition cache |
| decoder.CachedDefinitionCount() | Methods | Read | Number of cached definitions |
| map[key] | Index | Read | Retrieve the field whose value is std::any |
| StructDecoder::kStatusKey | Constants | — | "_status" — Decoding failed / Key for the placeholder result |
| StructDecoder::kTypeIdKey | Constants | — | "_typeId" — Store the placeholder result under the key "TypeId" |
| StructDecoder::kAttrDataTypeDefinition | Constants | — | 27 — DataTypeDefinition's AttributeId |
Related Structures (StructureKind, Part 3 §8.49 StructureType):
- Structure (0) — General
- StructureWithSubtypedValues (1) — Contains subtypes
- StructureWithOptionalFields (2) — Contains optional fields (preceded by EncodingMask)
- Union (3) — Union (prefix selector)
- UnionWithSubtypedValues (4) — Union + Subtypes
Code Example
#include <darra/opcua/struct_decoder.hpp>
#include <any>
#include <iostream>
using namespace darra::opcua;
Stack::Init();
Session s("opc.tcp://localhost:4840");
s.Connect();
StructDecoder dec(s);
// 1) Read custom structure — Decode the data inside DataValue Variant
auto dv = s.Read("ns=2;s=Robot.CurrentPose");
auto pose = dec.Decode(dv.Value()); // std::unordered_map<std::string, std::any>
std::cout << "x = " << std::any_cast<double>(pose["X"]) << "\n";
std::cout << "y = " << std::any_cast<double>(pose["Y"]) << "\n";
// 2) Iterate over all fields — the result is a map, which can be iterated over directly
for (auto& [name, value] : pose)
std::cout << name << "\n";
// 3) The values of nested structure fields are std::unordered_map<std::string, std::any>
auto quat = std::any_cast<std::unordered_map<std::string, std::any>>(pose["Quaternion"]);
std::cout << "qw = " << std::any_cast<double>(quat["W"]) << "\n";
// 4) The value of the array field is a `std::vector<std::any>`
auto arr = std::any_cast<std::vector<std::any>>(pose["JointAngles"]);
for (auto& a : arr)
std::cout << std::any_cast<double>(a) << "\n";
// 5) If you have already obtained the `body EO ` from another source, decode it directly
std::vector<uint8_t> body = /* ... */;
auto manual = dec.DecodeBody(body, "ns=2;i=3001");
Implementation Constraints
- ExtensionObject If no bytes can be retrieved, Decode(Variant) returns a partial result containing the field
_status. - If raw bytes are already available, decode them directly using DecodeBody(bytes, type_id)
- The field definitions are taken from
LoadDefinition. Since the cache is currently not connected (always returnsnullptr), itDecodeBodyreturns an empty map at this stage—the signature remains unchanged once the cache is connected. - Built-in types that have not been overridden (such as DiagnosticInfo, i=25) are thrown
std::runtime_error("Builtin type not implemented in StructDecoder"); they are not silently skipped.
Best Practices
- Share a decoder; definitions for the same server and type are loaded only once.
- Server-side: DataType Define the changes, call the function, then decode again
ClearCache(). - The value of the nested structure is a map;
std::any_cast<std::unordered_map<std::string, std::any>>retrieve it using and then access it. - Union: Check the selector—after decoding, there is only one field; use the field name to determine which branch is selected.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| StructDecoder | StructDecoder | StructDecoder | StructDecoder | StructDecoder | DarraUa_StructDecoderHandle |
| Decode | decode | decode | Decode | decode | DarraUa_StructDecoder_Decode |
| DecodeBody | decode_body | decodeBody | DecodeBody | decode_body | DarraUa_StructDecoder_DecodeBody |
| ClearCache | clear_cache | — | ClearCache | clear_cache | DarraUa_StructDecoder_ClearCache |
| CachedDefinitionCount | cached_definition_count | — | CachedDefinitionCount | cached_definition_count | DarraUa_StructDecoder_CachedCount |