diff --git a/marshal.go b/marshal.go index 6984dd8f..27867fba 100644 --- a/marshal.go +++ b/marshal.go @@ -445,8 +445,11 @@ func (t *Tree) Unmarshal(v interface{}) error { // See Marshal() documentation for types mapping table. func (t *Tree) Marshal() ([]byte, error) { var buf bytes.Buffer - err := NewEncoder(&buf).Encode(t) - return buf.Bytes(), err + _, err := t.WriteTo(&buf) + if err != nil { + return nil, err + } + return buf.Bytes(), nil } // Unmarshal parses the TOML-encoded data and stores the result in the value diff --git a/marshal_test.go b/marshal_test.go index 107587e6..30f5bc88 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1675,3 +1675,27 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) { } }) } + +func TestTreeMarshal(t *testing.T) { + cases := [][]byte{ + basicTestToml, + marshalTestToml, + emptyTestToml, + pointerTestToml, + } + for _, expected := range cases { + t.Run("", func(t *testing.T) { + tree, err := LoadBytes(expected) + if err != nil { + t.Fatal(err) + } + result, err := tree.Marshal() + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(result, expected) { + t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result) + } + }) + } +}