yamlconv

package
v0.153.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package yamlconv converts between YAML and types that describe themselves with json tags and MarshalJSON/UnmarshalJSON methods.

YAML reaches such a type through JSON, so its methods run and extensions are carried. Two things have to be reconciled first, both of which YAML resolves and JSON cannot represent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal renders v as YAML via JSON, so v's MarshalJSON runs.

func PrepareForJSON

func PrepareForJSON(n *yaml.Node)

PrepareForJSON retags the scalars that would not survive the conversion.

A date-shaped scalar resolves to a timestamp, which has no JSON form, so it is retagged and keeps the text the document gave it. A scalar written with the tag outright is left alone, being a deliberate request for that type.

A mapping key resolves too: the unquoted 200: written in most specs for a status code is an integer, 18_24 is 1824 and .0 is the number zero. Retagging the key names the member by the text that spells it, which is the name the typed decode reads. JSON has no other name to offer: encoding/json refuses a map[any]any, and encoding/json/v2 refuses one holding a float key and renumbers an integer one, 18_24 to 1824 and 0x2A to 42. A key's own tag goes the same way, a member name being a string however the key is written.

Interim: https://github.com/yaml/go-yaml/issues/452 asks for an option to leave a timestamp-shaped scalar a string, and https://github.com/yaml/go-yaml/issues/491 for a mapping key to be read as its text. Either one removes the retag it covers.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal decodes YAML into v via JSON, so v's UnmarshalJSON runs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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