mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
add UnmarshalJSON interface support for package gconv
This commit is contained in:
@ -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{})
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user