JSONSelector

Identifies the variant member of a JSONOneOf record to use during serialization or deserialization.

Syntax

JSONSelector is a mandatory attribute used with JSONOneOf.

Important:

This serializer-specific attribute is supported only by json.Serializer and does not work with util.JSON.

Usage

Every JSONOneOf record must include exactly one field marked with JSONSelector. That field must be SMALLINT or INTEGER. You set its value to select the variant to use: the value is a 1-based positional index across the whole record, counting the JSONSelector field itself as position 1. When the JSONSelector field is the first member of the record (as in the following example), the first variant is therefore index 2; setting the value to 1 is invalid and causes error -15807.

JSONSelector represents the oneOf keyword in the OpenAPI JSON schema specification.

Example using JSONSelector and JSONOneOf

In this example, the JSONSelector attribute is set on the _selector member in the input record of the CreateAccount function. To select a variant, set _selector to the positional index of the variant field: 2 for id, 3 for name.

For example, in a request from your client application, you might have a statement like this: LET in._selector=2.

IMPORT COM

PUBLIC TYPE accountType RECORD
    id INTEGER,
    name STRING,
    date DATETIME YEAR TO SECOND,
    age INTEGER,
    gender BOOLEAN
END RECORD

PRIVATE DEFINE accounts DYNAMIC ARRAY OF accountType

PUBLIC DEFINE accountError RECORD ATTRIBUTE(WSError = 'account service error')
    id INTEGER,
    msg STRING
END RECORD

TYPE createAccountType RECORD ATTRIBUTE(JSONOneOf)
    _selector INTEGER ATTRIBUTE(JSONSelector),
    id INTEGER,
    name STRING
END RECORD

FUNCTION CreateAccount(
    in createAccountType)
    ATTRIBUTE(WSPost, WSPath = "/createaccount")
    RETURNS(createAccountType)

    DEFINE out createAccountType
    DEFINE idx INTEGER
    LET out = in

    CASE
        WHEN in._selector = 2
            LET idx = accounts.search("id", in.id)
            IF idx > 0 THEN
                # raise RESTError with accountError
            ELSE
              CALL accounts.appendElement()
              LET accounts[accounts.getLength()].id = in.id
            END IF
        WHEN in._selector = 3
            LET idx = accounts.search("name", in.name)
            # ... function code ... 
    END CASE

    RETURN out
END FUNCTION