Skip to content

Commit

Permalink
Merge pull request #10 from dhalbert/ujson-to-json
Browse files Browse the repository at this point in the history
import either json or ujson
  • Loading branch information
jerryneedell authored Feb 22, 2019
2 parents 7d37fe9 + 4f65676 commit 08c1726
Showing 1 changed file with 10 additions and 4 deletions.
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

0 comments on commit 08c1726

Please sign in to comment.