You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
json.Unmarshal parse number into float64 by default.
if the number is int64, the result is not correct.
It is suggested to use json.Number instead of float64.
However, lots of packages just use json.Unmarshal instead of explicitly specify json.Number.
e.g.
//golang.org/x/net/websocket
websocket.JSON.receive use json.Unmarshal to parse json object
Could encoding/json package use json.Number as the default number type?
The text was updated successfully, but these errors were encountered:
Representing int64 values as numbers in JSON is not very good idea. It won't work in javascript as well (and possibly other languages/packages). E.g. in Chrome:
@kostya-sh Thanks for your reply.
I just read that document and realise that int64 is not well supported in JSON.
In my project, int64 is used to represent 64 bits id. It is widely used.
I admit this is not an issue.
It would be better to support int64 by default. It is well supported in libraries on Android and iOS platform.
No, for compatibility reasons the default cannot be changed.
Note that when decoding into an int64, there is no problem. The float64 default only matters when decoding into an interface{} value, which is already a less-than-ideal way to use package json. Better to decode into a typed data structure.
Even for decoding into an interface{} value, the choice can be changed by calling json.Decoder's UseNumber method before decoding.
@rsc Thanks for your reply. You are right. There is no problem to decode into an int64. Only matters when decoding into an interface{} value. I recall that if decode the json data into map[string]interface{}, it would matter. But if decode it directly into a struct with int64 as its field, it would be fine.
json.Unmarshal parse number into float64 by default.
if the number is int64, the result is not correct.
It is suggested to use json.Number instead of float64.
However, lots of packages just use json.Unmarshal instead of explicitly specify json.Number.
e.g.
Could encoding/json package use json.Number as the default number type?
The text was updated successfully, but these errors were encountered: