Skip to content

import either json or ujson #25

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

Merged
merged 1 commit into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions adafruit_espatcontrol/adafruit_espatcontrol_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ def text(self):

def json(self):
"""The HTTP content, parsed into a json dictionary"""
import ujson
return ujson.loads(self.content)
try:
import json as json_module
except ImportError:
import ujson as json_module
return json_module.loads(self.content)


# pylint: disable=too-many-branches, too-many-statements, unused-argument, too-many-arguments, too-many-locals
Expand Down Expand Up @@ -115,8 +118,11 @@ def request(method, url, data=None, json=None, headers=None, stream=None):
sock.write(b"\r\n")
if json is not None:
assert data is None
import ujson
data = ujson.dumps(json)
try:
import json as json_module
except ImportError:
import ujson as json_module
data = json_module.dumps(json)
sock.write(b"Content-Type: application/json\r\n")
if data:
sock.write(b"Content-Length: %d\r\n" % len(data))
Expand Down
7 changes: 5 additions & 2 deletions examples/espatcontrol_quoteEPD.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from digitalio import DigitalInOut
from adafruit_espatcontrol import adafruit_espatcontrol
from adafruit_espatcontrol import adafruit_espatcontrol_requests as requests
import ujson
try:
import json as json_module
except ImportError:
import ujson as json_module
from adafruit_epd.epd import Adafruit_EPD
from adafruit_epd.il0373 import Adafruit_IL0373

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