Fast Table Serialization Library for Pure Luau/Lua 5.1+
LuaEncode is a simple library for serialization of Lua tables and data structures, with support for Roblox's engine data types. LuaEncode natively supports both Luau and Lua 5.1+
- Serialization and output of basic types
number
,string
,table
,boolean
, andnil
for key/values - Pretty-printing and custom indentation configuration
- Compatible with custom Roblox data types (e.g.
Instance
,UDim2
,Vector3
,DateTime
, etc..) - See Roblox Engine Data Type Coverage for more info - Securely iterates and reads values, with potentially untrusted inputs in-mind
- Manual cycle inserts in serialized codegen with
InsertCycles
- Raw key/value input with
FunctionsReturnRaw
-
If you're using Rojo and Wally, you can import the package in your
wally.toml
:LuaEncode = "regginator/luaencode@1.4.3"
-
You can also grab the LuaEncode module from the Roblox Marketplace directly: https://roblox.com/library/12791121865
local LuaEncode = require("path/to/LuaEncode")
local Table = {
foo = "bar",
baz = {
1,
2,
3,
[5] = 5,
},
qux = function()
return "\"hi!\"" -- With `FunctionsReturnRaw` set as true in options, this will output as 'qux = "hi!"'
end,
}
local Encoded = LuaEncode(Table, {
Prettify = true, -- false by default (when this is true, IndentCount is also 4!)
FunctionsReturnRaw = true, -- false by default
})
print(Encoded)
Output
{
qux = "hi!",
baz = {
1,
2,
3,
[5] = 5
},
foo = "bar"
}
LuaEncode(inputTable: {[any]: any}, options: {[string]: any}): string
Argument | Type | Description |
---|---|---|
Prettify | <boolean:false> |
Whether or not the output should use pretty printing |
IndentCount | <number:0> |
The amount of "spaces" that should be indented per entry (Note: If Prettify is set to true and this is unspecified, it'll be set to 4 automatically) |
InsertCycles | <boolean:false> |
If there are cyclic references in your table, the output will be wrapped in an anonymous function that manually sets paths to those references. NOTE: If a key in the index path to the cycle is a reference type (e.g. table , function ), the codegen can't externally set that path, and will be ignored. |
OutputWarnings | <boolean:true> |
If "warnings" should be placed to the output (as comments); it's recommended to keep this enabled, however it can be disabled at ease |
FunctionsReturnRaw | <boolean:false> |
If functions in said table return back a "raw" value to place in the output as the key/value |
UseInstancePaths | <boolean:true> |
If Instance reference objects should return their Lua-accessable path for encoding. If the instance is parented under nil or isn't under game /workspace , it'll always fall back to Instance.new(ClassName) as before |
SerializeMathHuge | <boolean:true> |
If numbers calculated as "infinite" (or negative-inf) numbers should be serialized with "math.huge". (uses the math import, as opposed to just a direct data type) If false, "1/0 " or "-1/0 " will be serialized, which is supported on all target versions |
(See AllRobloxTypes.server.lua for example input and (the current expected) output of ALL Roblox DataTypes.)
β Implemented | β Partially Implemented | β Unimplemented | β Never
DataType | Serialized As | Implemented |
---|---|---|
Axes | Axes.new() |
β |
BrickColor | BrickColor.new() |
β |
CFrame | CFrame.new() |
β |
CatalogSearchParams | CatalogSearchParams.new() |
β |
Color3 | Color3.new() |
β |
ColorSequence | ColorSequence.new(<ColorSequenceKeypoints>) |
β |
ColorSequenceKeypoint | ColorSequenceKeypoint.new() |
β |
DateTime | DateTime.fromUnixTimestamp() |
β |
DockWidgetPluginGuiInfo | DockWidgetPluginGuiInfo.new() |
β |
Enum | Enum.<EnumType> |
β |
EnumItem | Enum.<EnumType>.<EnumItem> |
β |
Enums | Enum |
β |
Faces | Faces.new() |
β |
FloatCurveKey | FloatCurveKey.new() |
β |
Font | Font.new() |
β |
Instance | Instance.new() |
β |
NumberRange | NumberRange.new() |
β |
NumberSequence | NumberSequence.new(<NumberSequenceKeypoints>) |
β |
NumberSequenceKeypoint | NumberSequenceKeypoint.new() |
β |
OverlapParams | OverlapParams.new() |
β |
PathWaypoint | PathWaypoint.new() |
β |
PhysicalProperties | PhysicalProperties.new() |
β |
RBXScriptConnection | N/A |
β |
RBXScriptSignal | N/A |
β |
Random | Random.new() |
β |
Ray | Ray.new() |
β |
RaycastParams | RaycastParams.new() |
β |
RaycastResult | N/A |
β |
Rect | Rect.new() |
β |
Region3 | Region3.new() |
β |
Region3int16 | Region3int16.new() |
β |
TweenInfo | TweenInfo.new() |
β |
RotationCurveKey | RotationCurveKey.new() |
β |
UDim | UDim.new() |
β |
UDim2 | UDim2.new() |
β |
Vector2 | Vector2.new() |
β |
Vector2int16 | Vector2int16.new() |
β |
Vector3 | Vector3.new() |
β |
Vector3int16 | Vector3int16.new() |
β |
(Official Roblox DataType documentation here)
See file: LICENSE
MIT License
Copyright (c) 2022-2024 Reggie <reggie@latte.to>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.