Add MarshalJSON to a few types
This commit is contained in:
@@ -195,3 +195,16 @@ func (src Int2) Value() (driver.Value, error) {
|
||||
return nil, errUndefined
|
||||
}
|
||||
}
|
||||
|
||||
func (src Int2) MarshalJSON() ([]byte, error) {
|
||||
switch src.Status {
|
||||
case Present:
|
||||
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
|
||||
case Null:
|
||||
return []byte("null"), nil
|
||||
case Undefined:
|
||||
return []byte("undefined"), nil
|
||||
}
|
||||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
@@ -186,3 +186,16 @@ func (src Int4) Value() (driver.Value, error) {
|
||||
return nil, errUndefined
|
||||
}
|
||||
}
|
||||
|
||||
func (src Int4) MarshalJSON() ([]byte, error) {
|
||||
switch src.Status {
|
||||
case Present:
|
||||
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
|
||||
case Null:
|
||||
return []byte("null"), nil
|
||||
case Undefined:
|
||||
return []byte("undefined"), nil
|
||||
}
|
||||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
@@ -172,3 +172,16 @@ func (src Int8) Value() (driver.Value, error) {
|
||||
return nil, errUndefined
|
||||
}
|
||||
}
|
||||
|
||||
func (src Int8) MarshalJSON() ([]byte, error) {
|
||||
switch src.Status {
|
||||
case Present:
|
||||
return []byte(strconv.FormatInt(src.Int, 10)), nil
|
||||
case Null:
|
||||
return []byte("null"), nil
|
||||
case Undefined:
|
||||
return []byte("undefined"), nil
|
||||
}
|
||||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
@@ -129,6 +129,7 @@ type TextEncoder interface {
|
||||
}
|
||||
|
||||
var errUndefined = errors.New("cannot encode status undefined")
|
||||
var errBadStatus = errors.New("invalid status")
|
||||
|
||||
type DataType struct {
|
||||
Value Value
|
||||
|
||||
@@ -2,6 +2,7 @@ package pgtype
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
@@ -134,3 +135,16 @@ func (src Text) Value() (driver.Value, error) {
|
||||
return nil, errUndefined
|
||||
}
|
||||
}
|
||||
|
||||
func (src Text) MarshalJSON() ([]byte, error) {
|
||||
switch src.Status {
|
||||
case Present:
|
||||
return json.Marshal(src.String)
|
||||
case Null:
|
||||
return []byte("null"), nil
|
||||
case Undefined:
|
||||
return []byte("undefined"), nil
|
||||
}
|
||||
|
||||
return nil, errBadStatus
|
||||
}
|
||||
|
||||
@@ -49,3 +49,7 @@ func (dst *Varchar) Scan(src interface{}) error {
|
||||
func (src Varchar) Value() (driver.Value, error) {
|
||||
return (Text)(src).Value()
|
||||
}
|
||||
|
||||
func (src Varchar) MarshalJSON() ([]byte, error) {
|
||||
return (Text)(src).MarshalJSON()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user