Skip to content
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

Fix JSON encoding of decimal.Decimal #69

Merged
merged 1 commit into from
Feb 19, 2020
Merged

Fix JSON encoding of decimal.Decimal #69

merged 1 commit into from
Feb 19, 2020

Conversation

OrangeTux
Copy link
Contributor

Version 0.6.0 forces validation of Calls before they are send to the
other end. The validation of call.SetChargingProfilePayload fails with
a TypeError. The charging profile contains a value of type
decimal.Decimal and this value cannot be serialized to JSON. The
problem can be demonstrated like this:

>>> import decimal
>>> import json
>>> >>> json.dumps(decimal.Decimal(3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable

This bug has been fixed by creating a custom enconder for
decimal.Decimal.

Fixes: #68

Version `0.6.0` forces validation of `Call`s before the are send to the
other end. The validation of `call.SetChargingProfilePayload` fails with
a TypeError. The charging profile contains a value of type
`decimal.Decimal` and this value cannot be serialized to JSON. The
problem can be demonstrated like this:

	>>> import decimal
	>>> import json
	>>> >>> json.dumps(decimal.Decimal(3))
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/__init__.py", line 231, in dumps
	    return _default_encoder.encode(obj)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 199, in encode
	    chunks = self.iterencode(o, _one_shot=True)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 257, in iterencode
	    return _iterencode(o, 0)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 179, in default
	    raise TypeError(f'Object of type {o.__class__.__name__} '
	TypeError: Object of type Decimal is not JSON serializable

This bug has been fixed by creating a custom enconder for
`decimal.Decimal`.

Fixes: #68
@OrangeTux OrangeTux requested review from proelke and tropxy February 19, 2020 09:58
@ghost
Copy link

ghost commented Feb 19, 2020

DeepCode's analysis on #bc13ea found:

  • 0 critical issues. ⚠️ 0 warnings and 1 minor issue. ✔️ 0 issues were fixed.

💬 This comment has been generated by the DeepCode bot, installed by the owner of the repository. The DeepCode bot protects your repository by detecting and commenting on security vulnerabilities or other critical issues.


☺️ If you want to provide feedback on our bot, here is how to contact us.

Copy link
Collaborator

@proelke proelke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small comment but otherwise looks good.

@OrangeTux OrangeTux requested a review from Roger February 19, 2020 11:48
@OrangeTux OrangeTux merged commit e2c60a6 into master Feb 19, 2020
@OrangeTux OrangeTux deleted the parse-decimal branch February 19, 2020 12:29
ajmirsky pushed a commit to ajmirsky/ocpp that referenced this pull request Nov 26, 2024
Version `0.6.0` forces validation of `Call`s before the are send to the
other end. The validation of `call.SetChargingProfilePayload` fails with
a TypeError. The charging profile contains a value of type
`decimal.Decimal` and this value cannot be serialized to JSON. The
problem can be demonstrated like this:

	>>> import decimal
	>>> import json
	>>> >>> json.dumps(decimal.Decimal(3))
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/__init__.py", line 231, in dumps
	    return _default_encoder.encode(obj)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 199, in encode
	    chunks = self.iterencode(o, _one_shot=True)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 257, in iterencode
	    return _iterencode(o, 0)
	  File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 179, in default
	    raise TypeError(f'Object of type {o.__class__.__name__} '
	TypeError: Object of type Decimal is not JSON serializable

This bug has been fixed by creating a custom enconder for
`decimal.Decimal`.

Fixes: mobilityhouse#68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Sending a SetChargeProfile call fails with TypeError
3 participants