简体   繁体   English

jsonpb,为什么要把int64解码成json,结果是字符串。 像 int64 str=10 -->str:"10"

[英]jsonpb, why decode int64 to json,the result is string. like int64 str=10 -->str:"10"

//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(`"`)
}

Question:题:

Why append "\\'" around the value?为什么在值周围附加“\\'”?

Because way that integers are represented in javascript means that the maximum integer is (2 to the power of 53)-1 (see https://stackoverflow.com/a/307200/1153938 )因为整数在javascript中表示的方式意味着最大整数是(2的53次方)-1(参见https://stackoverflow.com/a/307200/1153938

The biggest int from an int64 is larger than that, so in order to protect from the case of large ints, the library does a string of digits instead int64的最大 int 大int64值,因此为了防止出现大整数的情况,库会改为使用一串数字

As javascript numbers are signed the same goes for large negative numbers由于 javascript 数字是有符号的,因此对于大的负数也是如此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM