Added support for Decimal places in JSON marshall

This commit is contained in:
Carl Cudors 2018-10-14 17:31:30 +03:00
parent cd690d0c9e
commit cbaf07db27

View file

@ -52,6 +52,10 @@ var DivisionPrecision = 16
// silently lose precision. // silently lose precision.
var MarshalJSONWithoutQuotes = false var MarshalJSONWithoutQuotes = false
// If Set to True, returns rounded fixed-point string with places digits after the decimal point
var MarshalJSONWithDecimalPlaces = false
var MarshalJSONDecimalPlaces = 0
// Zero constant, to make computations faster. // Zero constant, to make computations faster.
var Zero = New(0, 1) var Zero = New(0, 1)
@ -931,11 +935,18 @@ func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (d Decimal) MarshalJSON() ([]byte, error) { func (d Decimal) MarshalJSON() ([]byte, error) {
var stringVal string
if MarshalJSONWithDecimalPlaces {
stringVal = d.StringFixed(MarshalJSONDecimalPlaces)
} else {
stringVal = d.String()
}
var str string var str string
if MarshalJSONWithoutQuotes { if MarshalJSONWithoutQuotes {
str = d.String() str = stringVal
} else { } else {
str = "\"" + d.String() + "\"" str = "\"" + stringVal + "\""
} }
return []byte(str), nil return []byte(str), nil
} }