mirror of
https://github.com/jimeh/go-golden.git
synced 2026-02-19 11:16:47 +00:00
Tests have started using github.com/jimeh/go-mocktesting which allows testing unhappy paths where t.Fatal() and related functions are called.
46 lines
930 B
Go
46 lines
930 B
Go
package marshal
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// JSON returns the JSON encoding of v. Returned JSON is intended by two spaces
|
|
// (pretty formatted), and is not HTML escaped.
|
|
func JSON(v interface{}) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetIndent("", " ")
|
|
enc.SetEscapeHTML(false)
|
|
|
|
err := enc.Encode(v)
|
|
|
|
return buf.Bytes(), err
|
|
}
|
|
|
|
// XML returns the XML encoding of v. Returned XML is intended by two spaces
|
|
// (pretty formatted).
|
|
func XML(v interface{}) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
enc := xml.NewEncoder(&buf)
|
|
enc.Indent("", " ")
|
|
|
|
err := enc.Encode(v)
|
|
|
|
return buf.Bytes(), err
|
|
}
|
|
|
|
// YAML returns the YAML encoding of v. Returned YAML is intended by two spaces.
|
|
func YAML(v interface{}) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
enc := yaml.NewEncoder(&buf)
|
|
enc.SetIndent(2)
|
|
|
|
err := enc.Encode(v)
|
|
|
|
return buf.Bytes(), err
|
|
}
|