-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert BigInteger to string in a dictionary for response #533
Comments
This is a good question, thanks for sharing! Flask-Restless has changed quite a bit in version 1.0.0b1. Now you can more easily define a custom serializer class; see the serialization section of the documentation for more information. Specifically, you can override the If this is a feature request, presumably the client would receive the correct document, something like {
"id": 13851168950191161
} but the actual Javascript code that loads a Javascript object from that JSON returns an object with If so, now the question is whether it makes sense for Flask-Restless to always translate big integers to strings? Or should it be the responsibility of the client? PS You should be able to get the type of a SQLAlchemy field using the (non-public) helper function flask-restless/flask_restless/helpers.py Line 226 in ec4cd2c
|
Thank you very much @jfinkels ! I will upgrade Flask-Restless to 1.0.0b1 and implement a custom serializer. I would not always want to convert any Python integer to a string, and as you pointed, I will use or implement a similar function as Definitely, it is a client side issue that the a big integer is not handled properly, but it will be very difficult to solve it in a client side unless I send all columns types to Javascript and somehow add double-quotes only for a big integer attribute before parsing the response to create a Javascript object. It is not a Flask-Restless issue, but it could become a common issue for applications using Javascript in a client-side. I was hoping that Flask-Restless provide a simple (global) way to map a model data type to a type in a dictionary for a response if possible. Thank you again. It is very helpful. |
Well, the way it is implemented now is as follows. The As a workaround, you can use the strategy I suggested above. Another workaround might be to create a method or property in your SQLAlchemy model that just returns the integer value as a string, then tell Flask-Restless to include that property and exclude the integer property. |
In any case, your suggestion is interesting, too:
Let me think about how such a thing would be implemented, and if it even makes sense. |
It would be a question or enhancement request.
I was wondering if there is a way to convert Sqlachemy
BigInteger
to a string instead of a number in a dictionary to include in HTTP response.In my Sqlachemy model, I have a column with
BigInteger
-my_id = Column(BigInteger)
.For example, if the
my_id
attribute has a value 13851168950191161, and it will bemy_id: 13851168950191161
in a HTTP response. In Javascript, the number becomes 13851168950191160 which is greater than the Number.MAX_SAFE_INTEGER. So, I wanted to convert to a string if the column is BigInteger type, or wanted to find another workaround / solution.I thought about overriding
helpers.to_dict
, but it looks like I cannot detect Sqlachemy data type.Thank you.
The text was updated successfully, but these errors were encountered: