Skip to content

Commit 16a233d

Browse files
committed
Support fmt.Stringer for JSON
1 parent 954753e commit 16a233d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

json.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ func (j *JSON) Scan(value interface{}) error {
3535
return nil
3636
}
3737
var bytes []byte
38-
switch v := value.(type) {
39-
case []byte:
40-
if len(v) > 0 {
41-
bytes = make([]byte, len(v))
42-
copy(bytes, v)
38+
if s, ok := value.(fmt.Stringer); ok {
39+
bytes = []byte(s.String())
40+
} else {
41+
switch v := value.(type) {
42+
case []byte:
43+
if len(v) > 0 {
44+
bytes = make([]byte, len(v))
45+
copy(bytes, v)
46+
}
47+
case string:
48+
bytes = []byte(v)
49+
default:
50+
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
4351
}
44-
case string:
45-
bytes = []byte(v)
46-
default:
47-
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
4852
}
4953

5054
result := json.RawMessage(bytes)

0 commit comments

Comments
 (0)