This method is used to create composite message types from other registered types, such as JSON, XML, or the ones added by a user prior to calling this method.
// Register the json-xml-json-binary composite message type with the type helper
TypeHelper.helper(
'compositejxjb',
TypeHelper.compositeHelper('json', 'xml', 'json', 'binary')
);
A created ITypeHelper-compatible object.
This method provides a helper for parsing messages of different types. If a helper was provided by a user, registers it for future use.
// access/store the existing helper
const oldHelper = TypeHelper.helper('json');
// create a JSON type helper that does not parse JSON data into native JS objects keeping it as a string
const jsonHelper = {
serialize: data => [data],
deserialize: data => JSON.stringify(data)
};
// override the existing/default type helper
TypeHelper.helper('json', jsonHelper);
The type of message to handle.
A helper object with predefined functions that can be provided for custom parsing/handling of a message type.
The helper object assigned/registered.
This is a helper class that is used to register custom message types in order for the client to support them. By default, the Client supports the following message types:
Composite message types can be created using TypeHelper.compositeHelper method.
Message type helpers can be accessed and registered via TypeHelper.helper static method.
Create a custom helper for XML messages:
const xmlTypeHelper = { serialize: data => [new XMLSerializer().serializeToString(data)], deserialize: data => { if (data.constructor === String) { return new DOMParser().parseFromString(data); } else { // Binary buffer, need to decode utf8 return new DOMParser().parseFromString( decodeURIComponent(escape(Uint8ToString(data))) ); } } }; // Register the above XML custom helper for parsing all XML messages automatically TypeHelper.helper('xml', xmlTypeHelper);
In case the custom parsing behavior is expected, it is possible to override the default type helper:
// create a JSON type helper that does not parse JSON data into native JS objects keeping it as a string const jsonHelper = { serialize: data => [data], deserialize: data => JSON.stringify(data) }; // override the default type helper TypeHelper.helper('json', jsonHelper);
Register the
json-json-json-binary
composite message type with the type helper:TypeHelper.helper( 'compositejjjb', TypeHelper.compositeHelper('json', 'json', 'json', 'binary') );
Set the custom delimiter for NVFIX format:
TypeHelper.helper('nvfix').delimiter('%01');
Register the custom helper for NVFIX format:
const nvfixTypeHelper = { serialize: data => { // already formatted fix/nvfix string if (typeof data === 'string') { return [data]; } // otherwise, we assume it's an object with keys and values return [ Object.keys(data).map(key => key + '=' + data[key]).join('\x01') + '\x01' ]; }, deserialize: data => { const parsedData = {}; String.fromCharCode.apply(null, new Int8Array(data)) .split('\x01') .slice(0, -1) .map(function(keyValue) { const keyValueTuple = keyValue.split('='); const key = keyValueTuple[0]; // no '=' inside of the value if (keyValueTuple.length === 2) { parsedData[key] = keyValueTuple[1]; } else { parsedData[key] = keyValue.slice(key.length + 1); } }); return parsedData; } }; // Register the above NVFIX custom helper for parsing all NVFIX messages automatically TypeHelper.helper('nvfix', nvfixTypeHelper);