SFSObject

Specialized object used by SmartFoxServer for client-server data transfer.


This class can be thought of as a specialized Map that can contain any type of data.

The advantage of using the SFSObject class (for example when sending an ExtensionRequest) is that you can fine tune the way your datais transmitted over the network. For instance, a number like 100 can be transmitted as a normal integer (which takes 32 bits), but also a short (16 bit) or even a byte (8 bit).

SFSObject supports many primitive data types and their relative arrays (see the SFSDataType class) and nested objects.

Constructor

new SFSObject()

Creates a new instance of the SFSObject class.

Methods

containsKey(key) → {boolean}

Indicates whether the object contains a mapping for the specified key.
Parameters:
NameTypeDescription
keystringThe key whose presence in the object should be checked.
Returns:
true if the object contains a mapping for the specified key.
Type: 
boolean

getBigInt(key) → {bigint}

Returns the element corresponding to the specified key as a BigInt.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
bigint

getBigIntArray(key) → {Array.<bigint>}

Returns the element corresponding to the specified key as an array of BigInts.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<bigint>

getBool(key) → {boolean}

Returns the element corresponding to the specified key as a boolean.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
boolean

getBoolArray(key) → {Array.<boolean>}

Returns the element corresponding to the specified key as an array of booleans.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<boolean>

getDump(formatopt) → {string}

Provides a formatted string representing the object.

The returned string can be logged or printed in the console for debugging purposes.

Parameters:
NameTypeAttributesDefaultDescription
formatboolean<optional>
trueIf true, the output is formatted in a human-readable way.
Returns:
The string representation of the object.
Type: 
string

getHexDump() → {string}

Provides a detailed hexadecimal representation of the object.

The returned string can be logged or traced in the console for debugging purposes.

Returns:
The hexadecimal string representation of the object.
Type: 
string

getKeys() → {Array.<string>}

Retrieves an array of all the keys contained in the object.
Returns:
The list of all the keys in the object.
Type: 
Array.<string>

getNumber(key) → {number}

Returns the element corresponding to the specified key as a number.

IMPORTANT: in case the original value is of type long, it will be cast to number, with possible loss of precision. Use SFSObject#getBigInt method to get the precise value.

Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
number

getNumberArray(key) → {Array.<number>}

Returns the element corresponding to the specified key as an array of numbers.

IMPORTANT: in case the original values in the array are of type long, they will be cast to number, with possible loss of precision. Use SFSObject#getBigIntArray method to get the precise values.

Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<number>

getSFSArray(key) → {SFSArray}

Returns the element corresponding to the specified key as an SFSArray.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
SFSArray

getSFSObject(key) → {SFSObject}

Returns the element corresponding to the specified key as an SFSObject.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
SFSObject

getString(key) → {string}

Returns the element corresponding to the specified key as an UTF-8 string.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
string

getStringArray(key) → {Array.<string>}

Returns the element corresponding to the specified key as an array of UTF-8 strings.
Parameters:
NameTypeDescription
keystringThe key whose associated value is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<string>

getVector2(key) → {SFSVector2}

Returns the element corresponding to the specified key as a SFSVector2.
Parameters:
NameTypeDescription
keystringThe key whose associated object is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
SFSVector2

getVector2Array(key) → {Array.<SFSVector2>}

Returns the element corresponding to the specified key as an array of SFSVector2.
Parameters:
NameTypeDescription
keystringThe key whose associated object is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<SFSVector2>

getVector3(key) → {SFSVector3}

Returns the element corresponding to the specified key as a SFSVector3.
Parameters:
NameTypeDescription
keystringThe key whose associated object is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
SFSVector3

getVector3Array(key) → {Array.<SFSVector3>}

Returns the element corresponding to the specified key as an array of SFSVector3.
Parameters:
NameTypeDescription
keystringThe key whose associated object is to be returned.
Returns:
The element of the object associated with the specified key; null if a mapping for the passed key doesn't exist.
Type: 
Array.<SFSVector3>

isNull(key) → {boolean}

Indicates whether the value mapped by the specified key is null.
Parameters:
NameTypeDescription
keystringThe key to be checked.
Returns:
true if the value mapped by the passed key is null or the mapping doesn't exist for that key.
Type: 
boolean

putBool(key, value)

Associates a boolean value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuebooleanThe value to be associated with the specified key.

putBoolArray(key, array)

Associates an array of boolean values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<boolean>The array of booleans to be associated with the specified key.

