I have a struct
which contains various currency values, in cents (1/100 USD):
type CurrencyValues struct {
v1 int `json:"v1,string"`
v2 int `json:"v2,string"`
}
I'd like to create a custom json Unmarshaller for currency values with thousand separators. These values are encoded as strings, with one or more thousand separators (,
), and possibly a decimal point (.
).
For this JSON {"v1": "10", "v2": "1,503.21"}
, I'd like to JSON Unmarshal a CurrencyValues{v1: 1000, v2: 150321}
.
Following a similar answer here: Golang: How to unmarshall both 0 and false as bool from JSON, I went ahead and created a custom type for my currency fields, which include a custom Unmarshalling function:
type ConvertibleCentValue int
func (cents *ConvertibleCentValue) UnmarshalJSON(data []byte) error {
asString := string(data)
// Remove thousands separators
asString = strings.Replace(asString, ",", "", -1)
// Parse to float, then convert dollars to cents
if floatVal, err := strconv.ParseFloat(asString, 32); err == nil {
*cents = ConvertibleCentValue(int(floatVal * 100.0))
return nil
} else {
return err
}
}
However, when writing unit tests:
func Test_ConvertibleCentValue_Unmarshal(t *testing.T) {
var c ConvertibleCentValue
assert.Nil(t, json.Unmarshal([]byte("1,500"), &c))
assert.Equal(t, 150000, int(c))
}
I encounter this error:
Error: Expected nil, but got: &json.SyntaxError{msg:"invalid character ',' after top-level value", Offset:2}
What am I missing here?