github.com/zhongdalu/gf@v1.0.0/g/encoding/gyaml/gyaml.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // Package gyaml provides accessing and converting for YAML content.
     8  package gyaml
     9  
    10  import "github.com/zhongdalu/gf/third/github.com/ghodss/yaml"
    11  
    12  func Encode(v interface{}) ([]byte, error) {
    13  	return yaml.Marshal(v)
    14  }
    15  
    16  func Decode(v []byte) (interface{}, error) {
    17  	var result interface{}
    18  	if err := yaml.Unmarshal(v, &result); err != nil {
    19  		return nil, err
    20  	}
    21  	return result, nil
    22  }
    23  
    24  func DecodeTo(v []byte, result interface{}) error {
    25  	return yaml.Unmarshal(v, &result)
    26  }
    27  
    28  func ToJson(v []byte) ([]byte, error) {
    29  	return yaml.YAMLToJSON(v)
    30  }