mirror of https://github.com/jackc/pgx.git
Add UnmarshalJSON to a few types
parent
f71bf5db91
commit
63f58fd32e
|
@ -3,6 +3,7 @@ package pgtype
|
|||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
|
@ -198,3 +199,15 @@ func (src *Int4) MarshalJSON() ([]byte, error) {
|
|||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
func (dst *Int4) UnmarshalJSON(b []byte) error {
|
||||
var n int32
|
||||
err := json.Unmarshal(b, &n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int4{Int: n, Status: Present}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -149,3 +149,15 @@ func (src *Text) MarshalJSON() ([]byte, error) {
|
|||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
func (dst *Text) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Text{String: s, Status: Present}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -52,3 +52,7 @@ func (src *Varchar) Value() (driver.Value, error) {
|
|||
func (src *Varchar) MarshalJSON() ([]byte, error) {
|
||||
return (*Text)(src).MarshalJSON()
|
||||
}
|
||||
|
||||
func (dst *Varchar) UnmarshalJSON(b []byte) error {
|
||||
return (*Text)(dst).UnmarshalJSON(b)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue