diff --git a/yaml.go b/yaml.go index 801dbe9..90d049d 100644 --- a/yaml.go +++ b/yaml.go @@ -342,7 +342,7 @@ cast_type: case uint64: s = "xd" + strconv.FormatUint(typedVal, 10) case string: - if typedVal[0] == 'x' { + if len(typedVal) > 0 && typedVal[0] == 'x' { s = "x" + typedVal } } @@ -367,7 +367,7 @@ func convertNestedJSONNumbers(jsonObj interface{}) (interface{}, error) { typedJSONObj[i], err = convertNestedJSONNumbers(value) } case string: - if typedJSONObj[0] == 'x' { + if len(typedJSONObj) > 0 && typedJSONObj[0] == 'x' { switch typedJSONObj[1] { case 'a': var val int64 diff --git a/yaml_test.go b/yaml_test.go index da51fa8..76b7007 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -242,6 +242,14 @@ func TestUnmarshal(t *testing.T) { "b": float64(333), }, }, + { + encoded: []byte("a: \"\"\nb: \n"), + decodeInto: new(interface{}), + decoded: map[string]interface{}{ + "a": "", + "b": nil, + }, + }, } for _, test := range tests {