safeyaml

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 14 Imported by: 50

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONtoYAMLBytes

func JSONtoYAMLBytes(jsonBytes []byte) ([]byte, error)

JSONtoYAMLBytes converts json data to yaml output while preserving order of object fields.

func JSONtoYAMLMapSlice

func JSONtoYAMLMapSlice(jsonBytes []byte) (interface{}, error)

JSONtoYAMLMapSlice decodes JSON bytes into an interface{} where all objects and maps are decoded as a yaml.MapSlice to preserve key ordering. This can be useful if a type defines custom JSON marshal logic and translating the JSON representation to a YAML representation while preserving key ordering is sufficient for serialization. For example:

func (o Foo) MarshalYAML() (interface{}, error) {
	jsonBytes, err := json.Marshal(o)
	if err != nil {
		return nil, err
	}
	return JSONtoYAML(jsonBytes)
}

func Marshal

func Marshal(in interface{}) (out []byte, err error)

func UnmarshalerToJSONBytes

func UnmarshalerToJSONBytes(unmarshal func(interface{}) error) ([]byte, error)

UnmarshalerToJSONBytes decodes YAML bytes using the provided unmarshal function, converts the object to a JSON-compatible representation and returns the result of marshalling the converted object as JSON. This can be used in an UnmarshalYAML implementation where the type implements custom UnmarshalJSON behavior and wants it to apply to YAML as well. For example:

func (o *Foo) UnmarshalYAML(unmarshal func(interface{}) error) error {
	jsonBytes, err := safeyaml.YAMLUnmarshalerToJSONBytes(unmarshal)
	if err != nil {
		return err
	}
	return json.Unmarshal(jsonBytes, o)
}

func YAMLtoJSONBytes

func YAMLtoJSONBytes(yamlBytes []byte) ([]byte, error)

YAMLtoJSONBytes converts YAML content to JSON. Returns an error if it encounters types that are invalid in JSON but valid in YAML (e.g. non-string map keys).

Types

This section is empty.

Jump to

Keyboard shortcuts

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