implement json.Marshaler and json.Unmarshaler for Float4, Float8
This commit is contained in:
committed by
Jack Christensen
parent
cf6ef75f91
commit
d3fb6e00da
@@ -3,6 +3,7 @@ package pgtype
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
@@ -65,6 +66,29 @@ func (f Float4) Value() (driver.Value, error) {
|
||||
return float64(f.Float32), nil
|
||||
}
|
||||
|
||||
func (f Float4) MarshalJSON() ([]byte, error) {
|
||||
if !f.Valid {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return json.Marshal(f.Float32)
|
||||
}
|
||||
|
||||
func (f *Float4) UnmarshalJSON(b []byte) error {
|
||||
var n *float32
|
||||
err := json.Unmarshal(b, &n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if n == nil {
|
||||
*f = Float4{}
|
||||
} else {
|
||||
*f = Float4{Float32: *n, Valid: true}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Float4Codec struct{}
|
||||
|
||||
func (Float4Codec) FormatSupported(format int16) bool {
|
||||
|
||||
Reference in New Issue
Block a user