将JSON中的大整数转换为浮点数的幂。

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

Marshalling jSON big ints turn into floats to the power

问题

我有这段数据:

productID, err := products.Insert(map[string]interface{}{
    "Properties": map[string]interface{}{
        strconv.Itoa(propertyNameID): map[string]string{
            "en": "Jeans Jersey",
            "nl": "Broek Jersey",
        },
        strconv.Itoa(propertyColorID): propertyOptionRedID,
    },
    "Type":        productTypeID,
    "Propertyset": propertysetID,
    "Active":      true,
    "EAN13":       "1234567890123"})

所有的***ID变量的类型都是int。不幸的是,当我进行普通的编组时:

{  
    "Active":true,
    "EAN13":"1234567890123",
    "Properties":{  
        "2286408386526632249":{  
            "en":"Jeans Jersey",
            "nl":"Broek Jersey"
        },
        "4750062295175300168":7.908474319828591e+18
    },
    "Propertyset":8.882218269088507e+18,
    "Type":7.185126253999425e+18
}

... 一些整数被转换为幂的float类型。

Itoa仍然只是一些测试,因为编组器无法处理map[int]interface{}(具有整数索引值的列表)。我只是不明白为什么int值会被更改为它们的"display"值,而不是它们的纯值。

更新: 我尝试了使用map[string]int和只有一个条目的"Properties"。结果仍然相同 将JSON中的大整数转换为浮点数的幂。

英文:

I have this piece of data:

productID, err := products.Insert(map[string]interface{}{
	"Properties": map[string]interface{}{
		strconv.Itoa(propertyNameID): map[string]string{
			"en": "Jeans Jersey",
			"nl": "Broek Jersey",
		},
		strconv.Itoa(propertyColorID): propertyOptionRedID,
	},
	"Type":        productTypeID,
	"Propertyset": propertysetID,
	"Active":      true,
	"EAN13":       "1234567890123"})

All the ***ID variables are of type int. Sadly when I do a normal marshal:

{  
    "Active":true,
    "EAN13":"1234567890123",
    "Properties":{  
        "2286408386526632249":{  
            "en":"Jeans Jersey",
            "nl":"Broek Jersey"
        },
        "4750062295175300168":7.908474319828591e+18
    },
    "Propertyset":8.882218269088507e+18,
    "Type":7.185126253999425e+18
}

... the some ints are transformed into float type to the power.

The Itoa are still just some tests tho, because the marshaller can't do map[int]interface{} (lists with index-values as integers). I just don't understand why the int values get changed to their "display"-value, instead of their pure value.

Update: I tried "Properties" with map[string]int and just one entry. Still the same result 将JSON中的大整数转换为浮点数的幂。

答案1

得分: 3

你可以使用string标签将int64类型的值转换为字符串,以避免在JSON中将其转换为float64类型。

type T struct {
    Val int64 `json:"val,string"`
}
t := T{Val: math.MaxInt64}
j, _ := json.Marshal(t)
fmt.Println(string(j))
// {"val":"9223372036854775807"}

以上代码将输出{"val":"9223372036854775807"}

英文:

You can marshal an int64 as a string in json to avoid the conversion to a float64 by using the string tag

type T struct {
	Val int64 `json:"val,string"`
}
t := T{Val: math.MaxInt64}
j, _ := json.Marshal(t)
fmt.Println(string(j))
// {"val":"9223372036854775807"}

huangapple
  • 本文由 发表于 2015年3月25日 03:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/29241443.html
匿名

发表评论

匿名网友

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

确定