Skip to main content

serializerUtil

A very simple utility for serializing and deserializing data types.

This utility supports the following data types:
  • CFrame
  • Vector3
  • Vector2
  • BrickColor
  • Color3
  • Enum
  • EnumItem
  • Axes
  • CatalogSearchParams
  • ColorSequence
  • ColorSequenceKeypoint
  • NumberSequence
  • NumberSequenceKeypoint
  • Faces
  • Axes
  • NumberRange
  • PathWaypoint
  • Region3
  • Region3init16
  • TweenInfo
  • UDim
  • UDim2
  • Vector3int16
  • Vector2int16
  • Region3int16
  • Font
  • Rect
  • DateTime
  • PhysicalProperties

Types

ClassDataType

type ClassDataType = {
valueTypeany,
serializer(
valueany,
intermediateSerializedData{}
) → (),
deserializer(serializedData{}) → (),
}

Properties

customValueType

serializerUtil.customValueType: "serDesCustomValueType"

A constant to be used as a key in tables, to represent some type which this utility can easily know about when serializing and deserializing values.

local spmeTable = {
	-- Allow the serializer utility module to know the "type" of this table
	[serializerUtil.customValueType] = "uniqueType"
}

Functions

serialize

serializerUtil.serialize(valueany) → ()

Serializes the given vale, if it can be serialized. If it can't be serialized, then the value it self is returned.

