jsonpb,为什么将int64解码为json,结果是字符串。像int64 str = 10-> str:“ 10”

时间:2018-12-24 09:24:50

标签: json go

//code:630

//jsonpb, why int64 -> json is string. like 10-->"10"

//https://github.com/golang/protobuf/blob/master/jsonpb/jsonpb.go

// Default handling defers to the encoding/json library.
b, err := json.Marshal(v.Interface())
if err != nil {
    return err
}
needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
if needToQuote {
    out.write(`"`)
}
out.write(string(b))
if needToQuote {
    out.write(`"`)
}

问题:

为什么要在值后加上“ \'”?

1 个答案:

答案 0 :(得分:0)

因为用javascript表示整数意味着最大整数是(2的53的幂)-1(请参见https://stackoverflow.com/a/307200/1153938

int64中的最大整数大于该整数,因此,为了保护免受较大整数的侵害,库会改用一串数字

由于对javascript数字进行了签名,对于大的负数也是如此

相关问题