Skip to main content

Structure Decoding

Overview​

OPC UA 1.04+ Place the field definitions for each custom struct into the DataType node's DataTypeDefinition property (Part 3 §5.10; the Java side does not yet Enums.AttributeId include this property).StructDecoder Solution UA Binary: Primitive types + nested structs + arrays + unions + Optional fields (enums are resolved based on their underlying integers).

API​

MembersCategoryReading and WritingNote
new StructDecoder(OpcUaSession ua)Structure—Bound to the session
decoder.decode(OpcUaVariant variant)MethodsReadDecode Variant (ExtensionObject), returns Map<String, Object>
decoder.decodeBody(byte[] body, String dataTypeId)MethodsReadPass the bytes directly + decode DataType, then return Map<String, Object>
decoder.registerDefinition(StructDefinition def)MethodsWriteRegister a field definition (cached by dataTypeId)
map.get(fieldName)MethodsReadGet Field (Map Standard Method)
map.entrySet()MethodsReadIterate through all field names → values

Related Structures ( StructDefinition.Kind Nested Enums):

public enum Kind {
Structure, // Regular
StructureWithSubtypedValues, // Fields of subtype
StructureWithOptionalFields, // Includes optional fields (prefix) UInt32 EncodingMask
Union, // Union (prefix selector UInt32 )
UnionWithSubtypedValues // Union + Subtype Fields
}

The parsed values are defined by field Map<String, Object>: scalar fields are directly Boolean / Integer / Long / Float / Double / String / Instant / UUID / byte[] / Enums.StatusCode (NodeId parsed as a string); nested structures are Map<String, Object>; array fields are List<Object>.

Code Example​

import com.darra.opcua.*;
import java.util.List;
import java.util.Map;

try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();

StructDecoder dec = new StructDecoder(ua);

// 1) First, register the field definitions (using the cache dataTypeId , one entry per type)
StructDefinition def = new StructDefinition();
def.name = "CartesianCoordinates";
def.dataTypeId = "ns=2;i=3001";
def.kind = StructDefinition.Kind.Structure;
def.fields.add(new StructField("X", "i=11", -1)); // i=11 = Double, -1 = scalar
def.fields.add(new StructField("Y", "i=11", -1));
dec.registerDefinition(def);

// 2) Read + Decode (using existing raw bytes dec.decodeBody(bytes, dataTypeId))
try (OpcUaDataValue dv = ua.read("ns=2;s=Robot.CurrentPose")) {
Map<String, Object> pose = dec.decode(dv.variant());
System.out.println("x = " + pose.get("X"));
System.out.println("y = " + pose.get("Y"));

// 3) Full-field traversal
for (Map.Entry<String, Object> e : pose.entrySet())
System.out.println(e.getKey() + " = " + e.getValue());

// 4) Nested structures are also of type `Map<String, Object>`
Map<String, Object> quat = (Map<String, Object>) pose.get("Quaternion");
System.out.println("qw = " + quat.get("W"));

// 5) The array field is a List<Object>
List<Object> arr = (List<Object>) pose.get("JointAngles");
for (Object a : arr) System.out.println(a);
}
}

Implementation Constraints​

  • decode(OpcUaVariant) If ExtensionObject the original byte stream cannot be obtained, return a Map containing the _status field (with the hint text as the value).
  • If raw bytes are already available, use decodeBody(byte[], String dataTypeId) direct decoding (using the same registration definition)
  • Field definitions must be registerDefinition(StructDefinition) registered manually; server-side DataTypeDefinition property retrieval has not yet been implemented.
  • If a nested structure is not registered, the field cannot be retrieved (returns null); maximum recursion depth is 100, maximum array length is 1M, maximum length forByteString String is [] 16MB, and the program terminates if these limits are exceeded. _status
  • DiagnosticInfo Skip

Best Practices​

  • Share a decoder; reuse the field table cache
  • First, then registerDefinition decodeBody, define registration on a per-record basis by dataTypeId
  • Cast after extracting nested structures Map<String, Object>; cast after extracting array fields List<Object>
  • The Map returned by Union contains only the field selected by the selector.

Cross-Language Comparison​

C#PythonJavaC++RustC
StructDecoderStructDecoderStructDecoderStructDecoderStructDecoderDarraUa_StructDecoderHandle
DecodedecodedecodeDecodedecodeDarraUa_StructDecoder_Decode

Next Step​