json.Unmarshaler difference: UnmarshalJSON([]byte("null")) skipped
efepapa opened this issue · 1 comments
efepapa commented
Go 1.14.8 linux/amd64
v0.1.15 does not use my custom UnmarshalJSON()
for []byte("null")
Documentation 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 = ""
achille-roussel commented
Hello @efepapa, thanks for reporting!
It looks like it's caused by https://github.com/segmentio/encoding/blob/master/json/decode.go#L1138-L1140