Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: TypeError: Object of type 'bytes' is not JSON serializable

### INFO
**Python Version**: `3.6.7 (default, Oct 21 2018, 08:08:16) [GCC 8.2.0]`
**Operating System**: `Linux-4.9.35-v7+-armv7l-with-debian-buster-sid`
**Locale**: `UTF-8`
**Branch**: [feature/py3_2](../tree/feature/py3_2)
**Database**: `44.12`
**Commit**: pymedusa/Medusa@523cd325297cb7cabf9ddbf3b83c8571a114419b
**Link to Log**: https://gist.github.com/88f05b353a8aad7dba465d111ef96285
### ERROR
<pre>
2018-12-12 00:00:09 ERROR    SNATCHQUEUE-SNATCH-48552 :: [523cd32] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/home/pi/Medusa/<a href="../blob/523cd325297cb7cabf9ddbf3b83c8571a114419b/medusa/clients/torrent/generic.py#L263">medusa/clients/torrent/generic.py</a>", line 263, in send_torrent
    r_code = self._add_torrent_file(result)
  File "/home/pi/Medusa/<a href="../blob/523cd325297cb7cabf9ddbf3b83c8571a114419b/medusa/clients/torrent/transmission_client.py#L121">medusa/clients/torrent/transmission_client.py</a>", line 121, in _add_torrent_file
    'method': 'torrent-add',
  File "/usr/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:
Should be fixed in the branch, please update.
### INFO
**Python Version**: `3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]`
**Operating System**: `Windows-10-10.0.17763-SP0`
**Locale**: `cp1252`
**Branch**: [develop](../tree/develop)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@d588a2c92bb007d4346e5f46811f896775efb3be
**Link to Log**: **No Log available**
### ERROR
<pre>
2019-02-26 17:53:10 ERROR    Thread_10 :: [d588a2c] Exception generated: Object of type bytes is not JSON serializable
Traceback (most recent call last):
  File "D:\Medusa\<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/server/web/core/base.py#L262">medusa\server\web\core\base.py</a>", line 262, in async_call
    result = function(**kwargs)
  File "D:\Medusa\<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/server/web/home/add_shows.py#L514">medusa\server\web\home\add_shows.py</a>", line 514, in addNewShow
    return finishAddShow()
  File "D:\Medusa\<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/server/web/home/add_shows.py#L404">medusa\server\web\home\add_shows.py</a>", line 404, in finishAddShow
    for i, cur_dir in enumerate(other_shows)
  File "D:\Medusa\<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/server/web/home/add_shows.py#L52">medusa\server\web\home\add_shows.py</a>", line 52, in json_response
    'params': params or [],
  File "D:\Python37\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "D:\Python37\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "D:\Python37\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "D:\Python37\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---

Details:
This exception happens when clicking Add Show when adding multiple existing shows from a TV root folder.

The Add Show button appears unresponsive and hung, and this error is generated in the logs.  The single show is added to the list, but the UI doesn't advance to the next show in the list.

_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Resolving patch:
diff --git a/medusa/server/web/home/add_shows.py b/medusa/server/web/home/add_shows.py
--- a/medusa/server/web/home/add_shows.py
+++ b/medusa/server/web/home/add_shows.py
@@ -53,6 +53,14 @@ def json_response(result=True, message=None, redirect=None, params=None):
     })
 
 
+def decode_shows(names):
+    """Decode show names to UTF-8."""
+    return [
+        name.decode('utf-8') if not isinstance(name, text_type) else name
+        for name in names
+    ]
+
+
 @route('/addShows(/?.*)')
 class HomeAddShows(Home):
     def __init__(self, *args, **kwargs):
@@ -96,14 +104,9 @@ def newShow(self, show_to_add=None, other_shows=None, search_string=None):
         elif not isinstance(other_shows, list):
             other_shows = [other_shows]
 
-        other_shows = [
-            x.decode('utf-8') if not isinstance(x, text_type) else x
-            for x in other_shows
-        ]
-
+        other_shows = decode_shows(other_shows)
         provided_indexer_id = int(indexer_id or 0)
         provided_indexer_name = show_name
-
         provided_indexer = int(indexer or app.INDEXER_DEFAULT)
 
         return t.render(
@@ -391,6 +394,8 @@ def addNewShow(self, whichSeries=None, indexer_lang=None, rootDir=None, defaultS
         elif not isinstance(other_shows, list):
             other_shows = [other_shows]
 
+        other_shows = decode_shows(other_shows)
+
         def finishAddShow():
             # if there are no extra shows then go home
             if not other_shows:
@@ -542,11 +547,7 @@ def addExistingShows(self, shows_to_add=None, promptForSettings=None):
         elif not isinstance(shows_to_add, list):
             shows_to_add = [shows_to_add]
 
-        shows_to_add = [
-            x.decode('utf-8') if not isinstance(x, text_type) else x
-            for x in shows_to_add
-        ]
-
+        shows_to_add = decode_shows(shows_to_add)
         prompt_for_settings = config.checkbox_to_value(prompt_for_settings)
 
         indexer_id_given = []


Test patch: