Add UnmarshalJSON to pgtype.Int2

fixes https://github.com/jackc/pgtype/issues/153
non-blocking
Jack Christensen 2022-04-16 07:07:31 -05:00
parent ccb207cba5
commit 25558de3bd
1 changed files with 17 additions and 0 deletions

17
int2.go
View File

@ -3,6 +3,7 @@ package pgtype
import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"strconv"
@ -302,3 +303,19 @@ func (src Int2) MarshalJSON() ([]byte, error) {
return nil, errBadStatus
}
func (dst *Int2) UnmarshalJSON(b []byte) error {
var n *int16
err := json.Unmarshal(b, &n)
if err != nil {
return err
}
if n == nil {
*dst = Int2{Status: Null}
} else {
*dst = Int2{Int: *n, Status: Present}
}
return nil
}