make sure simplejson always returns unicode

This commit is contained in:
Damien Elmes 2013-11-10 04:57:15 +09:00
parent 3dbc6fa0dd
commit db77c8c80b

View file

@ -3,12 +3,24 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import division from __future__ import division
import re, os, random, time, math, htmlentitydefs, subprocess, \ import re
tempfile, shutil, string, httplib2, sys, locale import os
import random
import time
import math
import htmlentitydefs
import subprocess
import tempfile
import shutil
import string
import sys
import locale
from hashlib import sha1 from hashlib import sha1
from anki.lang import _, ngettext
import platform import platform
from anki.lang import _, ngettext
if sys.version_info[1] < 5: if sys.version_info[1] < 5:
def format_string(a, b): def format_string(a, b):
return a % b return a % b
@ -16,6 +28,14 @@ if sys.version_info[1] < 5:
try: try:
import simplejson as json import simplejson as json
# make sure simplejson's loads() always returns unicode
# we don't try to support .load()
origLoads = json.loads
def loads(s, *args, **kwargs):
if not isinstance(s, unicode):
s = unicode(s, "utf8")
return origLoads(s, *args, **kwargs)
json.loads = loads
except ImportError: except ImportError:
import json import json