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

\n in a JSON string is not valid #140

Closed
3 tasks done
nagyrobi opened this issue Apr 16, 2021 · 4 comments
Closed
3 tasks done

\n in a JSON string is not valid #140

nagyrobi opened this issue Apr 16, 2021 · 4 comments
Assignees
Labels
bug Something isn't working done The underlying issue has been fixed

Comments

@nagyrobi
Copy link
Collaborator

Perform all steps below and tick them with [x]

  • Check the related part of the Documentation
  • Update openHASP to the latest version
  • Reproduce the issue and describe all steps

Describe the bug

openHASP returns the state of dropdowns and rollers containing unescaped newline (\n) character.
This is invalid and causes an exception in Home Assistant.

To Reproduce

Using dropdowns to list the sources and sound modes of a media player.
This is the related starter config loaded config.jsonl from flash:

{"page":6,"id":5,"obj":"dropdown","x":5,"y":128,"w":120,"h":30,"options":"Source1\nSource2\nSource3"}
{"page":6,"id":6,"obj":"dropdown","x":130,"y":128,"w":75,"h":30,"options":"Jazz\nPop\nRock"}

The objects get updated correctly by the custom component using templates generating lists separated by newline character:

      - obj: "p6b5" # sources list
        properties:
          "options": >
            {% if not (is_state('media_player.hang_up2stream','unavailable')) %}{{"(nincs forrás)\n"|e}}{%for source in state_attr('media_player.hang_up2stream','source_list')%}{{source+"\n"|e}}{%-if not loop.last%}{%-endif%}{%-endfor%}{% endif %}

      - obj: "p6b6" # sound modes list
        properties:
          "options": >
            {% if not (is_state('media_player.hang_up2stream','unavailable')) %}{%for soundmode in state_attr('media_player.hang_up2stream','sound_mode_list')%}{{soundmode+"\n"|e}}{%-if not loop.last%}{%-endif%}{%-endfor%}{% endif %}

The objects get populated correctly with the options, however when the plate returns them in MQTT state topics hasp/plate/state/p6b5 and hasp/plate/state/p6b6 Home Assistant registers error message json.decoder.JSONDecodeError: Invalid control character at the location of the first newline character:

2021-04-16 17:37:23 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'hasp/plate/state/p6b5': '{"options":"(nincs forrás)
3,5 Jack
Bluetooth
USB
PC Audio
Jazzy
Jazzy Robi
Rádió 1
Retró
Sláger
Best FM
Forrás
Manna
Info Rádió
Jazzy Soul
Jazzy Grrove
Jazzy Cool
Petőfi
SpotifyPlayer
Karácsony
Kossuth
Impuls
SubasioPiu"}'
Traceback (most recent call last):
  File "/config/custom_components/openhasp/__init__.py", line 559, in message_received
    message = HASP_EVENT_SCHEMA(json.loads(msg.payload))
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 27 (char 26)

2021-04-16 17:37:23 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'hasp/plate/state/p6b6': '{"options":"Classic
Jazz
Normal
Pop
Vocal"}'
Traceback (most recent call last):
  File "/config/custom_components/openhasp/__init__.py", line 559, in message_received
    message = HASP_EVENT_SCHEMA(json.loads(msg.payload))
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 20 (char 19)

2021-04-16 17:37:25 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'hasp/plate/state/p6b5': '{"options":"(nincs forrás)
3,5 Jack
Bluetooth
USB
PC Audio
Jazzy
Jazzy Robi
Rádió 1
Retró
Sláger
Best FM
Forrás
Manna
Info Rádió
Jazzy Soul
Jazzy Grrove
Jazzy Cool
Petőfi
SpotifyPlayer
Karácsony
Kossuth
Impuls
SubasioPiu"}'
Traceback (most recent call last):
  File "/config/custom_components/openhasp/__init__.py", line 559, in message_received
    message = HASP_EVENT_SCHEMA(json.loads(msg.payload))
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 27 (char 26)

2021-04-16 17:37:25 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'hasp/plate/state/p6b6': '{"options":"Classic
Jazz
Normal
Pop
Vocal"}'
Traceback (most recent call last):
  File "/config/custom_components/openhasp/__init__.py", line 559, in message_received
    message = HASP_EVENT_SCHEMA(json.loads(msg.payload))
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 20 (char 19)

Expected behavior

Jsons generated by the plate in MQTT payloads need to be checked not to contain invalid characters.

Screenshots or video

The plate works fine otherwise:
kép

@nagyrobi nagyrobi added the bug Something isn't working label Apr 16, 2021
@dgomes
Copy link
Collaborator

dgomes commented Apr 16, 2021

\n should be escaped, or better yet, replaced with ","

@fvanroie
Copy link
Collaborator

fvanroie commented Apr 16, 2021

I was aware that quotes in the string posed an issue, apparently there's more characters that need special treatment.
Escaping strings is always though, replacing characters is also problematic because you can have a list that already contains comma's and gives a different result when the object is de-serialized again.

The best solution is to run all returned JSON objects containing strings through the ArduinoJson encoder.
There will be some overhead, but at least the result is guaranteed to be json compliant without needing much code.

@fvanroie fvanroie self-assigned this Apr 16, 2021
@fvanroie
Copy link
Collaborator

fvanroie commented Apr 16, 2021

Fix pushed to master

@fvanroie fvanroie added the done The underlying issue has been fixed label Apr 16, 2021
@nagyrobi
Copy link
Collaborator Author

Fix works thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working done The underlying issue has been fixed
Projects
None yet
Development

No branches or pull requests

3 participants