github.com/gogf/gf@v1.16.9/encoding/gyaml/gyaml.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gyaml provides accessing and converting for YAML content. 8 package gyaml 9 10 import ( 11 "github.com/gogf/gf/internal/json" 12 "gopkg.in/yaml.v3" 13 14 "github.com/gogf/gf/util/gconv" 15 ) 16 17 func Encode(v interface{}) ([]byte, error) { 18 return yaml.Marshal(v) 19 } 20 21 func Decode(v []byte) (interface{}, error) { 22 var result map[string]interface{} 23 if err := yaml.Unmarshal(v, &result); err != nil { 24 return nil, err 25 } 26 return gconv.MapDeep(result), nil 27 } 28 29 func DecodeTo(v []byte, result interface{}) error { 30 return yaml.Unmarshal(v, result) 31 } 32 33 func ToJson(v []byte) ([]byte, error) { 34 if r, err := Decode(v); err != nil { 35 return nil, err 36 } else { 37 return json.Marshal(r) 38 } 39 }