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

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

Custom JSON Unmarshalling for string-encoded number

问题

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

type CurrencyValues struct {
   v1 int `json:"v1,string"`
   v2 int `json:"v2,string"`
}

我想为具有千位分隔符的货币值创建一个自定义的 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,我创建了一个自定义类型来表示我的货币字段,并包含一个自定义的反序列化函数:

type ConvertibleCentValue int

func (cents *ConvertibleCentValue) UnmarshalJSON(data []byte) error {
    asString := string(data)

    // 移除千位分隔符
    asString = strings.Replace(asString, ",", "", -1)

    // 解析为浮点数,然后将美元转换为分
    if floatVal, err := strconv.ParseFloat(asString, 32); err == nil {
        *cents = ConvertibleCentValue(int(floatVal * 100.0))
        return nil
    } else {
        return err
    }
}

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

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))
}

我遇到了这个错误:

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

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

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

得分: 4

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

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

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

b := []byte(`1,500`)
var s string
err := json.Unmarshal(b, &s)
fmt.Println(s, err) // 打印错误。

b = []byte(`"1,500"`)
err = json.Unmarshal(b, &s)
fmt.Println(s, err) // 正常工作。

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

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.

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:

确定