2
0

Add UnmarshalJSON to a few types

This commit is contained in:
Jack Christensen
2017-09-29 15:25:53 -05:00
parent f71bf5db91
commit 63f58fd32e
3 changed files with 29 additions and 0 deletions
+13
View File
@@ -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
}
+12
View File
@@ -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
}
+4
View File
@@ -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)
}