Documentation
¶
Overview ¶
Package jsonutils defines utilities for working with JSON.
Index ¶
- func LoadAs[T any](path string) result.Result[T]
- func LoadWith[T any](read func(string) ([]byte, error), path string) result.Result[T]
- func PrintJSONOn[T any](data T, writer io.Writer) error
- func Save[T any](value T, path string, perm os.FileMode) error
- func SaveWith[T any](writeFunc func(string, []byte, os.FileMode) error, value T, path string, ...) error
- func ToJSON[T any](data T) result.Result[string]
- func UnmarshalAs[T any](content []byte) result.Result[T]
- func UnmarshalFromReader[T any](reader io.Reader) result.Result[T]
- func UnmarshalFromReaderInto[T any](reader io.Reader, value *T) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadAs ¶ added in v0.2.0
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
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 ¶
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
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 ¶
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
UnmarshalAs ummarshals the given content into a result of a new instance of T.
func UnmarshalFromReader ¶
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
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.