fix: fixedpoint MarshalJson on inf

This commit is contained in:
zenix 2022-08-30 21:55:16 +09:00
parent 51d7c1b9ad
commit b52598d1ad
2 changed files with 11 additions and 1 deletions

View File

@ -242,6 +242,9 @@ func (v Value) MarshalYAML() (interface{}, error) {
}
func (v Value) MarshalJSON() ([]byte, error) {
if v.IsInf() {
return []byte("\"" + v.String() + "\""), nil
}
return []byte(v.FormatString(DefaultPrecision)), nil
}
@ -446,6 +449,10 @@ func NewFromInt(val int64) Value {
return Value(val * DefaultPow)
}
func (a Value) IsInf() bool {
return a == PosInf || a == NegInf
}
func (a Value) MulExp(exp int) Value {
return Value(int64(float64(a) * math.Pow(10, float64(exp))))
}

View File

@ -1033,7 +1033,7 @@ func (x Value) Compare(y Value) int {
}
func (v Value) MarshalYAML() (interface{}, error) {
return v.FormatString(8), nil
return v.FormatString(8), nil
}
func (v *Value) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
@ -1061,6 +1061,9 @@ func (v *Value) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
// FIXME: should we limit to 8 prec?
func (v Value) MarshalJSON() ([]byte, error) {
if v.IsInf() {
return []byte("\"" + v.String() + "\""), nil
}
return []byte(v.FormatString(8)), nil
}