mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
parent
3c692774ac
commit
b9ab2bce74
2 changed files with 26 additions and 8 deletions
|
@ -615,6 +615,10 @@ func (d Decimal) Truncate(precision int32) Decimal {
|
|||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
|
||||
if string(decimalBytes) == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
str, err := unquoteIfQuoted(decimalBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error decoding string '%s': %s", decimalBytes, err)
|
||||
|
|
|
@ -231,7 +231,6 @@ func TestNewFromFloatWithExponent(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func TestNewFromBigIntWithExponent(t *testing.T) {
|
||||
type Inp struct {
|
||||
val *big.Int
|
||||
|
@ -299,6 +298,21 @@ func TestJSON(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJSONNull(t *testing.T) {
|
||||
var doc struct {
|
||||
Amount Decimal `json:"amount"`
|
||||
}
|
||||
docStr := `{"amount": null}`
|
||||
err := json.Unmarshal([]byte(docStr), &doc)
|
||||
if err != nil {
|
||||
t.Errorf("error unmarshaling %s: %v", docStr, err)
|
||||
} else if !doc.Amount.Equal(Zero) {
|
||||
t.Errorf("expected Zero, got %s (%s, %d)",
|
||||
doc.Amount.String(),
|
||||
doc.Amount.value.String(), doc.Amount.exp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadJSON(t *testing.T) {
|
||||
for _, testCase := range []string{
|
||||
"]o_o[",
|
||||
|
|
Loading…
Reference in a new issue