jsonutils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonutils defines utilities for working with JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadAs added in v0.2.0

func LoadAs[T any](path string) result.Result[T]

LoadAs reads the content from the given path and ummarshals it into a result of a new instance of T.

func LoadWith added in v0.2.0

func LoadWith[T any](
	read func(string) ([]byte, error),
	path string,
) result.Result[T]

LoadWith passes the given path to the given read function to get the content. That content is then marshaled into a result of a new instance of T. This is useful when using a file system abstraction like https://github.com/spf13/afero.

fs := afero.Afero{Fs: afero.NewMemMapFs()}
err := jsonutils.LoadWith[string](fs.ReadFile, "/tmp/person.json")

func PrintJSONOn

func PrintJSONOn[T any](data T, writer io.Writer) error

PrintJSONOn pretty-prints the given data as JSON on the given writer. If the given data is not valid JSON an error is returned.

func Save added in v0.2.0

func Save[T any](value T, path string, perm os.FileMode) error

Save marshals the given T and writes it to a file at the given path with the given permissions. The file is truncated if it exists.

func SaveWith added in v0.2.0

func SaveWith[T any](
	writeFunc func(string, []byte, os.FileMode) error,
	value T,
	path string,
	perm os.FileMode,
) error

SaveWith marshals the given T and calls the given writeFunc with the resulting content, given path, and given permissions.This is useful when using a file system abstraction like https://github.com/spf13/afero.

fs := afero.Afero{Fs: afero.NewMemMapFs()}
err := jsonutils.SaveWith(fs.WriteFile, instance, "/tmp/person.json", 0700)

func ToJSON

func ToJSON[T any](data T) result.Result[string]

ToJSON returns a pretty-printed JSON string for the given data. If the given data is not valid JSON an error is returned. If you are marshaling a large data structure and are concerned about the performance of iteratively growing the destination strings.Builder then create your own builder, grow it to your expected size, and then call PrintJSONOn. If you want the bytes then call the following.

result.New(json.MarshalIndent(data, ``, `    `))

func UnmarshalAs added in v0.2.0

func UnmarshalAs[T any](content []byte) result.Result[T]

UnmarshalAs ummarshals the given content into a result of a new instance of T.

func UnmarshalFromReader

func UnmarshalFromReader[T any](reader io.Reader) result.Result[T]

UnmarshalFromReader unmarshals the JSON from the given reader and populates a new instance of T. The reader will be read fully before returning. If the data in the reader is not valid JSON an error is returned.

func UnmarshalFromReaderInto added in v0.2.0

func UnmarshalFromReaderInto[T any](reader io.Reader, value *T) error

UnmarshalFromReaderInto unmarshals the JSON from the given reader and populates the given instance. The reader will be read fully before returning. If the data in the reader is not valid JSON an error is returned. Note: This function remains for backward compatibility. Prefer using `json.NewDecoder(reader).Decode(value)`.

Types

This section is empty.

Jump to

Keyboard shortcuts

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