json

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 6 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMarshalOptions = &MarshalOptions{EscapeHTML: true, Indent: true, Sort: false}

Functions

func Marshal

func Marshal(obj interface{}) ([]byte, error)

func MarshalHelper

func MarshalHelper(obj interface{}, escapeHtml bool, indent bool) ([]byte, error)

func MarshalToFile

func MarshalToFile(obj interface{}, path string) error

func MarshalToFileOptions

func MarshalToFileOptions(obj interface{}, path string, options *MarshalOptions) error

func MarshalToString

func MarshalToString(obj interface{}) (string, error)

func MarshalWithOptions

func MarshalWithOptions(obj interface{}, options *MarshalOptions) ([]byte, error)

func MustMarshalToString added in v0.2.0

func MustMarshalToString(obj interface{}) string

func Parse added in v0.1.9

func Parse[T any](bs []byte) (*T, error)

func ParseFile added in v0.1.9

func ParseFile[T any](path string) (*T, error)

func ParseString added in v0.1.9

func ParseString[T any](contents string) (*T, error)

func Print

func Print(obj interface{})

func PrintOptions

func PrintOptions(obj interface{}, options *MarshalOptions)

func Remarshal

func Remarshal(obj interface{}) (interface{}, error)

Remarshal is of questionable utility. It first marshals, then unmarshals

into an `interface{}`, which throws away struct types (struct types cause
problems for predictability of order during marshaling).

func SortFileOptions

func SortFileOptions(path string, escapeHtml bool, indent bool) error

func SortOptions

func SortOptions(contents []byte, escapeHtml bool, indent bool) ([]byte, error)

SortOptions is used to get keys from a struct into a sorted order.

See https://stackoverflow.com/a/61887446/894284.
Apparently, golang's json library sorts keys from
maps, but NOT from structs.  So this function works by reading json into a
generic structure of maps, then marshaling back into completely sorted json.
NOTE: this only works for a json object.

Types

type MarshalOptions

type MarshalOptions struct {
	EscapeHTML bool
	Indent     bool
	Sort       bool
}

Jump to

Keyboard shortcuts

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