forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 35
/
yaml.go
29 lines (24 loc) · 821 Bytes
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package codec
import (
"encoding/json"
"github.com/gogo/protobuf/proto"
"gopkg.in/yaml.v2"
)
// MarshalYAML marshals toPrint using jsonMarshaler to leverage specialized MarshalJSON methods
// (usually related to serialize data with protobuf or amin depending on a configuration).
// This involves additional roundtrip through JSON.
func MarshalYAML(jsonMarshaler JSONMarshaler, toPrint proto.Message) ([]byte, error) {
// We are OK with the performance hit of the additional JSON roundtip. MarshalYAML is not
// used in any critical parts of the system.
bz, err := jsonMarshaler.MarshalJSON(toPrint)
if err != nil {
return nil, err
}
// generate YAML by decoding JSON and re-encoding to YAML
var j interface{}
err = json.Unmarshal(bz, &j)
if err != nil {
return nil, err
}
return yaml.Marshal(j)
}