自定义JSON反序列化以处理字符串编码的数字。

huangapple go评论111阅读模式
英文:

Custom JSON Unmarshalling for string-encoded number

问题

我有一个包含各种货币值的struct,以分为单位(1/100 美元):

  1. type CurrencyValues struct {
  2. v1 int `json:"v1,string"`
  3. v2 int `json:"v2,string"`
  4. }

我想为具有千位分隔符的货币值创建一个自定义的 JSON 反序列化器。这些值被编码为字符串,其中包含一个或多个千位分隔符(,),可能还有一个小数点(.)。

对于这个 JSON { "v1": "10", "v2": "1,503.21" },我想要 JSON 反序列化为 CurrencyValues{v1: 1000, v2: 150321}

根据这里的一个类似答案:https://stackoverflow.com/questions/30856454/golang-how-to-unmarshall-both-0-and-false-as-bool-from-json,我创建了一个自定义类型来表示我的货币字段,并包含一个自定义的反序列化函数:

  1. type ConvertibleCentValue int
  2. func (cents *ConvertibleCentValue) UnmarshalJSON(data []byte) error {
  3. asString := string(data)
  4. // 移除千位分隔符
  5. asString = strings.Replace(asString, ",", "", -1)
  6. // 解析为浮点数,然后将美元转换为分
  7. if floatVal, err := strconv.ParseFloat(asString, 32); err == nil {
  8. *cents = ConvertibleCentValue(int(floatVal * 100.0))
  9. return nil
  10. } else {
  11. return err
  12. }
  13. }

然而,在编写单元测试时:

  1. func Test_ConvertibleCentValue_Unmarshal(t *testing.T) {
  2. var c ConvertibleCentValue
  3. assert.Nil(t, json.Unmarshal([]byte("1,500"), &c))
  4. assert.Equal(t, 150000, int(c))
  5. }

我遇到了这个错误:

  1. Error: Expected nil, but got: &json.SyntaxError{msg:"invalid character ',' after top-level value", Offset:2}

我在这里漏掉了什么?

英文:

I have a struct which contains various currency values, in cents (1/100 USD):

  1. type CurrencyValues struct {
  2. v1 int `json:"v1,string"`
  3. v2 int `json:"v2,string"`
  4. }

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: https://stackoverflow.com/questions/30856454/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:

  1. type ConvertibleCentValue int
  2. func (cents *ConvertibleCentValue) UnmarshalJSON(data []byte) error {
  3. asString := string(data)
  4. // Remove thousands separators
  5. asString = strings.Replace(asString, ",", "", -1)
  6. // Parse to float, then convert dollars to cents
  7. if floatVal, err := strconv.ParseFloat(asString, 32); err == nil {
  8. *cents = ConvertibleCentValue(int(floatVal * 100.0))
  9. return nil
  10. } else {
  11. return err
  12. }
  13. }

However, when writing unit tests:

  1. func Test_ConvertibleCentValue_Unmarshal(t *testing.T) {
  2. var c ConvertibleCentValue
  3. assert.Nil(t, json.Unmarshal([]byte("1,500"), &c))
  4. assert.Equal(t, 150000, int(c))
  5. }

I encounter this error:

  1. Error: Expected nil, but got: &json.SyntaxError{msg:"invalid character ',' after top-level value", Offset:2}

What am I missing here?

答案1

得分: 4

你正在尝试解析无效的JSON字符串1,500。我认为你的意思是解析JSON字符串"1,500"

  1. assert.Nil(t, json.Unmarshal([]byte(`"1,500"`), &c))

请注意反引号。这里是一个简化的示例:

  1. b := []byte(`1,500`)
  2. var s string
  3. err := json.Unmarshal(b, &s)
  4. fmt.Println(s, err) // 打印错误。
  5. b = []byte(`"1,500"`)
  6. err = json.Unmarshal(b, &s)
  7. fmt.Println(s, err) // 正常工作。
  8. Playground: http://play.golang.org/p/uwayOSgmTv.
英文:

You're trying to unmarshal the string 1,500 which is invalid in JSON. I think what you means is to unmarshal the JSON string "1,500":

  1. assert.Nil(t, json.Unmarshal([]byte(`"1,500"`), &c))

Note the backticks. Here is a simplified example:

  1. b := []byte(`1,500`)
  2. var s string
  3. err := json.Unmarshal(b, &s)
  4. fmt.Println(s, err) // Prints error.
  5. b = []byte(`"1,500"`)
  6. err = json.Unmarshal(b, &s)
  7. fmt.Println(s, err) // Works fine.

Playground: http://play.golang.org/p/uwayOSgmTv.

huangapple
  • 本文由 发表于 2016年1月26日 21:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/35015447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定