2
0

add UnmarshalJSON for pgtype Numeric

This commit is contained in:
Yumin Xia
2023-01-29 21:14:49 -08:00
committed by Jack Christensen
parent 384a581e99
commit 766d2bba4f
2 changed files with 82 additions and 0 deletions
+12
View File
@@ -240,6 +240,18 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
return n.numberTextBytes(), nil
}
func (n *Numeric) UnmarshalJSON(src []byte) error {
if bytes.Compare(src, []byte(`null`)) == 0 {
*n = Numeric{}
return nil
}
if bytes.Compare(src, []byte(`"NaN"`)) == 0 {
*n = Numeric{NaN: true, Valid: true}
return nil
}
return scanPlanTextAnyToNumericScanner{}.Scan(src, n)
}
// numberString returns a string of the number. undefined if NaN, infinite, or NULL
func (n Numeric) numberTextBytes() []byte {
intStr := n.Int.String()