Repository: segmentio/encoding (go)

Issue:
json.Unmarshaler difference: UnmarshalJSON([]byte("null")) skipped
Go 1.14.8 linux/amd64

v0.1.15 does not use my custom `UnmarshalJSON()` for `[]byte("null")`

[Documentation](https://golang.org/pkg/encoding/json/#Unmarshaler) says:
> By convention, to approximate the behavior of Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.

`encoding/json` calls it, `github.com/segmentio/encoding/json` skips it.

```
package main

import (
	//"encoding/json"
	"fmt"
	"github.com/segmentio/encoding/json"
)

type RawJsonString string

func (r *RawJsonString) UnmarshalJSON(b []byte) error {
	fmt.Printf("UnmarshalJSON: %s\n", string(b))
	if len(b) == 0 {
		*r = "null"
	} else {
		*r = RawJsonString(b)
	}
	return nil
}

func main() {
	var out RawJsonString
	if err := json.Unmarshal([]byte("null"), &out); err != nil {
		panic(err)
	}
	fmt.Printf("out = %#v\n", out)
}
```


`encoding/json`:
```
  UnmarshalJSON: null
  out = "null"
```

`github.com/segmentio/encoding/json`:
```
  out = ""
```


Pull request description:
fix issue 44
Fixes #44 

Patch:
diff --git a/json/decode.go b/json/decode.go
index e01b444..caf6e8a 100644
--- a/json/decode.go
+++ b/json/decode.go
@@ -1135,10 +1135,6 @@ func (d decoder) decodeJSONUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Typ
 		return b, err
 	}
 
-	if len(v) != 0 && v[0] == 'n' { // null
-		return b, nil
-	}
-
 	u := reflect.NewAt(t, p)
 	if !pointer {
 		u = u.Elem()
@@ -1147,6 +1143,7 @@ func (d decoder) decodeJSONUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Typ
 	if u.IsNil() {
 		u.Set(reflect.New(t))
 	}
+
 	return b, u.Interface().(Unmarshaler).UnmarshalJSON(v)
 }
 


Test patch:
diff --git a/json/json_test.go b/json/json_test.go
index cf7e18d..e78debe 100644
--- a/json/json_test.go
+++ b/json/json_test.go
@@ -1559,6 +1559,27 @@ func TestGithubIssue41(t *testing.T) {
 
 }
 
+func TestGithubIssue44(t *testing.T) {
+	var out rawJsonString
+	if err := Unmarshal([]byte("null"), &out); err != nil {
+		t.Fatal(err)
+	}
+	if out != "null" {
+		t.Errorf("wanted \"null\" but got %q", out)
+	}
+}
+
+type rawJsonString string
+
+func (r *rawJsonString) UnmarshalJSON(b []byte) error {
+	if len(b) == 0 {
+		*r = "null"
+	} else {
+		*r = rawJsonString(b)
+	}
+	return nil
+}
+
 func TestSetTrustRawMessage(t *testing.T) {
 	buf := &bytes.Buffer{}
 	enc := NewEncoder(buf)