Skip to content

Commit 271dff7

Browse files
authored
Merge pull request #25 from dhalbert/ujson-to-json
import either json or ujson
2 parents 3efa8e7 + 0071359 commit 271dff7

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

adafruit_espatcontrol/adafruit_espatcontrol_requests.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ def text(self):
6262

6363
def json(self):
6464
"""The HTTP content, parsed into a json dictionary"""
65-
import ujson
66-
return ujson.loads(self.content)
65+
try:
66+
import json as json_module
67+
except ImportError:
68+
import ujson as json_module
69+
return json_module.loads(self.content)
6770

6871

6972
# pylint: disable=too-many-branches, too-many-statements, unused-argument, too-many-arguments, too-many-locals
@@ -115,8 +118,11 @@ def request(method, url, data=None, json=None, headers=None, stream=None):
115118
sock.write(b"\r\n")
116119
if json is not None:
117120
assert data is None
118-
import ujson
119-
data = ujson.dumps(json)
121+
try:
122+
import json as json_module
123+
except ImportError:
124+
import ujson as json_module
125+
data = json_module.dumps(json)
120126
sock.write(b"Content-Type: application/json\r\n")
121127
if data:
122128
sock.write(b"Content-Length: %d\r\n" % len(data))

examples/espatcontrol_quoteEPD.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
from digitalio import DigitalInOut
1212
from adafruit_espatcontrol import adafruit_espatcontrol
1313
from adafruit_espatcontrol import adafruit_espatcontrol_requests as requests
14-
import ujson
14+
try:
15+
import json as json_module
16+
except ImportError:
17+
import ujson as json_module
1518
from adafruit_epd.epd import Adafruit_EPD
1619
from adafruit_epd.il0373 import Adafruit_IL0373
1720

@@ -56,7 +59,7 @@ def get_value(response, location):
5659
"""Extract a value from a json object, based on the path in 'location'"""
5760
try:
5861
print("Parsing JSON response...", end='')
59-
json = ujson.loads(response)
62+
json = json_module.loads(response)
6063
print("parsed OK!")
6164
for x in location:
6265
json = json[x]

0 commit comments

Comments
 (0)