IntegerField value gets converted to a string for some numbers

六月ゝ 毕业季﹏ 提交于 2019-12-24 11:02:26

问题


Having a Cloud Endpoints (ProtoRPC) message class with an integer field, e.g.

TestMsg(messages.Message):
  int_field = messages.IntegerField(1)

and a method:

@endpoints.method(VoidMessage, TestMsg)
def test_int_field():
  return TestMsg(int_field=1234567890123)

On local dev server JSON response correctly results in:

{ int_field: 1234567890123 }

Whereas in production the number gets converted to a string for some reason:

{ int_field: "1234567890123" }

For smaller numbers integers don't seem to be converted to strings though.

Is this expected behaviour? Anyone can repro? (In case it matters: I'm running this code in EU datacenters)


回答1:


I guess @proppy is right. Also, it clearly states in the discovery format that

A 32-bit signed integer ("integer" type, int32 format). It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)

and

A 32-bit unsigned integer ("integer" type, uint32 format). It has a minimum value of 0 and a maximum value of 4,294,967,295 (inclusive).

All other kinds of int/bigint/whatever values are repesented as "string" types with different formats. More info: https://developers.google.com/discovery/v1/type-format

So, 1234567890123 number cannot in fact be represented in "integer" type. It's just the dev server doesn't convert integers to strings automatically (like the production infrastructure does), and I didn't realize how big the number was while testing locally.

It turns out a team at Google is already working on making it consistent: https://code.google.com/p/googleappengine/issues/detail?id=9173



来源:https://stackoverflow.com/questions/15985069/integerfield-value-gets-converted-to-a-string-for-some-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!