diff --git a/decode.go b/decode.go index 9a6f6afe..83d60559 100644 --- a/decode.go +++ b/decode.go @@ -511,7 +511,7 @@ func readArrayHeader(r *buffer) (length int64, _ error) { } length = int64(binary.BigEndian.Uint32(buf[4:8])) default: - return 0, errorErrorf("type code %#02x is not a recognized list type", type_) + return 0, errorErrorf("type code %#02x is not a recognized array type", type_) } return length, nil } @@ -568,7 +568,7 @@ func readBinary(r *buffer) ([]byte, error) { } length = int64(binary.BigEndian.Uint32(buf)) default: - return nil, errorErrorf("type code %#02x is not a recognized string type", type_) + return nil, errorErrorf("type code %#02x is not a recognized binary type", type_) } buf, ok := r.next(length) @@ -579,6 +579,10 @@ func readBinary(r *buffer) ([]byte, error) { } func readAny(r *buffer) (interface{}, error) { + if tryReadNull(r) { + return nil, nil + } + type_, err := r.peekType() if err != nil { return nil, errorNew("invalid length") diff --git a/encode.go b/encode.go index ab6bad65..bb895e89 100644 --- a/encode.go +++ b/encode.go @@ -42,6 +42,8 @@ type marshaler interface { func marshal(wr *buffer, i interface{}) error { switch t := i.(type) { + case nil: + wr.writeByte(byte(typeCodeNull)) case bool: if t { wr.writeByte(byte(typeCodeBoolTrue)) diff --git a/marshal_test.go b/marshal_test.go index 56ff985a..a100f86b 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -188,13 +188,22 @@ func TestMarshalUnmarshal(t *testing.T) { } } + // handle special case around nil type + if type_ == nil { + err = unmarshal(&buf, nil) + if err != nil { + t.Fatal(fmt.Sprintf("%+v", err)) + return + } + return + } + newType := reflect.New(reflect.TypeOf(type_)) err = unmarshal(&buf, newType.Interface()) if err != nil { t.Fatal(fmt.Sprintf("%+v", err)) return } - cmpType := reflect.Indirect(newType).Interface() if !testEqual(type_, cmpType) { t.Errorf("Roundtrip produced different results:\n %s", testDiff(type_, cmpType)) @@ -537,6 +546,7 @@ var ( } generalTypes = []interface{}{ + nil, UUID{1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16}, bool(true), int8(math.MaxInt8),