From 2cb8c451ebb96ba5d22e50137563ea1af144003d Mon Sep 17 00:00:00 2001 From: "i.anferov" Date: Tue, 14 Jul 2020 17:36:35 +0500 Subject: [PATCH 1/2] Duplicated json values on marshalind interfaces implementing easyjson.Marshaler --- gen/encoder.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gen/encoder.go b/gen/encoder.go index 893d9e8..74c530f 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -274,14 +274,15 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT } else { return fmt.Errorf("interface type %v not supported: only interface{} and interfaces that implement json or easyjson Marshaling are allowed", t) } + } else { + fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") + fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") + fmt.Fprintln(g.out, ws+"} else if m, ok := "+in+".(json.Marshaler); ok {") + fmt.Fprintln(g.out, ws+" out.Raw(m.MarshalJSON())") + fmt.Fprintln(g.out, ws+"} else {") + fmt.Fprintln(g.out, ws+" out.Raw(json.Marshal("+in+"))") + fmt.Fprintln(g.out, ws+"}") } - fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") - fmt.Fprintln(g.out, ws+"} else if m, ok := "+in+".(json.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" out.Raw(m.MarshalJSON())") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" out.Raw(json.Marshal("+in+"))") - fmt.Fprintln(g.out, ws+"}") default: return fmt.Errorf("don't know how to encode %v", t) } From 3c9f41b197dc81c49cb5f34fab950eca0c037fb2 Mon Sep 17 00:00:00 2001 From: Alexandr Mayorskiy Date: Tue, 14 Jul 2020 17:27:06 +0300 Subject: [PATCH 2/2] add test with nested marshaler interface --- Makefile | 38 ++++++++++++++++++++------------------ tests/basic_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index ec37cc4..c527340 100644 --- a/Makefile +++ b/Makefile @@ -26,30 +26,32 @@ generate: build ./tests/members_unescaped.go \ ./tests/intern.go \ ./tests/nocopy.go \ + ./tests/escaping.go + bin/easyjson -all \ + ./tests/data.go \ + ./tests/nothing.go \ + ./tests/errors.go \ + ./tests/html.go \ + ./tests/type_declaration_skip.go + bin/easyjson \ + ./tests/nested_easy.go \ + ./tests/named_type.go \ + ./tests/custom_map_key_type.go \ + ./tests/embedded_type.go \ + ./tests/reference_to_pointer.go \ + ./tests/key_marshaler_map.go \ + ./tests/unknown_fields.go \ + ./tests/type_declaration.go \ + ./tests/members_escaped.go \ + ./tests/intern.go \ + ./tests/nocopy.go \ ./tests/escaping.go \ - - bin/easyjson -all ./tests/data.go - bin/easyjson -all ./tests/nothing.go - bin/easyjson -all ./tests/errors.go - bin/easyjson -all ./tests/html.go + ./tests/nested_marshaler.go bin/easyjson -snake_case ./tests/snake.go bin/easyjson -omit_empty ./tests/omitempty.go bin/easyjson -build_tags=use_easyjson -disable_members_unescape ./benchmark/data.go - bin/easyjson ./tests/nested_easy.go - bin/easyjson ./tests/named_type.go - bin/easyjson ./tests/custom_map_key_type.go - bin/easyjson ./tests/embedded_type.go - bin/easyjson ./tests/reference_to_pointer.go - bin/easyjson ./tests/key_marshaler_map.go bin/easyjson -disallow_unknown_fields ./tests/disallow_unknown.go - bin/easyjson ./tests/unknown_fields.go - bin/easyjson ./tests/type_declaration.go - bin/easyjson -all ./tests/type_declaration_skip.go - bin/easyjson ./tests/members_escaped.go bin/easyjson -disable_members_unescape ./tests/members_unescaped.go - bin/easyjson ./tests/intern.go - bin/easyjson ./tests/nocopy.go - bin/easyjson ./tests/escaping.go test: generate go test \ diff --git a/tests/basic_test.go b/tests/basic_test.go index 11013c8..e18e515 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -231,6 +231,33 @@ func TestNestedEasyJsonMarshal(t *testing.T) { } } +func TestNestedMarshaler(t *testing.T) { + s := NestedMarshaler{ + Value: &StructWithMarshaler{ + Value: 5, + }, + Value2: 10, + } + + data, err := s.MarshalJSON() + if err != nil { + t.Errorf("Can't marshal NestedMarshaler: %s", err) + } + + s2 := NestedMarshaler { + Value: &StructWithMarshaler{}, + } + + err = s2.UnmarshalJSON(data) + if err != nil { + t.Errorf("Can't unmarshal NestedMarshaler: %s", err) + } + + if !reflect.DeepEqual(s2, s) { + t.Errorf("easyjson.Unmarshal() = %#v; want %#v", s2, s) + } +} + func TestUnmarshalStructWithEmbeddedPtrStruct(t *testing.T) { var s = StructWithInterface{Field2: &EmbeddedStruct{}} var err error