问题
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