Designing Custom Encoders

Optional

The Optional property specifies how delimiters for optional nodes are to be handled when the nodes are absent from the input instance or when their fields are empty.

Table 11 Optional Mode Options

Option 

Rule 

never 

If the node is absent, the delimiter is not allowed in either input or output. 

allow 

If the node is absent, the delimiter is allowed in input but will not be emitted in output. 

cheer 

If the node is absent, the delimiter is allowed in input and will also be emitted in output. 

force 

If the node is absent, the delimiter must appear in input and will be emitted in output. 


Note –

Only this option allows trailing delimiters for a sequence of absent optional nodes.


As illustrative examples, consider the tree structures shown in the following figure and table, where the node a has a caret (^) as its delimiter, and the child nodes b, c, and d all have asterisks (*) as their delimiters.

Figure 10 Optional Mode Property (Example 1)

Diagram of tree structure as described in content.

Option 

Input 

Output 

never 

b*d^

b*d^

allow 

b**d^

b*d^

cheer 

b**d^

b**d^

force 

b**d^

b**d^

Figure 11 Optional Mode Property (Example 2)

Diagram of tree structure as described in content.

Option 

Input 

Output 

never 

b^

b^

allow 

b^, b*^, or b**^

b^

cheer 

b^, b*^, or b**^

b**^

force 

b**^

b**^