0

I have been trying to serialize a decimal value with a string key but so far i only managed to make a successful 1 decimal digit (like '16.1'). I tried both json and simplejson libraries, subclassing the encoder, etc....

This works:

jsonContent = json.dumps({"value": Decimal('15.4')})

This doesn't:

jsonContent = json.dumps({"value": Decimal('15.43')})

Next steps in my code is to encode the jsonContent variable even if i think it's not necessary as it returns the same string and send it through serial bus. I can't get why with one decimal digit it works and not with more...I also tried with round() and float which ends with the same result. Thank you by advance.

Dim17300
  • 75
  • 5
  • Strange, [_neither_ of those work for me](http://ideone.com/aoDFdl); both give `TypeError: [...] is not JSON serializable`. Did you do anything special before serializing 15.4? – Kevin Oct 26 '16 at 12:26
  • Actually i forgot to mention that i was changing the precision from 28 to 4 but it's not relevant to me. It does not fail with simplejson library. – Dim17300 Oct 26 '16 at 12:39
  • I was not able to reproduce this problem in Python 3.5 or Python 2.7 -- using simplejson always works, so I'm not sure what the problem is. – sytech Oct 26 '16 at 12:54
  • Also was able to successfully use the `json` module using the subclassed json encoder that's mentioned in [this answer](http://stackoverflow.com/a/3885198/5747944) to a similar question. – sytech Oct 26 '16 at 13:00
  • Simplejson was working, just my next at command which was not accepting enough input data...Sorry guys and thanks – Dim17300 Oct 26 '16 at 13:09

1 Answers1

0

Simplejson was working, just my next at command which was not accepting enough input data...Sorry guys and thanks

Dim17300
  • 75
  • 5