Discussion:
[bottlepy] json output fails due to Decimal in dict
Henning Sprang
2015-08-06 10:54:51 UTC
Permalink
Hi,

I have some data that contains numbers as "Decimal" type instead of plain
int/float - and this makes the automatic json output fail due to a bug in
pythons default json implementation: https://bugs.python.org/issue16535

Being a bit new to bottle I wonder what would be the nicest option to solve
that - ideas so far:

* manually fix all my data to convert Decimal to int/float
* dumping the string myself in the bottle application method and setting
the content type manually to application/json
* use a different json encoder instead of standard python - that might
require to change the autojson output filter, but I don't see where to
start - the tutorial hints to a part of the documentation that I don't see
to find - it's also reported as a bug
https://github.com/bottlepy/bottle/issues/751 - if I get the info here I'd
like to fix that doc issue :)

The latter seems the preferable way - so pointers on where to start or
better ideas welcome ;)

Cheers and thanks in advance,
Henning
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Iuri
2015-08-06 11:10:37 UTC
Permalink
First of all, I don't think it is a bug. We don't have a standard way to
encode/decode Decimal.

Based on the #16535 python issue, simplejson works fine with Decimal, so
you can just install it and bottle will automatically use it.

Also, I think bottle can provide some obvious way to change JSON
encoder/decoder. For now, you must register another JSONPlugin, with your
custom json_dumps function (
https://github.com/bottlepy/bottle/blob/master/bottle.py#L1836).
Post by Henning Sprang
Hi,
I have some data that contains numbers as "Decimal" type instead of plain
int/float - and this makes the automatic json output fail due to a bug in
pythons default json implementation: https://bugs.python.org/issue16535
Being a bit new to bottle I wonder what would be the nicest option to
* manually fix all my data to convert Decimal to int/float
* dumping the string myself in the bottle application method and setting
the content type manually to application/json
* use a different json encoder instead of standard python - that might
require to change the autojson output filter, but I don't see where to
start - the tutorial hints to a part of the documentation that I don't see
to find - it's also reported as a bug
https://github.com/bottlepy/bottle/issues/751 - if I get the info here
I'd like to fix that doc issue :)
The latter seems the preferable way - so pointers on where to start or
better ideas welcome ;)
Cheers and thanks in advance,
Henning
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Henning Sprang
2015-08-06 14:38:26 UTC
Permalink
Hi Iuri,

Thanks for your reply!
Post by Iuri
First of all, I don't think it is a bug. We don't have a standard way to
encode/decode Decimal.
I didn't say it's a bug in bottle - but it's an issue caused by something
decribed in a bugreport in standard python :)

The bottle bug I was referring to seems to be a documentation issue where
something is pointed to that seems not to exist (further more detailed docs
about output filtering).
Post by Iuri
Based on the #16535 python issue, simplejson works fine with Decimal, so
you can just install it and bottle will automatically use it.
Cool, thanks, that worked!
(the project I'm working on was using an older version of bottle, but
upgrading to the latest did the trick!)
Post by Iuri
Also, I think bottle can provide some obvious way to change JSON
encoder/decoder. For now, you must register another JSONPlugin, with your
custom json_dumps function (
https://github.com/bottlepy/bottle/blob/master/bottle.py#L1836).
So, would it be correct to solve
https://github.com/bottlepy/bottle/issues/751 by pointing the reader of the
mentioned part of the documentation to the plugin developer guide instead
of the there mentioned tutorial on output filtering?
If so, I'll prepare a patch.

Cheers,
Henning
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...