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

Sending a SetChargeProfile call fails with TypeError #68

Closed
OrangeTux opened this issue Feb 18, 2020 · 1 comment · Fixed by #69
Closed

Sending a SetChargeProfile call fails with TypeError #68

OrangeTux opened this issue Feb 18, 2020 · 1 comment · Fixed by #69

Comments

@OrangeTux
Copy link
Contributor

Version 0.6.0 forces validation of Calls 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
@tropxy
Copy link
Collaborator

tropxy commented Feb 18, 2020

After a short research it seems we have 2 options:

  1. is to follow the suggestion in this python blog message that @OrangeTux found:
    https://bugs.python.org/issue16535#msg176158

  2. Add simplejson as dependency. simplejson supports natively Decimal serialization:
    https://simplejson.readthedocs.io/en/latest/

OrangeTux pushed a commit that referenced this issue Feb 19, 2020
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 added a commit that referenced this issue Feb 19, 2020
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
ajmirsky pushed a commit to ajmirsky/ocpp that referenced this issue 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 a pull request may close this issue.

2 participants