Skip to content

import either json or ujson #10

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 22, 2019
Merged
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_esp32spi/adafruit_esp32spi_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,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)

def iter_content(self, chunk_size=1, decode_unicode=False):
"""An iterator that will stream data by only reading 'chunk_size'
Expand Down Expand Up @@ -176,8 +179,11 @@ def request(method, url, data=None, json=None, headers=None, stream=False):
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