Skip to content

Serialization

env.zig can serialize configurations back to .env format.

Basic Serialization

zig
const output = try env.serialize();
defer allocator.free(output);
// output: "KEY=value\nOTHER=foo\n"

Save to File

zig
try env.save("output.env");

Serialization Options

Config OptionDefaultDescription
sort_keysfalseSort keys alphabetically
quote_spacestrueQuote values containing spaces
trailing_newlinetrueAdd a trailing newline
preserve_commentsfalsePreserve original comments
indent0Indentation spaces

Sorted Keys

zig
var env = env_mod.Env.init(allocator, .{ .sort_keys = true });
// Keys will be sorted alphabetically in output

Custom Quoting

Values with spaces, quotes, or special characters are automatically escaped:

zig
try env.set("MESSAGE", "hello world");
const output = try env.serialize();
// MESSAGE="hello world"

Example Output

env
DATABASE_HOST=localhost
DATABASE_NAME=myapp
DATABASE_PORT=5432
LOG_LEVEL=info

Released under the MIT License.