FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go


Description:
json.Unmarshal: convert nulls to zero values

As discussed in issue 2540, nulls are allowed for any type in JSON so
they should not result in an error during Unmarshal.
Fixes issue 2540.

Please review this at http://codereview.appspot.com/6759043/

Affected files:
M src/pkg/encoding/json/decode.go
M src/pkg/encoding/json/decode_test.go


Index: src/pkg/encoding/json/decode.go
===================================================================
--- a/src/pkg/encoding/json/decode.go
+++ b/src/pkg/encoding/json/decode.go
@@ -616,12 +616,7 @@

switch c := item[0]; c {
case 'n': // null
- switch v.Kind() {
- default:
- d.saveError(&UnmarshalTypeError{"null", v.Type()})
- case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
- v.Set(reflect.Zero(v.Type()))
- }
+ v.Set(reflect.Zero(v.Type()))

case 't', 'f': // true, false
value := c == 't'
Index: src/pkg/encoding/json/decode_test.go
===================================================================
--- a/src/pkg/encoding/json/decode_test.go
+++ b/src/pkg/encoding/json/decode_test.go
@@ -953,3 +953,28 @@
}
}
}
+
+// JSON null values should correspond to zero values instead of resulting
in an error.
+// Issue 2540
+func TestUnmarshalNulls(t *testing.T) {
+ jsonData := []byte(`{
+ "Bool" : null,
+ "Int" : null,
+ "Int8" : null,
+ "Int16" : null,
+ "Int32" : null,
+ "Int64" : null,
+ "Uint" : null,
+ "Uint8" : null,
+ "Uint16" : null,
+ "Uint32" : null,
+ "Uint64" : null,
+ "Float32" : null,
+ "Float64" : null,
+ "String" : null}`)
+ var nulls All
+ err := Unmarshal(jsonData, &nulls)
+ if err != nil {
+ t.Errorf("Unmarshal of null values failed: %v", err)
+ }
+}

Search Discussions

  • Rsc at Nov 1, 2012 at 4:42 pm
    https://codereview.appspot.com/6759043/diff/3003/src/pkg/encoding/json/decode.go
    File src/pkg/encoding/json/decode.go (right):

    https://codereview.appspot.com/6759043/diff/3003/src/pkg/encoding/json/decode.go#newcode619
    src/pkg/encoding/json/decode.go:619: v.Set(reflect.Zero(v.Type()))
    I think the old code should stay; just delete the default case.
    If we get

    "f": null

    where "f" is a float64, I don't mind just ignoring the null but I don't
    think it's appropriate to set f to 0.

    https://codereview.appspot.com/6759043/
  • Russ Cox at Nov 1, 2012 at 4:42 pm
    Also please update the test to verify that null does not zero values
    of non-reference type.
  • Rick Arnold at Nov 4, 2012 at 8:59 pm
    Done. PTAL
    On Thursday, November 1, 2012 12:42:39 PM UTC-4, rsc wrote:

    Also please update the test to verify that null does not zero values
    of non-reference type.
  • Rickarnoldjr at Nov 2, 2012 at 2:07 am
    Hello rsc@golang.org (cc: golang-dev@googlegroups.com),

    Please take another look.


    http://codereview.appspot.com/6759043/
  • Rsc at Nov 12, 2012 at 8:17 pm
  • Rsc at Nov 12, 2012 at 8:35 pm
    *** Submitted as
    http://code.google.com/p/go/source/detail?r=9a07c3da6c44 ***

    encoding/json: skip unexpected null values

    As discussed in issue 2540, nulls are allowed for any type in JSON so
    they should not result in an error during Unmarshal.
    Fixes issue 2540.

    R=rsc
    CC=golang-dev
    http://codereview.appspot.com/6759043

    Committer: Russ Cox <rsc@golang.org>


    http://codereview.appspot.com/6759043/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedOct 24, '12 at 4:20a
activeNov 12, '12 at 8:35p
posts7
users2
websitegolang.org

2 users in discussion

Rsc: 4 posts Rickarnoldjr: 3 posts

People

Translate

site design / logo © 2022 Grokbase