yaml

package
v0.0.0-...-624083b Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MIT Imports: 13 Imported by: 0

README

用法与JSON库非常相似:

package main

import (
	"fmt"
	yaml "github.com/shenyuanheli/tools"
)

type Person struct {
	Name string `json:"name"` // Affects YAML field names too.
	Age  int    `json:"age"`
}

func main() {
	// Marshal a Person struct to YAML.
	p := Person{"John", 30}
	y, err := yaml.YamlUtils.Marshal(p)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	fmt.Println(string(y))
	/* Output:
	age: 30
	name: John
	*/

	// Unmarshal the YAML back into a Person struct.
	var p2 Person
	err = yaml.YamlUtils.Unmarshal(y, &p2)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	fmt.Println(p2)
	/* Output:
	{John 30}
	*/
}

yaml.YAMLToJSON and yaml.JSONToYAML methods are also available:

package main

import (
	"fmt"
	yaml "github.com/shenyuanheli/tools"
)

func main() {
	j := []byte(`{"name": "John", "age": 30}`)
	y, err := yaml.YamlUtils.JSONToYAML(j)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	fmt.Println(string(y))
	/* Output:
	name: John
	age: 30
	*/
	j2, err := yaml.YamlUtils.YAMLToJSON(y)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	fmt.Println(string(j2))
	/* Output:
	{"age":30,"name":"John"}
	*/
}

Documentation

Overview

*

@author: 1043193460@qq.com
@date: 2022/12/14 12:02
@note:

*

*

  @author: 1043193460@qq.com
  @date: 2022/12/14 12:02
  @note: 这个包首先使用goyaml将YAML转换为JSON,然后使用json。Marshal和json。要转换为结构或从结构转换为结构的反编组
		另请参见http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang

*

*

@author: 1043193460@qq.com
@date: 2022/12/14 12:02
@note: 这个文件包含的变化只是兼容1.10和以后。

*

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisallowUnknownFields

func DisallowUnknownFields(d *json.Decoder) *json.Decoder

DisableUnknownFields将JSON解码器配置为未知时出错 字段会出现,而不是默认情况下删除它们。

Types

type JSONOpt

type JSONOpt func(*json.Decoder) *json.Decoder

JSONOpt是用于从JSON格式解码的解码选项。

type YamlUtils

type YamlUtils struct {
}

func (*YamlUtils) JSONToYAML

func (y *YamlUtils) JSONToYAML(j []byte) ([]byte, error)

将JSON转换为YAML。

func (*YamlUtils) Marshal

func (y *YamlUtils) Marshal(o interface{}) ([]byte, error)

将对象编组为JSON,然后将JSON转换为YAML并返回YAML。

func (*YamlUtils) Unmarshal

func (y *YamlUtils) Unmarshal(b []byte, o interface{}, opts ...JSONOpt) error

解组将YAML转换为JSON,然后使用JSON将其解组为对象,可选地配置JSON解组的行为。

func (*YamlUtils) UnmarshalStrict

func (y *YamlUtils) UnmarshalStrict(b []byte, o interface{}, opts ...JSONOpt) error

UnmarshallStrict与Unmarshall类似重复将导致错误。要严格控制未知字段,请添加DisableUnknownFields选项。

func (*YamlUtils) YAMLToJSON

func (y *YamlUtils) YAMLToJSON(b []byte) ([]byte, error)

YAMLToJSON将YAML转换为JSON。由于JSON是YAML的子集,通过此方法传递JSON应该是一个错误。

func (*YamlUtils) YAMLToJSONStrict

func (y *YamlUtils) YAMLToJSONStrict(b []byte) ([]byte, error)

YAMLToJSONStrict类似于YAMLToJSON,但允许严格的YAML解码,返回任何重复字段名的错误。

Jump to

Keyboard shortcuts

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