Unions shall be encoded as JSON objects as shown in Table H.8 for the ReversibleEncoding.
Note that JSON objects are unordered sets of name-value pairs. The order specified by the DataTypeDefinition is not preserved when a Union is serialized in JSON. The SwitchField may not appear as the first field.
Table H.8 – JSON Object Definition for a Union
Name |
Description |
SwitchField |
The identifier for the field in the Union which is encoded as a JSON number. The valid values for this field follow the conventions defined in 5.2.8. If the SwitchField value is its DefaultValue of 0, then the SwitchField and the Value field are not present. |
Value |
The value of the field encoded using the rules that apply to the data type. |
For the NonReversibleEncoding, Union values are encoded using the rule for the current value. If the SwitchField is 0 the Union is encoded as a JSON null value.
For example, instances of the union:
struct Union1
{
Byte Selector;
{
Int32 A;
Double B;
Char* C;
}
Value;
};
would be represented in the ReversibleEncoding as:
{ "SwitchField":2, "Value":3.1415 }
In the NonReversibleEncoding, it is represented as:
3.1415
The CompactEncoding is the same as the ReversibleEncoding. The VerboseEncoding always has a JSON object with a name and value for the active field, however, the NonReversibleEncoding only encodes the value.
___________