From 019100ee690814935ebcf9d6bb1cc88b239d67ef Mon Sep 17 00:00:00 2001 From: Victor Starodub Date: Wed, 13 Apr 2016 16:17:22 +0300 Subject: [PATCH] closes #9: Fixed parsing null as a struct. --- gen/decoder.go | 4 ++++ tests/basic_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/gen/decoder.go b/gen/decoder.go index ea38f2b..c831d49 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -214,6 +214,10 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { typ := g.getType(t) fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") + fmt.Fprintln(g.out, " if in.IsNull() {") + fmt.Fprintln(g.out, " in.Skip()") + fmt.Fprintln(g.out, " return") + fmt.Fprintln(g.out, " }") // Init embedded pointer fields. for i := 0; i < t.NumField(); i++ { diff --git a/tests/basic_test.go b/tests/basic_test.go index 4231eea..a6a0413 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -91,3 +91,14 @@ func TestRawMessageSTD(t *testing.T) { t.Errorf("json.Unmarshal() = %v; want %v", gotV, wantV) } } + +func TestParseNull(t *testing.T) { + var got, want SubStruct + if err := easyjson.Unmarshal([]byte("null"), &got); err != nil { + t.Errorf("Unmarshal() error: %v", err) + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("Unmarshal() = %+v; want %+v", got, want) + } +}