Use MarshalJSON() instead of json.Marshal() for interface type if it implements json.Marshaler

This commit is contained in:
dd
2016-09-22 21:28:05 +03:00
committed by Dmitry Doroginin
parent e978125a7e
commit dff6a319fb
+5 -1
View File
@@ -180,7 +180,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
if t.NumMethod() != 0 {
return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t)
}
fmt.Fprintln(g.out, ws+"out.Raw(json.Marshal("+in+"))")
fmt.Fprintln(g.out, ws+"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)