Skip to content

serde.serialize

Serializes data using the given format.

Signature

luau
function serialize(format: SerializationFormat, data: any, options: SerializeOptions?): string

Types

luau
type SerializationFormat = "json" | "toml" | "yaml"

type SerializeOptions = {
    pretty: boolean?,
}

Summary

Parameters

ParameterTypeDescription
formatSerializationFormatThe serialization format to use.
dataanyThe data to be serialized.
optionsSerializeOptions?Optional configuration for the serialization.

Example

json

lua
local serde = require("@std/serde")

local luaTable = {
    Index = true,
    Array = {
      "Value1",
      "Value2"
    },
    Table = {
      Index1 = "Value1",
      Index2 = "Value2"
    },
}

print(serde.serialize("json", luaTable, { pretty = true })) --[[
    {
      "Index": true,
      "Array": [
        "Value1",
        "Value2"
      ],
      "Table": {
        "Index1": "Value1",
        "Index2": "Value2"
      }
    }
]]

toml

lua
local serde = require("@std/serde")

local luaTable = {
  title = "Example",
  owner = {
    name = "Tom",
    dob = "1979-05-27T07:32:00Z"
  }
}

print(serde.serialize("toml", luaTable)) --[[
    title = "Example"

    [owner]
    name = "Tom"
    dob = "1979-05-27T07:32:00Z"
]]

yaml

lua
local serde = require("@std/serde")

local luaTable = {
  Index = true,
  Array = {
    "One",
    "Two",
    "Three"
  },
  Table = {
    Key1 = "Value1",
    Key2 = "Value2"
  }
}

print(serde.serialize("yaml", luaTable)) --[[
    Index: true
    Array:
      - One
      - Two
      - Three
    Table:
      Key1: Value1
      Key2: Value2
]]