encoding/json: ignore mismatched null during unmarshal?
gopherbot opened this issue · 13 comments
gopherbot commented
by dahankzter:
Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull", "hg update default", rebuild, and
retry
what you did to
reproduce the problem. Thanks.
What steps will reproduce the problem?
1. Create a struct with a string field
type T struct {
S string
}
2. Create json with the nested string field null
{\"s\" : null}
3. Unmarshal it
What is the expected output?
I expect no error to be returned from the json.Unmarshall
What do you see instead?
Error message:
Unmarshal error: json: cannot unmarshal null into Go value of type string
Which compiler are you using (5g, 6g, 8g, gccgo)?
6g
Which operating system are you using?
Arch Linux x86_64
Which revision are you using? (hg identify)
5190380293e8+ tip
Please provide any additional information below.
The patch:
diff -r 5190380293e8 src/pkg/encoding/json/decode.go
--- a/src/pkg/encoding/json/decode.go Thu Dec 08 15:12:08 2011 +0900
+++ b/src/pkg/encoding/json/decode.go Thu Dec 08 18:06:53 2011 +0100
@@ -592,7 +592,7 @@
switch v.Kind() {
default:
d.saveError(&UnmarshalTypeError{"null", v.Type()})
- case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
+ case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice, reflect.String:
v.Set(reflect.Zero(v.Type()))
}
Should be tentatively sufficient.rsc commented
rsc commented
gopherbot commented
I agree but isn't it so that the resulting struct is identical with or without the applied change? The only difference seems to be that there is an error returned. Upon checking the status of the returned error I would note that something went wrong and act accordingly when in fact there was nothing wrong other than that the unmarshalling noted a null string and decided not to set that struct element value. I may have missed something crucial in the code but to me it seems at least wrong to return an error in this case although I agree that null is not a string. Henrik
rsc commented
gopherbot commented
rsc commented
gopherbot commented
rsc commented
rsc commented
rsc commented
gopherbot commented
Fixed by: https://golang.org/cl/6759043/
gopherbot commented
CL https://golang.org/cl/100430043 mentions this issue.