From 4f65676ec608fd0d52c5e2bce3b86ea87f7142b1 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 21 Feb 2019 17:35:40 -0500 Subject: [PATCH] import either json or ujson --- adafruit_esp32spi/adafruit_esp32spi_requests.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_requests.py b/adafruit_esp32spi/adafruit_esp32spi_requests.py index 25f46be..dab8255 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_requests.py +++ b/adafruit_esp32spi/adafruit_esp32spi_requests.py @@ -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' @@ -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))