## 5.2.6 Structures

Structures are encoded as a sequence of fields in the order that they appear in the definition. The encoding for each field is determined by the built-in type for the field.

All fields specified in the structure shall be encoded. If optional fields exist in the structure then see 5.2.7.

Structures do not have a null value. If an encoder is written in a programming language that allows structures to have null values, then the encoder shall create a new instance with default values for all fields and serialize that. Encoders shall not generate an encoding error in this situation.

The following is an example of a structure using C/C++ syntax:

struct Type2
{
Int32 A;
Int32 B;
};

struct Type1
{
Int32 X;
Byte NoOfY;
Type2* Y;
Int32 Z;
};



In the C/C++ example above, the Y field is a pointer to an array with a length stored in NoOfY. When encoding an array the length is part of the array encoding so the NoOfY field is not encoded. That said, encoders and decoders use NoOfY during encoding.

An instance of Type1 which contains an array of two Type2 instances would be encoded as 28-byte sequence. If the instance of Type1 was encoded in an ExtensionObject it would have an additional prefix shown in Table 17 which would make the total length 37 bytes The TypeId, Encoding and the Length are fields defined by the ExtensionObject. The encoding of the Type2 instances do not include any type identifier because it is explicitly defined in Type1.

Table 17 – Sample OPC UA Binary Encoded structure

Field Bytes Value
Type Id 4 The identifier for Type1
Encoding 1 0x1 for ByteString
Length 4 28
X 4 The value of field ‘X’
Y.Length 4 2
Y.A 4 The value of field ‘Y[0].A’
Y.B 4 The value of field ‘Y[0].B’
Y.A 4 The value of field ‘Y[1].A’
Y.B 4 The value of field ‘Y[1].B’
Z 4 The value of field ‘Z’

The Value of the DataTypeDefinition Attribute for a DataType Node describing Type1 is:

Name Type Description
defaultEncodingId NodeId NodeId of the “Type1_Encoding_DefaultBinary” Node.
baseDataType NodeId “i=22” [Structure]
structureType StructureType Structure_0 [Structure without optional fields]
fields [0] StructureField
name String “X”
description LocalizedText Description of X
dataType NodeId “i=6” [Int32]
valueRank Int32 -1 (Scalar)
isOptional Boolean false
fields [1] StructureField
name String “Y“
description LocalizedText Description of Y-Array
dataType NodeId NodeId of the Type2 DataType Node (e.g. “ns=3; s=MyType2”)
valueRank Int32 1 (OneDimension)
isOptional Boolean false
fields [2] StructureField
name String “Z“
description LocalizedText Description of Z
dataType NodeId “i=6” [Int32]
valueRank Int32 -1 (Scalar)
isOptional Boolean false

The Value of the DataTypeDefinition Attribute for a DataType Node describing Type2 is:

Name Type Description
defaultEncodingId NodeId NodeId of the “Type2_Encoding_DefaultBinary” Node.
baseDataType NodeId “i=22” [Structure]
structureType StructureType Structure_0 [Structure without optional fields]
fields [0] StructureField
name String “A“
description LocalizedText Description of A
dataType NodeId “i=6” [Int32]
valueRank Int32 -1 (Scalar)
isOptional Boolean false
fields [1] StructureField
name String “B“
description LocalizedText Description of B
dataType NodeId “i=6” [Int32]
valueRank Int32 -1 (Scalar)
isOptional Boolean false