local serializedValue = serializerUtil.serializer(150)
print(serializedValue) --> 150 (can't serialize a number!)
local serializedValue = serializerUtil.serializer(CFrame.new(5,5,5))
print(serializedValue) --> {...} (serialized data)

deserialize

serializerUtil.deserialize(serializedDataany) → ()

Deserializes the given serialized value, if it can be deserialized. If it can't be deserialized, then the value it self is returned.

local serializedValue = serializerUtil.serializer(CFrame.new(5,5,5))
print(serializerUtil.deserialize(serializedValue)) --> CFrame (5,5,5) 
print(serializerUtil.deserialize(15)) --> 15 (Can't deserialize a non-serialized value)

addClassDataType

serializerUtil.addClassDataType(classDataTypeClassDataType) → ()

Adds a class data type to the serializer. A class data type (in the context of this utility) is simply an object which you can use to add your own custom serializers and deserializers.

-- In this project X, we have a funny which we would like to serialize
-- and deserialize according to how our game is structured;
local someUniqueTable = {
	[serializerUtil.customValueType] = "TheUniqueTable"
}

serializerUtil.addClassDataType({
	valueType = "TheUniqueTable",
	serializer = {
		run = function(uniqueTable, intermediateSerializedData)
			intermediateSerializedData.component = {1, 2, 3, 4, 5, 6}
		end,
	},
	deserializer = {
		run = function(serializedUniqueTable)
			return serializedUniqueTable.component
		end,
	},
})

local serializedTable = serializerUtil.serialize(someUniqueTable)
print(serializerUtil.deserialize(serializedTable)) --> {1, 2, 3, 4, 5, 6}
Override default data types

Currently this utility supports a lot of data types to serialize and serialize - however you can override them too with your own serializers and deserializers for them, for e.g:

-- Let's just override the serializer and deserializer for the `Vector3` data type
-- in favour of our own:
serializerUtil.addClassDataType({
	valueType = "Vector3",
	serializer = {
		run = function(vector3, intermediateSerializedData)
			intermediateSerializedData.components = {vector3.X, vector3.Y, vector3.Z}
		end,
	},
	deserializer = {
		run = function(serializedVector3)
			return Vector3.new(table.unpack(serializedVector3.components))
		end,
	},
})

This gives you flexibility to append your own serializer and deserializer for specific data types as per your game's need!

addClassDataTypes

serializerUtil.addClassDataTypes(classDataTypes{ClassDataType}) → ()

Shorthand for adding multiple class data types in 1 go. serializerUtil.addClass is called for every class data type in classDataTypes.

local classDataTypes = {
	{
		valueType = "Vector3",
		serializer = {
			run = function(vector3, intermediateSerializedData)
				intermediateSerializedData.components = {vector3.X, vector3.Y, vector.Z}
			end,
		},
		deserializer = {
			run = function(serializedVector3)
				return Vector3.new(table.unpack(serializedVector3.components))
			end,
		},
	},

	{
		valueType = "Vector2",
		serializer = {
			run = function(vector2, intermediateSerializedData)
				intermediateSerializedData.components = {vector2.X, vector2.Y}
			end,
		},
		deserializer = {
			run = function(serializedVector2)
				return Vector2.new(table.unpack(serializedVector2.components))
			end,
		},
	}
}

serializerUtil.addClassDataTypes(classDataTypes)

serializeTableDeep

serializerUtil.serializeTableDeep(deserializedTable{[any]any}) → ()

Deep serializes all keys and values in the given table via serializerUtil.serialize.

local tabl = {bo = Enum.Keycode.A}
local serializedTable = serializerUtil.serializeTableDeep(tabl)
print(serializedTable) --> {bo: {...}} (serialized)

deserializeTableDeep

serializerUtil.deserializeTableDeep(serializedTable{[any]any}) → ()

Deep - deserializes all keys and values in the given table via serializerUtil.deserialize.

local tabl = {bo = Enum.Keycode.A}
local serializedTable = serializerUtil.serializeTableDeep(tabl)
print(serializerUtil.deserializeTableDeep(serializedTable)) --> {bo: Enum.Keycode.A} (deserialized)
Show raw api
{
    "functions": [
        {
            "name": "serialize",
            "desc": "Serializes the given vale, if it can be serialized. If it can't be serialized, then the value it self is returned.\n\n```lua\nlocal serializedValue = serializerUtil.serializer(150)\nprint(serializedValue) --> 150 (can't serialize a number!)\nlocal serializedValue = serializerUtil.serializer(CFrame.new(5,5,5))\nprint(serializedValue) --> {...} (serialized data)\n```",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 96,
                "path": "src/serializerUtil/init.luau"
            }
        },
        {
            "name": "deserialize",
            "desc": "Deserializes the given serialized value, if it can be deserialized. If it can't be deserialized, then the value\nit self is returned.\n\n```lua\nlocal serializedValue = serializerUtil.serializer(CFrame.new(5,5,5))\nprint(serializerUtil.deserialize(serializedValue)) --> CFrame (5,5,5) \nprint(serializerUtil.deserialize(15)) --> 15 (Can't deserialize a non-serialized value)\n```",
            "params": [
                {
                    "name": "serializedData",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 123,
                "path": "src/serializerUtil/init.luau"
            }
        },
        {
            "name": "addClassDataType",
            "desc": "Adds a class data type to the serializer. A class data type (in the context of this utility) is simply\nan object which you can use to add your own custom serializers and deserializers.\n\n```lua\n-- In this project X, we have a funny which we would like to serialize\n-- and deserialize according to how our game is structured;\nlocal someUniqueTable = {\n\t[serializerUtil.customValueType] = \"TheUniqueTable\"\n}\n\nserializerUtil.addClassDataType({\n\tvalueType = \"TheUniqueTable\",\n\tserializer = {\n\t\trun = function(uniqueTable, intermediateSerializedData)\n\t\t\tintermediateSerializedData.component = {1, 2, 3, 4, 5, 6}\n\t\tend,\n\t},\n\tdeserializer = {\n\t\trun = function(serializedUniqueTable)\n\t\t\treturn serializedUniqueTable.component\n\t\tend,\n\t},\n})\n\nlocal serializedTable = serializerUtil.serialize(someUniqueTable)\nprint(serializerUtil.deserialize(serializedTable)) --> {1, 2, 3, 4, 5, 6}\n```\n\n:::tip Override default data types\nCurrently this utility supports a lot of data types to serialize and serialize - however\nyou can override them too with your own serializers and deserializers for them, for e.g:\n\n```lua\n-- Let's just override the serializer and deserializer for the `Vector3` data type\n-- in favour of our own:\nserializerUtil.addClassDataType({\n\tvalueType = \"Vector3\",\n\tserializer = {\n\t\trun = function(vector3, intermediateSerializedData)\n\t\t\tintermediateSerializedData.components = {vector3.X, vector3.Y, vector3.Z}\n\t\tend,\n\t},\n\tdeserializer = {\n\t\trun = function(serializedVector3)\n\t\t\treturn Vector3.new(table.unpack(serializedVector3.components))\n\t\tend,\n\t},\n})\n```\n\nThis gives you flexibility to append your own serializer and deserializer for specific data types\nas per your game's need!\n:::",
            "params": [
                {
                    "name": "classDataType",
                    "desc": "",
                    "lua_type": "ClassDataType"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 195,
                "path": "src/serializerUtil/init.luau"
            }
        },
        {
            "name": "addClassDataTypes",
            "desc": "Shorthand for adding multiple class data types in 1 go. [serializerUtil.addClass] is called\nfor every class data type in `classDataTypes`.\n\n```lua\nlocal classDataTypes = {\n\t{\n\t\tvalueType = \"Vector3\",\n\t\tserializer = {\n\t\t\trun = function(vector3, intermediateSerializedData)\n\t\t\t\tintermediateSerializedData.components = {vector3.X, vector3.Y, vector.Z}\n\t\t\tend,\n\t\t},\n\t\tdeserializer = {\n\t\t\trun = function(serializedVector3)\n\t\t\t\treturn Vector3.new(table.unpack(serializedVector3.components))\n\t\t\tend,\n\t\t},\n\t},\n\n\t{\n\t\tvalueType = \"Vector2\",\n\t\tserializer = {\n\t\t\trun = function(vector2, intermediateSerializedData)\n\t\t\t\tintermediateSerializedData.components = {vector2.X, vector2.Y}\n\t\t\tend,\n\t\t},\n\t\tdeserializer = {\n\t\t\trun = function(serializedVector2)\n\t\t\t\treturn Vector2.new(table.unpack(serializedVector2.components))\n\t\t\tend,\n\t\t},\n\t}\n}\n\nserializerUtil.addClassDataTypes(classDataTypes)\n```",
            "params": [
                {
                    "name": "classDataTypes",
                    "desc": "",
                    "lua_type": "{ ClassDataType }"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 245,
                "path": "src/serializerUtil/init.luau"
            }
        },
        {
            "name": "serializeTableDeep",
            "desc": "Deep serializes all keys and values in the given table via [serializerUtil.serialize].\n\n```lua\nlocal tabl = {bo = Enum.Keycode.A}\nlocal serializedTable = serializerUtil.serializeTableDeep(tabl)\nprint(serializedTable) --> {bo: {...}} (serialized)\n```",
            "params": [
                {
                    "name": "deserializedTable",
                    "desc": "",
                    "lua_type": "{ [any]: any }"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 261,
                "path": "src/serializerUtil/init.luau"
            }
        },
        {
            "name": "deserializeTableDeep",
            "desc": "Deep - deserializes all keys and values in the given table via [serializerUtil.deserialize].\n\n```lua\nlocal tabl = {bo = Enum.Keycode.A}\nlocal serializedTable = serializerUtil.serializeTableDeep(tabl)\nprint(serializerUtil.deserializeTableDeep(serializedTable)) --> {bo: Enum.Keycode.A} (deserialized)\n```",
            "params": [
                {
                    "name": "serializedTable",
                    "desc": "",
                    "lua_type": "{ [any]: any }"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 297,
                "path": "src/serializerUtil/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "customValueType",
            "desc": " \n\nA constant to be used as a key in tables, to represent some type which this utility\ncan easily know about when serializing and deserializing values.\n\n```lua\nlocal spmeTable = {\n\t-- Allow the serializer utility module to know the \"type\" of this table\n\t[serializerUtil.customValueType] = \"uniqueType\"\n}\n```",
            "lua_type": "\"serDesCustomValueType\"",
            "source": {
                "line": 58,
                "path": "src/serializerUtil/init.luau"
            }
        }
    ],
    "types": [
        {
            "name": "ClassDataType",
            "desc": " ",
            "lua_type": "{valueType: any, serializer: (value: any, intermediateSerializedData: {}) -> (), deserializer: (serializedData: {}) -> (),}",
            "source": {
                "line": 43,
                "path": "src/serializerUtil/init.luau"
            }
        }
    ],
    "name": "serializerUtil",
    "desc": "A very simple utility for serializing and deserializing data types.\n\n:::note This utility supports the following data types: \n- CFrame\n- Vector3\n- Vector2\n- BrickColor\n- Color3\n- Enum\n- EnumItem\n- Axes\n- CatalogSearchParams\n- ColorSequence\n- ColorSequenceKeypoint\n- NumberSequence\n- NumberSequenceKeypoint\n- Faces\n- Axes\n- NumberRange\n- PathWaypoint\n- Region3\n- Region3init16\n- TweenInfo\n- UDim\n- UDim2\n- Vector3int16\n- Vector2int16\n- Region3int16\n- Font\n- Rect\n- DateTime\n- PhysicalProperties\n:::",
    "source": {
        "line": 38,
        "path": "src/serializerUtil/init.luau"
    }
}