add UnmarshalJSON interface support for package gconv

This commit is contained in:
John Guo
2021-08-17 16:30:47 +08:00
parent ad6c4f5245
commit be2fcf7f57
2 changed files with 15 additions and 0 deletions

View File

@ -90,6 +90,12 @@ type apiUnmarshalText interface {
UnmarshalText(text []byte) error
}
// apiUnmarshalText is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface apiUnmarshalJSON.
type apiUnmarshalJSON interface {
UnmarshalJSON(b []byte) error
}
// apiSet is the interface for custom value assignment.
type apiSet interface {
Set(value interface{}) (old interface{})

View File

@ -355,6 +355,15 @@ func bindVarToReflectValueWithInterfaceCheck(reflectValue reflect.Value, value i
return v.UnmarshalText(b), ok
}
}
// UnmarshalJSON.
if v, ok := pointer.(apiUnmarshalJSON); ok {
if s, ok := value.(string); ok {
return v.UnmarshalJSON([]byte(s)), ok
}
if b, ok := value.([]byte); ok {
return v.UnmarshalJSON(b), ok
}
}
if v, ok := pointer.(apiSet); ok {
v.Set(value)
return nil, ok