douxuan4556 2016-01-26 13:54
浏览 100
已采纳

自定义JSON解组字符串编码的数字

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?

  • 写回答

1条回答 默认 最新

  • dongzha3058 2016-01-26 13:59
    关注

    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":

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

    Note the backticks. Here is a simplified example:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里