forked from shopspring/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_json.go
52 lines (46 loc) · 1.58 KB
/
decimal_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package decimal
import "fmt"
// MarshalJSONWithoutQuotes should be set to true if you want the decimal to
// be JSON marshaled as a number, instead of as a string.
// WARNING: this is dangerous for decimals with many digits, since many JSON
// unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754
// double-precision floating point numbers, which means you can potentially
// silently lose precision.
var MarshalJSONWithoutQuotes = false
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
switch string(decimalBytes) {
case "null":
// TODO should this set to zero value or nil?
return nil
case `""`:
// Set to zero value
// Allow enter an empty string which is common if you work with web apps or json apis.
// HTML typically uses a string based input which is the best representation for our Decimal.
// The empty value for that is an empty string.
//
// Make sure we do not return an error when reading empty strings. We interpret them as a zero.
*d = Decimal{}
return nil
}
str, err := unquoteIfQuoted(decimalBytes)
if err != nil {
return fmt.Errorf("error decoding string '%s': %s", decimalBytes, err)
}
decimal, err := NewFromString(str)
*d = decimal
if err != nil {
return fmt.Errorf("error decoding string '%s': %s", str, err)
}
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (d Decimal) MarshalJSON() ([]byte, error) {
var str string
if MarshalJSONWithoutQuotes {
str = d.String()
} else {
str = `"` + d.String() + `"`
}
return []byte(str), nil
}