Luadata by Example

String Transform

Game addon data files can contain enormous strings — serialized data blobs, encoded textures, or chat logs that run to hundreds of thousands of characters. When you're parsing these files for structure, you often don't need the full string content.

The WithStringTransform option lets you limit string lengths during parsing. Strings that exceed the configured MaxLen are automatically transformed.

lua
name = "Thrall"
biography = "A very long string that goes on and on and contains the entire history of the Horde from the First War through the events of the latest expansion including every battle and political development..."
go
reader, err := luadata.TextToJSON("input", input,
    luadata.WithStringTransform(20, "truncate"),
)
output
{
  "name": "Thrall",
  "biography": "A very long string th"
}

The name field is untouched because it's under 20 characters. The biography field is truncated to exactly 20 characters.

The transformation happens during parsing, so the full string content is never stored in memory. This makes it safe to process very large files.

Like the other options, the string transform applies to all strings in the output. For per-field control over string encoding, a JSON Schema can declare specific fields as binary — see Schema String Formats.

The next example covers all four transform modes.

Want more flexibility? Open the interactive converter to try any Lua input with all available options.