jsonutil

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package jsonutil provides JSON utilities with consistent formatting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalIndentWithNewline

func MarshalIndentWithNewline(v any, prefix, indent string) ([]byte, error)

MarshalIndentWithNewline is like json.MarshalIndent but adds a trailing newline. This ensures JSON files have proper POSIX line endings.

func MarshalWithNoHTMLEscape added in v0.5.6

func MarshalWithNoHTMLEscape(v any) ([]byte, error)

MarshalWithNoHTMLEscape is like json.Marshal but disables HTML escaping.

func WriteFileAtomic added in v0.6.2

func WriteFileAtomic(filePath string, data []byte, perm fs.FileMode) error

WriteFileAtomic writes data to filePath atomically by writing to a temp file in the same directory, fsyncing it, renaming into place, and fsyncing the parent directory. A crash or signal mid-write leaves the original file intact rather than a truncated partial — important for config files like .entire/settings.json that callers expect to remain parseable across interrupted writes.

The fsync between Write and Close guarantees the temp file's bytes are on disk before the rename takes effect; without it, some filesystems (notably ext4 with non-default mount options) can surface the rename as completed while the file is still empty after a hard crash.

The parent-directory fsync after rename guarantees the rename's directory entry is durable. Without it, the file contents are on disk but the directory may still point to the pre-rename state after a crash, so the "leaves the original intact" promise would silently break. Windows does not support directory fsync; we make this step best-effort so the call does not fail on platforms where the operation is a no-op.

perm is applied to the temp file via Chmod before rename so the final file lands with the requested permission regardless of the temp file's default.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL