Sfoglia il codice sorgente

Adding encodings to decodeJSON

Andrej 1 anno fa
parent
commit
d8e6466211
1 ha cambiato i file con 9 aggiunte e 6 eliminazioni
  1. 9 6
      orthancDatabaseBrowser.py

+ 9 - 6
orthancDatabaseBrowser.py

@@ -4,12 +4,15 @@ import chardet
 import io
 
 def extractJSON(data):
-    encoding=chardet.detect(data)["encoding"]
-    try:
-    	return json.loads(data.decode(encoding))
-    except UnicodeDecodeError:
-      print(f'Failed to decode with [{encoding}]: {data}')
-      return None
+   encoding=chardet.detect(data)["encoding"]
+   #try with a set of encodings to maximize probability of success
+   encodings=[encoding,'utf_8']
+   for x in encodings:
+      try:
+         return json.loads(data.decode(encoding))
+      except UnicodeDecodeError:
+         print(f'Failed to decode with [{encoding}]: {data}')
+   return None
 
 
 class orthancDB: