The Problem
I know that DynamoDB is working with Decimals for every number it saves. I'm using python with boto3 and for every int
I put in a table - it easily converts it to Decimal. The same isn't true for floats - when trying to put a float in a table, boto3 throws a TypeError (Float types are not supported. Use Decimal types instead.
).
Same problem happens the other way around - when querying a table and returning values, number types would be returned as Decimals as well, which is a problem by itself because Decimals are not serializable and can't be converted to json so easily (when I try to return a response with my flask-restx app, it fails to serialize the dict).
Unhelpful Solutions I've Found
One of the more common solutions I find online is to use a 3rd party lib (like simplejson
) that can serialize Decimals to dump the dict into string. But I can't find a real use for that, as my app should be returning a dict and not a string. I can serialize and then deserialize, but thats a bad solution for sure.
I've also seen solutions suggesting to implement a custom encoder, but it can't help either for the same reason.
What I'm Looking For
I have a flask-restx app that receives POST requests with json body that I want to save in a DynamoDB table as-is. If boto3 could parse the numbers to Decimals and vice versa on retrieving the data back, it could be perfect. Otherwise, maybe implementing a middleware that runs a moment before updating the table item and a moment after the querying data from the table could be the best solution.
Why I can't find any solid solution for such a common problem online? Am I missing something?
** Edit **
A conversation about the issue can be seen in boto3's repo:
https://github.com/boto/boto3/issues/665
And as it seems, sadly there isn't any good solution for that.