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 DefaultValues 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;
	UInt16 W[10];
	Byte M[2,3,4];
};

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. W is a fixed length array with an implicitly defined length of 10. This length is always encoded with the array. M is a fixed length multidimensional array. The length of each dimension is always encoded with the array.

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 29 which would make the total length 101 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 29 – Sample OPC UA Binary Encoded structure
FieldBytesValue
Type Id4The identifier for the ‘Type1’ Binary Encoding Node
Encoding10x1 for ByteString
Length492
X4The value of field ‘X’
Y.Length42
Y.A4The value of field ‘Y[0].A’
Y.B4The value of field ‘Y[0].B’
Y.A4The value of field ‘Y[1].A’
Y.B4The value of field ‘Y[1].B’
Z4The value of field ‘Z’
W.Length410
W20The value of field ‘W’.
M.Dimensions.Length43
M.Dimensions12The sequence [2,3,4] encoded as a Int32.
M.Values24The values in ‘M’ encoded sequentially as described in 5.2.2.16.

The Value of the DataTypeDefinition Attribute for a DataType Node describing Type1 is shown in Table 30.

Table 30 – DataTypeDefinition for "Type1" from Sample
Name Type Description
defaultEncodingIdNodeIdNodeId of the "Type1_Encoding_DefaultBinary" Node.
baseDataTypeNodeId"i=22" [Structure]
structureTypeStructureTypeStructure_0 [Structure without optional fields]
fields [0]StructureField

name

String"X"

description

LocalizedTextDescription of X

dataType

NodeId"i=6" [Int32]

valueRank

Int32-1 (Scalar)

arrayDimensions

UInt32[]null

maxStringLength

UInt320

isOptional

Booleanfalse
fields [1]StructureField

name

String"Y"

description

LocalizedTextDescription of Y-Array

dataType

NodeIdNodeId of the Type2 DataType Node (e.g. "ns=3;s=MyType2")

valueRank

Int321 (OneDimension)

arrayDimensions

UInt32[]{ 0 }

maxStringLength

UInt320

isOptional

Booleanfalse
fields [2]StructureField

name

String"Z"

description

LocalizedTextDescription of Z

dataType

NodeId"i=6" [Int32]

valueRank

Int32-1 (Scalar)

arrayDimensions

UInt32[]null

maxStringLength

UInt320

isOptional

Booleanfalse
fields [3]StructureField

name

String"W"

description

LocalizedTextDescription of W

dataType

NodeId"i=5" [UInt16]

valueRank

Int321 (OneDimension)

arrayDimensions

UInt32[]{ 10 }

maxStringLength

UInt320

isOptional

Booleanfalse
fields [4]StructureField

name

String"M"

description

LocalizedTextDescription of M

dataType

NodeId"i=3" [Byte]

valueRank

Int323

arrayDimensions

UInt32[]{ 2, 3, 4 }

maxStringLength

UInt320

isOptional

Booleanfalse

The Value of the DataTypeDefinition Attribute for a DataType Node describing Type2 is shown in Table 31.

Table 31 – DataTypeDefinition for "Type2" from Sample
Name Type Description
defaultEncodingIdNodeIdNodeId of the "Type2_Encoding_DefaultBinary" Node.
baseDataTypeNodeId"i=22" [Structure]
structureTypeStructureTypeStructure_0 [Structure without optional fields]
fields [0]StructureField

name

String"A"

description

LocalizedTextDescription of A

dataType

NodeId"i=6" [Int32]

valueRank

Int32-1 (Scalar)

arrayDimensions

UInt32[]null

maxStringLength

UInt320

isOptional

Booleanfalse
fields [1]StructureField

name

String"B"

description

LocalizedTextDescription of B

dataType

NodeId"i=6" [Int32]

valueRank

Int32-1 (Scalar)

arrayDimensions

UInt32[]null

maxStringLength

UInt320

isOptional

Booleanfalse