Skip to main content

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[].

MembersCategoryReading and WritingNote
StructDecoder(Session& ua)Structure—Bind to Session (explicit)
decoder.Decode(variant)MethodsReadReturns std::unordered_map<std::string, std::any>
decoder.DecodeBody(body, data_type_node_id)MethodsReadDirect decoding when DataTypeNodeId the "body" byte stream is known
decoder.ClearCache()MethodsWriteClear the DataTypeDefinition cache
decoder.CachedDefinitionCount()MethodsReadNumber of cached definitions
map[key]IndexReadRetrieve the field whose value is std::any
StructDecoder::kStatusKeyConstants—"_status" — Decoding failed / Key for the placeholder result
StructDecoder::kTypeIdKeyConstants—"_typeId" — Store the placeholder result under the key "TypeId"
StructDecoder::kAttrDataTypeDefinitionConstants—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 returns nullptr), it DecodeBody returns 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#PythonJavaC++RustC
StructDecoderStructDecoderStructDecoderStructDecoderStructDecoderDarraUa_StructDecoderHandle
DecodedecodedecodeDecodedecodeDarraUa_StructDecoder_Decode
DecodeBodydecode_bodydecodeBodyDecodeBodydecode_bodyDarraUa_StructDecoder_DecodeBody
ClearCacheclear_cache—ClearCacheclear_cacheDarraUa_StructDecoder_ClearCache
CachedDefinitionCountcached_definition_count—CachedDefinitionCountcached_definition_countDarraUa_StructDecoder_CachedCount

Next Step​