3
0

Properly encode value with (rdbms drivers) TypeJSON

This commit is contained in:
Denis Arh
2022-07-21 08:31:57 +02:00
parent c99095eba6
commit c2202f2739

View File

@@ -3,6 +3,7 @@ package drivers
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
@@ -243,7 +244,19 @@ func (t *TypeJSON) Decode(raw any) (any, bool, error) {
}
func (t *TypeJSON) Encode(val any) (driver.Value, error) {
return val, nil
switch c := val.(type) {
case driver.Valuer:
// does the value type know how to encode itself for the DB?
return c.Value()
case json.Marshaler:
// does the value type know how to encode itself as JSON?
return c.MarshalJSON()
default:
// Last reasort - just encode with JSON pkg
return json.Marshal(val)
}
}
func (t *TypeBlob) Decode(raw any) (any, bool, error) {