Adobe Campaign JavaScript SDK

Type conversion (XtkCaster)

Campaign data types have some specifities. For instance number can never be null, and Campaign uses 0 instead. In addition, XML to JSON is not always accurate and the conversion may return strings instead of booleans, objects instead of arrays, etc.

The XtkCaster class is here to help and can be used to coerce an value to any type. For instance if you know that a value is a boolean, but are not sure if it was actually returned as a boolean, you can use the XtkCaster to ensure this. The XtkCaster.asBoolean(value) will ensure this.

You get a static XtkCaster object like this

const XtkCaster = sdk.XtkCaster;

or directly from the client for convenience

const XtkCaster = client.XtkCaster;

To convert a Campaign value into a given type, use one of the following.

stringValue = XtkCaster.asString(anyValue);
booleanValue = XtkCaster.asBoolean(anyValue);
byteValue = XtkCaster.asByte(anyValue);
shortValue = XtkCaster.asShort(anyValue);
int32Value = XtkCaster.asLong(anyValue);
numberValue = XtkCaster.asNumber(anyValue);
timestampValue = XtkCaster.asTimestamp(anyValue);
dateValue = XtkCaster.asDate(anyValue);

More dynamic conversions can be achieved using the as function. See the types table above for details.

stringValue = XtkCaster.as(anyValue, 6);

In addition, the following helpers are available

Arrays

Arrays can be tricky to handle when converted from XML: an empty array will be converted to a null or undefined values, an array containing exactly one element may be converted into the element itself. The XtkCaster.asArray will ensure that it's parameter will be converted to an array, possibly empty.

expect(XtkCaster.asArray(null)).toStrictEqual([]);
expect(XtkCaster.asArray("Hello")).toStrictEqual(["Hello"]);
expect(XtkCaster.asArray(["Hello", "World""])).toStrictEqual(["Hello", "World""]);