diff --git a/gen/decoder.go b/gen/decoder.go index f5d5305..b2b6bdc 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -86,7 +86,7 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i return err } -// returns true of the type t implements one of the custom unmarshaler interfaces +// returns true if the type t implements one of the custom unmarshaler interfaces func hasCustomUnmarshaler(t reflect.Type) bool { t = reflect.PtrTo(t) return t.Implements(reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem()) || @@ -295,16 +295,24 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+"}") case reflect.Interface: + fmt.Printf("//%v: %v", out, g.interfaceIsEasyjsonUnmarshaller(t)) if t.NumMethod() != 0 { - return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t) + if g.interfaceIsEasyjsonUnmarshaller(t) { + fmt.Fprintln(g.out, ws+out+".UnmarshalEasyJSON(in)") + } else if g.interfaceIsJsonUnmarshaller(t) { + fmt.Fprintln(g.out, ws+out+".UnmarshalJSON(in.Raw())") + } else { + return fmt.Errorf("interface type %v not supported: only interface{} and easyjson/json Unmarshaler are allowed", t) + } + } else { + fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {") + fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)") + fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {") + fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())") + fmt.Fprintln(g.out, ws+"} else {") + fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()") + fmt.Fprintln(g.out, ws+"}") } - fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {") - fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)") - fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {") - fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()") - fmt.Fprintln(g.out, ws+"}") default: return fmt.Errorf("don't know how to decode %v", t) } @@ -312,6 +320,14 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field } +func (g *Generator) interfaceIsEasyjsonUnmarshaller(t reflect.Type) bool { + return t.Implements(reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem()) +} + +func (g *Generator) interfaceIsJsonUnmarshaller(t reflect.Type) bool { + return t.Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) +} + func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) error { jsonName := g.fieldNamer.GetJSONFieldName(t, f) tags := parseFieldTags(f) diff --git a/gen/encoder.go b/gen/encoder.go index 62fd4d3..f9650e7 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -113,7 +113,7 @@ func (g *Generator) genTypeEncoder(t reflect.Type, in string, tags fieldTags, in return err } -// returns true of the type t implements one of the custom marshaler interfaces +// returns true if the type t implements one of the custom marshaler interfaces func hasCustomMarshaler(t reflect.Type) bool { t = reflect.PtrTo(t) return t.Implements(reflect.TypeOf((*easyjson.Marshaler)(nil)).Elem()) || @@ -260,7 +260,17 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT case reflect.Interface: if t.NumMethod() != 0 { - return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t) + if g.interfaceIsEasyjsonMarshaller(t) { + fmt.Fprintln(g.out, ws+in+".MarshalEasyJSON(out)") + } else if g.interfaceIsJSONMarshaller(t) { + 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 {") + fmt.Fprintln(g.out, ws+in+".MarshalJSON(out)") + fmt.Fprintln(g.out, ws+"}") + } else { + return fmt.Errorf("interface type %v not supported: only interface{} and interfaces that implement json or easyjson Marshaling are allowed", t) + } } fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") @@ -269,13 +279,20 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT 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) } return nil } +func (g *Generator) interfaceIsEasyjsonMarshaller(t reflect.Type) bool { + return t.Implements(reflect.TypeOf((*easyjson.Marshaler)(nil)).Elem()) +} + +func (g *Generator) interfaceIsJSONMarshaller(t reflect.Type) bool { + return t.Implements(reflect.TypeOf((*json.Marshaler)(nil)).Elem()) +} + func (g *Generator) notEmptyCheck(t reflect.Type, v string) string { optionalIface := reflect.TypeOf((*easyjson.Optional)(nil)).Elem() if reflect.PtrTo(t).Implements(optionalIface) {