putByte(key, value)

Associates a byte (8 bits) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumberThe value to be associated with the specified key.

putByteArray(key, array)

Associates a byte array with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayUint8ArrayThe byte array to be associated with the specified key.

putDouble(key, value)

Associates a double precision number (64 bits) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumberThe value to be associated with the specified key.

putDoubleArray(key, array)

Associates an array of double precision number values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<number>The array of doubles to be associated with the specified key.

putFloat(key, value)

Associates a floating point number (32 bits) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumberThe value to be associated with the specified key.

putFloatArray(key, array)

Associates an array of floating point number values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<number>The array of floats to be associated with the specified key.

putInt(key, value)

Associates an integer (32 bits) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumberThe value to be associated with the specified key.

putIntArray(key, array)

Associates an array of integer values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<number>The array of integers to be associated with the specified key.

putLong(key, value)

Associates a long integer (64 bits) value with the specified key in the object.

IMPORTANT: in this API, long integers are represented by BigInt values, but SmartFoxServer supports long integers up to 64-bit signed. Make sure to take this limitation (and others, see BigInt documentation) into account when dealing with long integers.

Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumber | bigintThe value to be associated with the specified key.

putLongArray(key, array)

Associates an array of long integer values with the specified key in the object.

IMPORTANT: in this API, long integers are represented by BigInt values, but SmartFoxServer supports long integers up to 64-bit signed. Make sure to take this limitation (and others, see BigInt documentation) into account when dealing with long integers.

Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<number> | Array.<bigint>The array of longs to be associated with the specified key.

putNull(key)

Associates a null value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the null value.

putSFSArray(key, value)

Associates an SFSArray with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified object.
valueSFSArrayThe object to be associated with the specified key.

putSFSObject(key, value)

Associates an SFSObject with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified object.
valueSFSObjectThe object to be associated with the specified key.

putShort(key, value)

Associates a short integer (16 bits) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuenumberThe value to be associated with the specified key.

putShortArray(key, array)

Associates an array of short integer values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<number>The array of shorts to be associated with the specified key.

putShortString(key, value)

Associates a UTF-8 string (with max length of 255 bytes) value with the specified key in the object.

Use this method if you're sure your strings are short enough and you want to save a few bytes during the encoding.

Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuestringThe value to be associated with the specified key.

putShortStringArray(key, array)

Associates an array of short UTF-8 string values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<string>The array of short UTF-8 strings to be associated with the specified key.

putString(key, value)

Associates a UTF-8 string (with max length of 32767 bytes) value with the specified key in the object.

This is the default choice for strings and uses 2 bytes to define the string length.

Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuestringThe value to be associated with the specified key.

putStringArray(key, array)

Associates an array of UTF-8 string values with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
arrayArray.<string>The array of UTF-8 strings to be associated with the specified key.

putText(key, value)

Associates a UTF-8 string (with max length of 2 GBytes) value with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified value.
valuestringThe value to be associated with the specified key.

putVector2(key, value)

Associates a SFSVector2 object with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified object.
valueSFSVector2The object to be associated with the specified key.

putVector2Array(key, value)

Associates an array of SFSVector2 objects with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
valueArray.<SFSVector2>The array of SFSVector2 objects to be associated with the specified key.

putVector3(key, value)

Associates a SFSVector3 object with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified object.
valueSFSVector3The object to be associated with the specified key.

putVector3Array(key, value)

Associates an array of SFSVector3 objects with the specified key in the object.
Parameters:
NameTypeDescription
keystringThe key to be associated with the specified array.
valueArray.<SFSVector3>The array of SFSVector3 objects to be associated with the specified key.

removeElement(key)

Removes the element corresponding to the passed key from the object.
Parameters:
NameTypeDescription
keystringThe key of the element to be removed.

size() → {number}

Number of elements mapped in the object.
Returns:
The number of elements in the object.
Type: 
number

toBinary() → {Uint8Array}

Provides the binary form of the object.
Returns:
The binary data representing the object.
Type: 
Uint8Array

(static) newFromBinaryData(binData) → {SFSObject}

Returns a new SFSObject instance.

This method is a static alternative to the standard class constructor that builds an SFSObject from a valid SFSObject binary representation.

Parameters:
NameTypeDescription
binDataUint8ArrayThe byte array representation of the SFSObject.
Returns:
A new SFSObject instance.
Type: 
SFSObject