marshal top level nil correctly
diff --git a/decode.go b/decode.go
index 74eda3c..ba926c0 100644
--- a/decode.go
+++ b/decode.go
@@ -516,7 +516,7 @@
d.unmarshal(n, out)
case sequenceNode:
// Step backwards as earlier nodes take precedence.
- for i := len(n.children)-1; i >= 0; i-- {
+ for i := len(n.children) - 1; i >= 0; i-- {
ni := n.children[i]
if ni.kind == aliasNode {
an, ok := d.doc.anchors[ni.value]
diff --git a/decode_test.go b/decode_test.go
index a290645..88fa4c7 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -372,7 +372,7 @@
map[string]time.Duration{"a": 3 * time.Second},
},
- // Issue #24.
+ // Issue #24.
{
"a: <foo>",
map[string]string{"a": "<foo>"},
diff --git a/encode.go b/encode.go
index 1d928b0..fb88bb3 100644
--- a/encode.go
+++ b/encode.go
@@ -56,6 +56,10 @@
func (e *encoder) marshal(tag string, in reflect.Value) {
var value interface{}
+ if !in.IsValid() {
+ e.nilv()
+ return
+ }
if getter, ok := in.Interface().(Getter); ok {
tag, value = getter.GetYAML()
if value == nil {
diff --git a/encode_test.go b/encode_test.go
index 83dc490..4b1480e 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -2,8 +2,8 @@
import (
"fmt"
- "gopkg.in/yaml.v1"
. "gopkg.in/check.v1"
+ "gopkg.in/yaml.v1"
"math"
"strconv"
"strings"
@@ -17,6 +17,9 @@
data string
}{
{
+ nil,
+ "null\n",
+ }, {
&struct{}{},
"{}\n",
}, {
@@ -220,7 +223,7 @@
"a: 3s\n",
},
- // Issue #24.
+ // Issue #24.
{
map[string]string{"a": "<foo>"},
"a: <foo>\n",