0

I have got my code working to serialise an object which has Decimal and store in JSON in my PostgreSQL utilising this post here , i am using Flask-SQLAlchemy so have created my own custom apply_driver_hacks method to pass in a json_serializer which will serialise Decimal objects - so all good so far.

However on pulling the data back out , the deserializer loads these values as float. I've searched high and low but can't seem to find any way to reload as Decimal - i can pass in a custom json_deserializer as an option to Flask-SQLAlchemy but cant find a way to pass in any value such as use_decimal=True which is what I would use if using simplejson to load up Json with Decimal .

Any help greatly appreciated !

1
  • @IljaEverilä - Thank you very much! - that does indeed work and solves my problem which just a couple of lines of code, really appreciated for the insight! Commented May 25, 2018 at 10:49

1 Answer 1

1

Pass a callable that passes parse_float=decimal.Decimal to json.loads() as json_deserializer. A simple function such as

def json_deserializer(*args, **kwgs):
    return json.loads(*args, parse_float=decimal.Decimal, **kwgs)

should do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.