5.4.7 Structures with optional fields

Structures with optional fields shall be encoded as JSON objects as shown in Table 39.

Table 39 – JSON Object Definition for a Structures with Optional Fields

Name Description
EncodingMask    A bit mask indicating what fields are encoded in the structure (see 5.2.7)    This mask is encoded as a JSON number.   The bits are sequentially assigned to optional fields in the order that they are defined.This field is not encoded in the non-reversible form.
<FieldName>    The field in structure encoded according to the rules defined for their DataType.One entry may exist for each mandatory field and each optional field that is present.

Fields which are NULL or have a default value shall be encoded using the rules shown in Table 40.

Table 40 – JSON Encoding Rules for Structures with Optional Fields

Field Value Field Type Reversible Non-Reversible
NULL Mandatory Omitted JSON null
Default Value Mandatory Omitted Default Value
NULL Optional (Present) Omitted JSON null
Default Value Optional (Present) Omitted Default Value
NULL Optional (Omitted) Omitted Omitted
Default Value Optional (Omitted) Omitted Omitted

If a Structure with optional fields is subtyped, the subtypes extend the EncodingMask defined for the parent.

The following is an example of a structure with optional fields using C++ syntax:


struct TypeA
{
Int32 X;
Int32* O1;
SByte Y;
Int32* O2;
};



O1 and O2 are optional fields where a NULL indicates that the field is not present.

Assume that O1 is not specified and the value of O2 is 0.

The reversible encoding would be:

{ "EncodingMask": 2 "X": 1, "Y": 2 }


Where decoders would assign the default value of 0 to O2 since the mask bit is set even though the field was omitted (this is the behaviour defined for the Int32 DataType). Decoders would mark O1 as ‘not specified’.

The non-reversible encoding would be:

{ "X": 1, "Y": 2, "O2": 0 }