mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Add checks for json/easyjson marshaler/unmarshaler interfaces
This commit is contained in:
+18
-8
@@ -259,15 +259,15 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
|
||||
|
||||
case reflect.Interface:
|
||||
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)
|
||||
}
|
||||
}
|
||||
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+out+" = in.Interface()")
|
||||
default:
|
||||
return fmt.Errorf("don't know how to decode %v", t)
|
||||
}
|
||||
@@ -275,6 +275,16 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
|
||||
|
||||
}
|
||||
|
||||
func (g *Generator) interfaceIsEasyjsonUnmarshaller(t reflect.Type) bool {
|
||||
unmarshalerType := reflect.TypeOf((*easyjson.Unmarshaler)(nil))
|
||||
return t.Implements(unmarshalerType)
|
||||
}
|
||||
|
||||
func (g *Generator) interfaceIsJsonUnmarshaller(t reflect.Type) bool {
|
||||
unmarshalerType := reflect.TypeOf((*json.Unmarshaler)(nil))
|
||||
return t.Implements(unmarshalerType)
|
||||
}
|
||||
|
||||
func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) error {
|
||||
jsonName := g.fieldNamer.GetJSONFieldName(t, f)
|
||||
tags := parseFieldTags(f)
|
||||
|
||||
+17
-8
@@ -243,22 +243,31 @@ 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+in+".MarshalJSON(out)")
|
||||
} else {
|
||||
return fmt.Errorf("interface type %v not supported: only interface{} was allowed", 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 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)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) interfaceIsEasyjsonMarshaller(t reflect.Type) bool {
|
||||
marshalerType := reflect.TypeOf((*easyjson.Marshaler)(nil))
|
||||
return t.Implements(marshalerType)
|
||||
}
|
||||
|
||||
func (g *Generator) interfaceIsJsonMarshaller(t reflect.Type) bool {
|
||||
marshalerType := reflect.TypeOf((*json.Marshaler)(nil))
|
||||
return t.Implements(marshalerType)
|
||||
}
|
||||
|
||||
func (g *Generator) notEmptyCheck(t reflect.Type, v string) string {
|
||||
optionalIface := reflect.TypeOf((*easyjson.Optional)(nil)).Elem()
|
||||
if reflect.PtrTo(t).Implements(optionalIface) {
|
||||
|
||||
Reference in New Issue
Block a user