/tomlua

TOML parser implemented in pure Lua.

Primary LanguageLua

Lua TOML Encoder/Decoder Library

License

A Lua library for encoding and decoding TOML file formats to/from JSON and Lua tables.

Table of Contents

Installation

To use this library in your Lua project, simply include the toml_encoder_decoder.lua file in your project and require it in your code.

local toml = require("toml_encoder_decoder")

Usage

Encoding

Encode Lua tables or JSON strings to TOML.

local luaTable = { key1 = "value1", key2 = { nestedKey = "nestedValue" } }
local tomlString = toml.encode(luaTable)
local jsonString = '{"key1":"value1","key2":{"nestedKey":"nestedValue"}}'
local tomlString = toml.encode(jsonString, { format = "json" })

Decoding

Decode TOML strings to Lua tables or JSON strings.

local tomlString = 'key1 = "value1"\n[key2]\nnestedKey = "nestedValue"'
local luaTable = toml.decode(tomlString)
local jsonString = '{"key1":"value1","key2":{"nestedKey":"nestedValue"}}'
local luaTable = toml.decode(jsonString, { format = "json" })

Example

local toml = require("toml_encoder_decoder")

-- Encoding
local luaTable = { key1 = "value1", key2 = { nestedKey = "nestedValue" } }
local tomlString = toml.encode(luaTable)
print("Encoded TOML:")
print(tomlString)

-- Decoding
local decodedTable = toml.decode(tomlString)
print("\nDecoded Lua Table:")
for key, value in pairs(decodedTable) do
    print(key, value)
end

Contributing

Feel free to contribute to this project. Fork it, make changes, and create a pull request. Any contributions are welcome!

License

This library is open-sourced under the MIT License.