forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
46 lines (41 loc) · 1015 Bytes
/
marshal.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"encoding/json"
"github.com/ghodss/yaml"
)
// safeYAML returns a string after marshalling the supplied parameter into YAML
func safeYAML(p interface{}) string {
b, err := json.Marshal(p)
if err != nil {
warn("error marshalling JSON: %s", err)
return ""
}
yb, err := yaml.JSONToYAML(b)
if err != nil {
warn("error converting JSON to YAML: %s", err)
return ""
}
return string(yb)
}
// safeJSON returns a string after marshalling the supplied parameter into
// JSON. Accepts an optional argument to trigger pretty/indented formatting of
// the JSON string
func safeJSON(p interface{}, indent bool) string {
var b []byte
var err error
if !indent {
b, err = json.Marshal(p)
} else {
b, err = json.MarshalIndent(&p, "", " ")
}
if err != nil {
warn("error marshalling JSON: %s", err)
return ""
}
return string(b)
}