Skip to content

Optimal Table Serialization for Native Luau/Lua 5.1+

License

Notifications You must be signed in to change notification settings

regginator/LuaEncode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LuaEncode

Optimal Table Serialization for Native Luau/Lua 5.1+

Stars Fork Latest Release Last Modified License Wally

Roblox Marketplace Latte Softworks Discord My GitHub My Twitter


πŸŽ‰ About

LuaEncode is a simple, user-friendly library developers can use for serialization of Luau/Lua tables and data structures. This natively supports both Luau (Vanilla or Roblox's implementation), and Lua 5.1+

🌟 Features

  • Full serialization and output of basic types number, string, table, boolean, and nil for key/values
  • Fast, optimized, and efficient
  • Flexible and user-friendly API
  • Pretty-printing and custom indentation configuration
  • Compatible with all custom Roblox DataTypes (e.g. Instance, UDim2, Vector3, DateTime, etc..) - See Custom Roblox Lua DataType Coverage for more info
  • Built with complete, secure iteration and value reading in mind
  • Built-in duplicate/cyclic detection and stack limit
  • Raw keys/value input with FunctionsReturnRaw

βš™οΈ Installation

  • GitHub Releases

    You can download LuaEncode.lua or LuaEncode.rbxm module for the latest GitHub release, and use the module as desired

  • Rojo/Wally

    If you're familiar with Rojo or Wally, you can either clone the repository and build the model yourself, or import in your Wally.toml as a dependency:

    LuaEncode = "regginator/luaencode@1.3.1"
  • Roblox Marketplace

    You can use the LuaEncode module on the Roblox Marketplace directly, and it'll always be updated via its ID:

    local LuaEncode = require(12791121865)

πŸš€ Basic Usage

local LuaEncode = require("path/to/LuaEncode")

local Table = {
    foo = "bar",
    baz = {
        1,
        2,
        3,
        [5] = 5,
    },
    qux = function()
        return "\"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"
    }

πŸ”¨ API

LuaEncode(inputTable: {[any]: any}, options: {[string]: any}): string

Options

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)
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
StackLimit <number?:500> The limit to the stack level before recursive encoding cuts off, and stops execution. This is used to prevent stack overflow errors and such. You could use math.huge here if you really wanted
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

Custom Roblox Lua DataType Coverage

(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)


πŸ›οΈ License

See file: LICENSE

MIT License

Copyright (c) 2022-2023 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.